Sorting + DSU
Complexity\n- Time complexity:O(ElogE + QlogQ + Q + E) = O(NlogN)\n\n- Space complexity:O(N)\n\n# Code\n\nclass DSU {\npublic:\n    vector<int>Parent;\n    vect
# Complexity\n- Time complexity:O(ElogE + QlogQ + Q + E) = O(NlogN)\n\n- Space complexity:O(N)\n\n# Code\n```\nclass DSU {\npublic:\n    vector<int>Parent;\n    vector<int>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\n    int Find(int x) {\n        return Parent[x] = Parent[x] == x? x : Find(Parent[x]);\n    }\n\n    void Union(int a, int b) {\n        a = Find(a);\n        b = Find(b);\n        if(Rank[a] == Rank[b]) Rank[a]++;\n        if(Rank[a] > Rank[b]){\n            Parent[b] = a;\n        }\n        else {\n            Parent[a] = b;\n        }\n    }\n};\n\nclass Solution {\npublic:\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& q) {\n        int nq = q.size();\n        for(int i = 0; i < nq; i++) {\n            q[i].push_back(i);\n        }\n        sort(begin(q), end(q), [&](auto const& a, auto const& b) {\n          return a[2] < b[2];\n        });\n        sort(begin(edgeList), end(edgeList), [&](auto const& a, auto const& b) {\n          return a[2] < b[2];\n        });\n        vector<bool>ans(nq, 0);\n        int i = 0;\n        DSU dsu(n);\n        for(int j = 0; j < nq; j++) {\n          while(i < edgeList.size() && edgeList[i][2] < q[j][2]) {\n            dsu.Union(edgeList[i][0], edgeList[i][1]);\n            i++;\n          }\n          if(dsu.Find(q[j][0]) == dsu.Find(q[j][1])) ans[q[j][3]] = 1;\n        }\n        return ans;\n    }\n};\n```
['C++']