C++ | DSU Solution Explained
\nbool cmp(vector<int>& a, vector<int>& b){\n    return a[2]<b[2];\n}\nclass Solution {\npublic:\n    int par[100002];\n    \n    int find(int a){\n        if(p
```\nbool cmp(vector<int>& a, vector<int>& b){\n    return a[2]<b[2];\n}\nclass Solution {\npublic:\n    int par[100002];\n    \n    int find(int a){\n        if(par[a]==a) return a;\n        return par[a]=find(par[a]);\n    }\n    \n    void uni(int a, int b){\n        a=find(a);\n        b=find(b);\n        if(a==b) return;\n        if(a<b) swap(a,b);\n        par[b]=a;\n    }\n\t\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edge, vector<vector<int>>& queries) {\n        int i=0,j=0;\n        int q=queries.size();\n        \n        vector<vector<int> > qu;\n\t\t\n        for(i=0;i<queries.size();i++){\n            qu.push_back({queries[i][0],queries[i][1],queries[i][2],i}); //storing the index of query to keep mark of after sorting\n        }\n\t\t\n        sort(edge.begin(), edge.end(), cmp); //sort edge according to distance\n        sort(qu.begin(), qu.end(),cmp); //sort queries according to limit\n        \n        for(i=0;i<n+1;i++){\n            par[i]=i;\n        }\n        i=0;\n        vector<bool> ans(q);\n\t\t\n        while(i<q){\n            while(j<edge.size() && edge[j][2]<qu[i][2]){ //checking if edge dist is less than current limit, then union \n                uni(edge[j][0], edge[j][1]);\n                j++;\n            }\n            if(find(qu[i][0]) == find(qu[i][1])) ans[qu[i][3]]=true;\n            else ans[qu[i][3]]=false;\n            i++;\n        }\n        return ans;\n    }\n};\n```
['Union Find']