Brute Force To Optimize|| Disjoint Set
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n   int f(int u,vector<int>&par)\n   {\n     if(par[u]==u) return u;\n     return par[u]=f(par[u],par);\n   }\n   void unionBySize(int u,int v,vector<int>&par,vector<int>&sz)\n   {\n     int paru=f(u,par);\n     int parv=f(v,par);\n     if(paru==parv) return;\n     if(sz[paru]>sz[parv])\n     {\n       par[parv]=paru;\n       sz[paru]+=sz[parv];\n     }\n     else\n     {\n       par[paru]=parv;\n       sz[parv]+=sz[paru];\n     }\n   }\n\n   static bool cmp(vector<int>&a,vector<int>&b)\n   {\n     return a[2]<b[2];\n   }\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {\n      vector<int>par;\n      vector<int>sz(n,0);\n      vector<bool>ans(queries.size(),0);\n      for(int i=0;i<n;i++) par.push_back(i);\n      for(int i=0;i<queries.size();i++)\n      queries[i].push_back(i);\n     sort(queries.begin(),queries.end(),cmp);\n     sort(edgeList.begin(),edgeList.end(),cmp);\n     int i=0;\n     for(auto v:queries)\n     {\n        int lim=v[2];\n        while(i<edgeList.size())\n        {\n          if(edgeList[i][2]<lim)\n          unionBySize(edgeList[i][0],edgeList[i][1],par,sz);\n          else break;\n          i++;\n        }\n        if(f(v[0],par)==f(v[1],par)) ans[v[3]]=1;\n     }\n     return ans;\n    }\n};\n```
['Union Find', 'Graph', 'Sorting', 'Shortest Path', 'C++']