DSU | STRIVER Version!
\nclass DisjointSet {\n    vector<int> rank, parent, size; \npublic: \n    DisjointSet(int n) {\n        rank.resize(n+1, 0); \n        parent.resize(n+1);\n   
```\nclass DisjointSet {\n    vector<int> rank, parent, size; \npublic: \n    DisjointSet(int n) {\n        rank.resize(n+1, 0); \n        parent.resize(n+1);\n        size.resize(n+1); \n        for(int i = 0;i<=n;i++) {\n            parent[i] = i; \n            size[i] = 1; \n        }\n    }\n\n    int findUPar(int node) {\n        if(node == parent[node])\n            return node; \n        return parent[node] = findUPar(parent[node]); \n    }\n\n    void unionByRank(int u, int v) {\n        int ulp_u = findUPar(u); \n        int ulp_v = findUPar(v); \n        if(ulp_u == ulp_v) return; \n        if(rank[ulp_u] < rank[ulp_v]) {\n            parent[ulp_u] = ulp_v; \n        }\n        else if(rank[ulp_v] < rank[ulp_u]) {\n            parent[ulp_v] = ulp_u; \n        }\n        else {\n            parent[ulp_v] = ulp_u; \n            rank[ulp_u]++; \n        }\n    }\n\n    void unionBySize(int u, int v) {\n        int ulp_u = findUPar(u); \n        int ulp_v = findUPar(v); \n        if(ulp_u == ulp_v) return; \n        if(size[ulp_u] < size[ulp_v]) {\n            parent[ulp_u] = ulp_v; \n            size[ulp_v] += size[ulp_u]; \n        }\n        else {\n            parent[ulp_v] = ulp_u;\n            size[ulp_u] += size[ulp_v]; \n        }\n    }\n}; \n\n\nclass Solution {\nprivate:\n    bool static comp(vector<int> &a, vector<int> &b){\n        return a[2]<b[2];\n    }\n    bool static compQ(vector<int> &a, vector<int> &b){\n        return a[2]<b[2];\n    }\n    \n    \npublic:\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& q) {\n        sort(edgeList.begin(),edgeList.end(),comp);   //sorting according to dist\n        DisjointSet ds(n);\n        vector<bool> res(q.size());\n        for(int i=0;i<q.size();i++){\n            q[i].push_back(i);  // pushing the index in each q so we don\'t loose track of its position\n        }\n        sort(q.begin(),q.end(),compQ); //sorting according to dist\n        // for(auto it: edgeList) cout<<it[0]<<" ";\n        \n        int k=0;\n        for(int i=0;i<q.size();i++){//for each q, traverse the edgelist and insert those edges into the DSU whose dist is\n\t\t//strictly less than the q_dist\n            while(k<edgeList.size() && edgeList[k][2]<q[i][2]){\n                if(ds.findUPar(edgeList[k][0])==ds.findUPar(edgeList[k][1])){  // they are connected\n                }\n                else{ // they are not connected\n                    ds.unionByRank(edgeList[k][0],edgeList[k][1]);  \n                    \n                }\n                k++;\n            }\n\t\t\t//after we are done connecting all the edges within our limit, we check if both the nodes from the q are from the same parent. If yes, insert true into the res or else insert false into the res vector.\n            if(ds.findUPar(q[i][0])==ds.findUPar(q[i][1])){\n                res[q[i][3]]=true;\n            }\n            else res[q[i][3]]=false;\n        }\n        \n        return res;\n    }\n};\n```\n\nPeace :-)
['C']