minimum spanning tree ,disjoint set union
\nclass Solution {\npublic:\n    \n    // darna mt bhai\n    \n    \n   /* create a mst by given graph using krushkal\'s algorithm once mst is formed then we us
```\nclass Solution {\npublic:\n    \n    // darna mt bhai\n    \n    \n   /* create a mst by given graph using krushkal\'s algorithm once mst is formed then we use binary lifting concept for finding lowest common ansestor is used for answering the query\n \n along with storing the parent array we also maintain a maxweight array \n where maxweight[i][j] tell that what is the maximum weight in path between \n node i and node (i+2^j) \n \n this can be calculated using dfs and once these vales are calculated then\n using lowest common anscestor method we can answer the query\n \n for better understanding look at my submission\n \n */\n    // custom comparator used for sorting the edges array \n    // helpful in kruskal\'s algorithm\n    static bool comp(vector<int>&a,vector<int>&b){\n        return a[2]<b[2];\n    }\n    \n    // x=30 used for binary lifting\n    int x=30;\n    \n    // different vectors used \n    vector<vector<int>>parent,maxweight;\n    vector<int>par,Size,vis,depth;\n    vector<vector<pair<int,int>>>g;\n    \n// union find to create mst\n    void make(int i){\n        par[i]=i;\n        Size[i]=1;\n    }\n    int find(int a){\n        if(par[a]==a)return a;\n        return par[a]=find(par[a]);\n    }\n    void Union(int a,int b){\n        a=find(a),b=find(b);\n        if(a!=b){\n            if(Size[a]<Size[b])swap(a,b);\n            par[b]=a;\n            Size[a]+=Size[b];\n            return;\n        }\n    }\n    \n    //creating mst using kruskal\'s algorithm\n    \n    void create_mst(vector<vector<int>>&edge,int n){\n        sort(edge.begin(),edge.end(),comp);\n        par.clear(),Size.clear();\n        par.resize(n),Size.resize(n);\n        for(int i=0;i<n;++i)make(i);\n        g.clear(),g.resize(n);\n        for(auto e:edge){\n            int a=e[0],b=e[1],w=e[2];\n            if(find(a)!=find(b)){\n                Union(a,b);\n                g[a].push_back({b,w});\n                g[b].push_back({a,w});\n            }\n        }\n        return;\n    }\n    \n    //run dfs for calculating (initial values of parent and maxweight)\n   \n    void dfs(int i){\n        vis[i]=true;\n        for(auto ch:g[i]){\n            int v=ch.first,w=ch.second;\n            if(!vis[v]){\n                parent[v][0]=i;\n                depth[v]=depth[i]+1;\n                maxweight[v][0]=w;\n                dfs(v);\n            }\n        }\n        \n    }\n    \n    // this function calculating all parent and maxweight values\n    void cal(int n){\n     parent.clear(),maxweight.clear(),depth.clear(),depth.resize(n,0);\nparent.resize(n,vector<int>(x+1,-1)),maxweight.resize(n,vector<int>(x+1,INT_MIN));\n        vis.clear();\n        vis.resize(n,false);\n        for(int i=0;i<n;++i){\n            if(!vis[i]){\n                depth[i]=0;\n                dfs(i);\n            }\n        }\n        \n        //calculating rest of parent and maxweight values\n        \n        for(int j=1;j<=x;++j){\n            for(int i=0;i<n;++i){\n              if(parent[i][j-1]!=-1){\n    parent[i][j]=parent[parent[i][j-1]][j-1];\n    maxweight[i][j]=max(maxweight[i][j-1],maxweight[parent[i][j-1]][j-1]);\n              }\n            }\n        }\n    }\n    \n    // modified method for finding k_Th ancestor\n    \n    pair<int,int> k_th_ancestor(int a,int k){\n        int weight=INT_MIN;\n        for(int i=x;i>=0;--i){\n            if(k&(1<<i)){\n                k-=(1<<i);\n                weight=max(weight,maxweight[a][i]);\n                 a=parent[a][i];\n                if(a==-1)return {-1,INT_MIN};\n            }\n        }\n        return {a,weight};\n    }\n    \n    //function for finding lca\n    int lca(int a,int b){\n        if(depth[a]>depth[b])swap(a,b);\n        int d=depth[b]-depth[a];\n        auto pr=k_th_ancestor(b,d);\n        b=pr.first;\n        int weight=pr.second;\n        if(a==b)return weight;\n        for(int i=x;i>=0;--i){\n            if(parent[a][i]!=parent[b][i]){\n                weight=max(weight,max(maxweight[a][i],maxweight[b][i]));\n                a=parent[a][i];\n                b=parent[b][i];\n            }\n        }\n        weight=max(weight,max(maxweight[a][0],maxweight[b][0]));\n        return weight;\n    }\n    \n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {\n        create_mst(edgeList,n);\n        cal(n);\n        vector<bool>ans(queries.size());\n        for(int i=0;i<queries.size();++i){\n            int a=queries[i][0],b=queries[i][1],limit=queries[i][2];\n            if(find(a)!=find(b)){\n                ans[i]=false;\n                continue;\n            }\n            ans[i]=(lca(a,b)<limit);\n        }\n        return ans;\n    }\n};\n```
[]