EASY
\n# Code\n\nclass DisjointSet {\nprivate:\n    vector<int> parent, rank;\n\npublic:\n    DisjointSet(int n) {\n        parent.resize(n+1, 0);\n        rank.resi
\n# Code\n```\nclass DisjointSet {\nprivate:\n    vector<int> parent, rank;\n\npublic:\n    DisjointSet(int n) {\n        parent.resize(n+1, 0);\n        rank.resize(n+1, 0);\n\n        for(int i=0; i<n+1; i++) {\n            parent[i] = i;\n        }\n    }\n\n    int findParent(int node) {\n        if(node == parent[node])\n            return node;\n        \n        return parent[node] = findParent(parent[node]);\n    }\n\n    void unionByRank(int u, int v) {\n        int upU = findParent(u);\n        int upV = findParent(v);\n\n        if(upU == upV)\n            return;\n        \n        if(rank[upU] < rank[upV]) {\n            parent[upU] = upV;\n        }\n        else {\n            parent[upV] = upU;\n            rank[upU]++;\n        }\n    }\n\n    bool isParentEqual(int u, int v) {\n        return (findParent(u) == findParent(v));\n    }\n};\n\nclass Solution {\n    bool static compare(const vector<int> &a, const vector<int> &b) {\n        return (a[2] < b[2]);\n    }\n\npublic:\n    vector<bool> distanceLimitedPathsExist(int nodes, vector<vector<int>>& edges, vector<vector<int>>& queries) {\n        int n = queries.size();\n\n        vector<bool> ans(n, false);\n        \n        for(int i=0; i<n; i++) {\n            queries[i].push_back(i);\n        }\n\n        sort(edges.begin(), edges.end(), compare);\n        sort(queries.begin(), queries.end(), compare);\n\n        DisjointSet ds(nodes);\n        int i=0;\n\n        for(auto &query : queries) {\n            int u = query[0];\n            int v = query[1];\n            int limit = query[2];\n            int qId = query[3];\n\n            for(; (i < edges.size()) && (edges[i][2] < limit); i++) {\n                ds.unionByRank(edges[i][0], edges[i][1]);\n            }\n            ans[qId] = ds.isParentEqual(u, v);\n        }\n\n        return ans;\n    }\n};\n```
['C++']