[ C++ ] [ Disjoint Set Union ]
Code\n\nclass DisjointSet{\n    public:\n    vector<int>size;\n    vector<int>parent;\n    DisjointSet(int n){\n        size.resize(n,1);\n        parent.resize
# Code\n```\nclass DisjointSet{\n    public:\n    vector<int>size;\n    vector<int>parent;\n    DisjointSet(int n){\n        size.resize(n,1);\n        parent.resize(n);\n        for(int i=0;i<n;i++)parent[i]=i;\n    }\n    \n    int findUpar(int node){\n        if(node==parent[node])return node;\n\n       return parent[node]=findUpar(parent[node]);\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 {\npublic:\n    static bool cmp(vector<int>&a,vector<int>&b){\n        return a[2]<b[2];\n    }\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {\n        DisjointSet ds(n);\n        \n        for(int i=0;i<queries.size();i++){\n            queries[i].push_back(i);\n        }\n        vector<bool>ans(queries.size(),false);\n        sort(queries.begin(),queries.end(),cmp);\n        sort(edgeList.begin(),edgeList.end(),cmp);\n        int j=0;\n        for(int i=0;i<queries.size();i++){\n            while(j<edgeList.size()&&edgeList[j][2]<queries[i][2]){\n                ds.UnionBySize(edgeList[j][0],edgeList[j][1]);\n                j++;\n            }\n            if(ds.findUpar(queries[i][0])==ds.findUpar(queries[i][1])){\n                ans[queries[i][3]]=true;\n            }\n        }\n        return ans;\n\n    }\n};\n```
['C++']