DP+BinaryLifting
\nvector<set<int>>g;\nmap<vector<int>,int>cost;\nvector<int>h,vis;\nmap<int,int>par;\nint ances[100005][18],dp[100005][18];\nvoid dfs(int u,int p){\n    vis[u]=
```\nvector<set<int>>g;\nmap<vector<int>,int>cost;\nvector<int>h,vis;\nmap<int,int>par;\nint ances[100005][18],dp[100005][18];\nvoid dfs(int u,int p){\n    vis[u]=1;\n    ances[u][0]=p;\n    vector<int>tmp={p,u};\n    dp[u][0]=cost[tmp];\n    for(int i=1;i<18;i++){\n        dp[u][i]=max(dp[ances[u][i-1]][i-1],dp[u][i-1]);\n        ances[u][i]=ances[ances[u][i-1]][i-1];\n    }\n    h[u]=h[p]+1;\n    for(int child:g[u]){\n        if(vis[child])continue;\n        dfs(child,u);\n    }\n}\nint climb(int u,int ht,int &res){\n    int bit=17;\n    while(ht>0 and bit>=0){\n        if((ht&(1<<bit))>0){\n            ht-=(1<<bit);\n            res=max(dp[u][bit],res);\n            u=ances[u][bit];\n        }\n        bit--;\n    }\n    return u;\n}\nint find(int u){\n    if(!par.count(u) or par[u]==u)return par[u]=u;\n    return par[u]=find(par[u]);\n}\nint lca(int u,int v,int& res){\n    int res1=0;\n    if(h[u]<h[v])swap(u,v);\n    u=climb(u,h[u]-h[v],res1);\n    res=max(res1,res);\n    res1=0;\n    if(u==v)return u;\n    int bit=17;\n    while(ances[u][0]!=ances[v][0] and bit>=0){\n        if(ances[u][bit]==ances[v][bit]){\n            bit--;\n            continue;\n        }\n        res=max(dp[u][bit],res);\n        res=max(dp[v][bit],res);\n        u=ances[u][bit];\n        v=ances[v][bit];\n        bit--;\n    }\n    res=max(res,dp[u][0]);\n    res=max(res,dp[v][0]);\n    return ances[u][0];\n}\n\nclass Solution {\npublic:\nvector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& e, vector<vector<int>>& q) {\n    memset(dp,0,sizeof(dp));\n    memset(ances,0,sizeof(ances));\n    h=vector<int>(n,0);\n    g=vector<set<int>>(n);\n    vis=vector<int>(n,0);\n    cost.clear();\n    par.clear();\n    vector<vector<int>>el(0);\n    for(auto v:e){\n        el.push_back({v[2],v[0],v[1]});\n    }\n    sort(el.begin(),el.end());\n    for(auto v:el){\n        \n        int p1=find(v[1]);\n        int p2=find(v[2]);\n        if(p1==p2)continue;\n        par[p1]=p2;\n        vector<int>tmp={v[1],v[2]};\n        vector<int>tmp1={v[2],v[1]};\n        cost[tmp1]=v[0];\n        cost[tmp]=v[0];\n        g[v[1]].insert(v[2]);\n        g[v[2]].insert(v[1]);\n    }\n    for(int i=0;i<n;i++){\n        if(vis[i])continue;\n        cost[{i,i}]=0;\n        dfs(i,i);\n    }\n    vector<bool>res(0);\n    for(auto v:q){\n        int ans=0;\n        lca(v[0],v[1],ans);\n        \n        if(ans<v[2] and find(v[0])==find(v[1]))res.push_back(1);\n        else res.push_back(0);\n    }\n    return res;\n}\n};\n```
[]