Easy to Understand | optimized | Simple Approach
\n\n# Code\n\nclass DSU {\n    public:\n    vector<int> Parent, Rank;\n    DSU(int n) {\n        Parent.resize(n);\n        Rank.resize(n, 0);\n        for (int
\n\n# Code\n```\nclass DSU {\n    public:\n    vector<int> Parent, Rank;\n    DSU(int n) {\n        Parent.resize(n);\n        Rank.resize(n, 0);\n        for (int i = 0; i < n; i++) Parent[i] = i;\n    }\n    int Find(int x) {\n        return Parent[x] = Parent[x] == x ? x : Find(Parent[x]);\n    }\n    bool Union(int x, int y) {\n        int xset = Find(x), yset = Find(y);\n        if (xset != yset) {\n            Rank[xset] < Rank[yset] ? Parent[xset] = yset : Parent[yset] = xset;\n            Rank[xset] += Rank[xset] == Rank[yset];\n            return true;\n        }\n        return false;\n    }\n};\n\nclass Solution {\npublic:\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {\n        DSU dsu(n);\n        for(int i=0;i<queries.size();i++)\n            queries[i].push_back(i);\n        \n        sort(queries.begin(), queries.end(), [&](auto const &a, auto const &b){\n            return a[2] < b[2];\n        });\n        sort(edgeList.begin(), edgeList.end(), [&](auto const &a, auto const &b){\n            return a[2] < b[2];\n        });\n        \n        int i=0;\n        vector<bool> res(queries.size(), false);\n        for(auto q: queries){\n            while(i<edgeList.size() && edgeList[i][2]<q[2]){\n                dsu.Union(edgeList[i][0] , edgeList[i][1]);\n                i++;\n            }\n            \n            if(dsu.Find(q[0]) == dsu.Find(q[1]))\n                res[q[3]] = true;\n        }\n        \n        return res;\n    }\n};\n```
['C++']