✅✅C++ DSU/Union Find Easy
We Rearrange the Queries and EdgeLists so as to kind of precompute for future queries by DSU\n\n# Complexity\n- Time complexity:\n- O(N+Q)\n\n- Space complexity
We Rearrange the Queries and EdgeLists so as to kind of precompute for future queries by DSU\n\n# Complexity\n- Time complexity:\n- O(N+Q)\n\n- Space complexity:\n- O(N+Q)\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int>parent,size;\n    void make(int var)\n    {\n        parent[var]=var;\n        size[var]=1;\n        return;\n    }\n    int findparent(int var)\n    {\n        if(parent[var]==var)\n        {\n            return var;\n        }\n        parent[var]=findparent(parent[var]);\n        return parent[var];\n    }\n    void Union(int var1,int var2)\n    {\n        var1=findparent(var1);\n        var2=findparent(var2);\n        if(var1==var2)\n        {\n            return;\n        }\n        if(size[var1]>size[var2])\n        {\n            parent[var2]=var1;\n            size[var1]+=size[var2];\n        }\n        else\n        {\n            parent[var1]=var2;\n            size[var2]+=size[var1];\n        }\n        return;\n    }\n    static bool comparator(vector<int>&a,vector<int>&b)\n    {\n        return (a[2]<=b[2]);\n    }\n    vector<bool> distanceLimitedPathsExist(int n,vector<vector<int>>&edgeList,vector<vector<int>>&queries) \n    {\n        parent.assign(n,0);\n        size.assign(n,0);\n        int i=0,j=0;\n        for(i=0;i<n;i++)\n        {\n            make(i);\n        }\n        for(i=0;i<queries.size();i++)\n        {\n            queries[i].push_back(i);\n        }\n        vector<bool>ans(queries.size(),false);\n        sort(edgeList.begin(),edgeList.end(),comparator);\n        sort(queries.begin(),queries.end(),comparator);\n        for(int i=0;i<queries.size();i++)\n        {\n            while(j<edgeList.size() && edgeList[j][2]<queries[i][2])\n            {\n                Union(edgeList[j][0],edgeList[j][1]);\n                j++;\n            }\n            parent[queries[i][0]]=findparent(queries[i][0]);\n            parent[queries[i][1]]=findparent(queries[i][1]);\n            ans[queries[i][3]]=(parent[queries[i][0]]==parent[queries[i][1]]);\n        }\n        return ans;\n    }\n};\n```
['C++']