[C++] Union Find using Template
\n\n# Code\n\nclass DSU{\npublic:\n    vector<int>Parent,Size;\npublic :\n\t// O(N)\n    DSU(int n){\n        Parent.resize(n);\n        Size.resize(n);\n      
\n\n# Code\n```\nclass DSU{\npublic:\n    vector<int>Parent,Size;\npublic :\n\t// O(N)\n    DSU(int n){\n        Parent.resize(n);\n        Size.resize(n);\n        for(int i=0;i<n;i++){\n            Parent[i]=i;\n            Size[i]=1;\n        }\n    }\n    // O(N)\n    int Find(int x){\n        if(x==Parent[x]) return x;\n        return Parent[x]=Find(Parent[x]);\n    }\n    //O(N)\n    bool Union(int x,int y){\n        x=Find(x);\n        y=Find(y);\n        if(x==y) return false;\n        if(Size[x]<Size[y]){\n            swap(x,y);\n        }\n        Parent[y] = x;\n        Size[x] += Size[y];\n        return true;\n    }\n};\nclass Solution {\npublic:\n    vector<bool> distanceLimitedPathsExist(int num, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {\n        int n=edgeList.size();\n        int m=queries.size();\n        vector<bool>ans(m);\n        for(int i=0;i<m;i++){\n            queries[i].push_back(i);\n        }\n        sort(edgeList.begin(),edgeList.end(),[](vector<int>a,vector<int>b)->bool{\n            return a[2]<b[2];\n        });\n        sort(queries.begin(),queries.end(),[](vector<int>a,vector<int>b)->bool{\n            return a[2]<b[2];\n        });\n        int eIdx=0;\n        DSU dsu(num);\n        for(int i=0;i<m;i++){\n            for(;eIdx<n&& edgeList[eIdx][2]<queries[i][2];eIdx++ ){\n                dsu.Union(edgeList[eIdx][0],edgeList[eIdx][1]);\n            }\n            ans[queries[i][3]]=(dsu.Find(queries[i][0])==dsu.Find(queries[i][1]));\n        }\n        return ans;\n    }\n};\n```
['C++']