[C++] || Disjoint Set Union Data Structure
Sort queries based on limits .\n\n Sort edgeList based on weights.\n\n Find lower_bound of limit in edgeList . This indicates that upto idx index all the weighs
* Sort queries based on limits .\n\n* Sort edgeList based on weights.\n\n* Find lower_bound of limit in edgeList . This indicates that upto `idx` index all the weighs in edgesList will be **strictly smaller** than `limit`.\n\n* For all the connections in edgeList make components/sets using DSU. (DSU is the best data strcuture for this purpose).\n\n* If Two elements come under a common set , then this shows that every edge in their path is strictly smalller than `limit`.\n\n\n\n\n```\nclass DSU{\nprivate:\n    int n ; \n    vector<int> parent, rank ;\n    \npublic:\n    DSU(int n){\n        this->n = n ;\n        parent.resize(n);\n        iota(begin(parent),end(parent),0);\n        rank.resize(n,1) ;\n    }\n    \n    int find_parent(int node){\n        if(node == parent[node]) return node ;\n        return parent[node] = find_parent(parent[node]) ;\n    }\n    void Union(int u , int v){\n        int U = find_parent(u) , V = find_parent(v) ;\n        if(U == V) return ;\n        if(rank[U] < rank[V]) swap(U,V) ;\n        rank[U] += rank[V] ;\n        parent[V] = U ;\n    }\n    \n};\n\nstruct cmp{\n    bool operator()(const vector<int> &v1 , const vector<int> &v2){\n        return v1[2] < v2[2] ;\n    }\n};\n\nclass Solution {\npublic:\n    \n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {\n        DSU dsu(n) ;\n        vector<bool> ans(queries.size(),false) ;\n        for(int i = 0 ; i < queries.size() ; ++i ) queries[i].push_back(i) ;\n        \n        sort(begin(queries),end(queries),[&](const vector<int> &v1 ,const vector<int> &v2)->bool{\n           return v1[2] < v2[2] ; \n        });\n        sort(begin(edgeList),end(edgeList),[&](const vector<int> &v1 , const vector<int> &v2)->bool{\n           return v1[2] < v2[2] ; \n        });\n        \n        // for(auto &x : edgeList) cout << x[2] << " " ; cout << endl ;\n        // for(auto &x : queries) cout << x[2] << " " ; cout << endl ;\n        \n        int prev = 0 ;\n        for(auto &x : queries){\n            int p = x[0] , q = x[1] , limit = x[2] , i = x[3] ;\n\t\t\t// to make sure comparision is done based on 2nd index in edgeList form a custom comparator cmp\n            int idx = lower_bound(begin(edgeList) + prev , end(edgeList),vector<int>{INT_MIN,INT_MIN,limit},cmp()) - begin(edgeList) - 1 ;\n            \n            for(int j = prev ; j <= idx ; ++j ){\n                //cout << j << endl ;\n                int u = edgeList[j][0] , v = edgeList[j][1] , dis = edgeList[j][2] ;\n                dsu.Union(u,v) ;\n            }\n            prev = idx + 1 ;\n\t\t\t//Check if p and q fall under the same set or not ?\n            if(dsu.find_parent(p) == dsu.find_parent(q)) ans[i] = true ;\n        }\n        return ans ;\n    }\n};\n```
['Graph', 'C']