Image Explanation🏆- [Easiest & Complete Intuition] - C++/Java/Python
Video Solution (Aryan Mittal) - Link in LeetCode Profile\nChecking Existence of Edge Length Limited Paths by Aryan Mittal\n\n\n\n# Approach & Intution\n\n\n\n\n
# Video Solution (`Aryan Mittal`) - Link in LeetCode Profile\n`Checking Existence of Edge Length Limited Paths` by `Aryan Mittal`\n![Google4.png](https://assets.leetcode.com/users/images/5e4850eb-7f31-417f-b101-c685af27e333_1682735486.275424.png)\n\n\n# Approach & Intution\n![image.png](https://assets.leetcode.com/users/images/7fdc3925-b40c-47f8-b329-8a35eb024040_1682734690.1872091.png)\n![image.png](https://assets.leetcode.com/users/images/9523d5ad-c938-487e-8fd5-fdef71f04780_1682734707.1898415.png)\n![image.png](https://assets.leetcode.com/users/images/2488c447-de39-4354-a249-24bf9be4ca37_1682734714.6824613.png)\n![image.png](https://assets.leetcode.com/users/images/2c00fdfd-e762-40ac-a2b0-b7ae02d0337d_1682734726.421154.png)\n![image.png](https://assets.leetcode.com/users/images/5aea696d-4164-4cfd-9589-306bd36a76a2_1682734735.6077356.png)\n![image.png](https://assets.leetcode.com/users/images/27921ff2-c244-4a7b-828e-f2b66e486ba0_1682734744.5216231.png)\n![image.png](https://assets.leetcode.com/users/images/2d7aedab-b35d-40cf-aa3c-687764bc7848_1682734753.2953846.png)\n![image.png](https://assets.leetcode.com/users/images/a1c8bec1-1782-4740-89f2-068c3ec8889c_1682734762.1884577.png)\n![image.png](https://assets.leetcode.com/users/images/26576915-565f-44c9-b751-ef0b71e792b3_1682734770.2168715.png)\n![image.png](https://assets.leetcode.com/users/images/b3fdfdb5-3a36-4c31-81db-2b22ffbeb8f1_1682734778.0451572.png)\n![image.png](https://assets.leetcode.com/users/images/f7df8bf9-5bd4-4002-915b-6949850892f0_1682734785.5234723.png)\n![image.png](https://assets.leetcode.com/users/images/8b06b897-cc67-4d69-bcb8-5014b39a38a6_1682734794.4986067.png)\n![image.png](https://assets.leetcode.com/users/images/5bcdbb08-4d23-475c-a3f3-3233fe5211d8_1682734821.2350752.png)\n![image.png](https://assets.leetcode.com/users/images/78f76182-2a5c-46a5-8b81-3b261bb51c03_1682734830.8271737.png)\n\n\n# Code\n`Mark the DSU Code as the template [I always use this ONLY]`\n```C++ []\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```\n```Java []\nclass DSU {\n    private int[] parent;\n    private int[] rank;\n\n    public DSU(int n) {\n        parent = new int[n];\n        rank = new int[n];\n        for (int i = 0; i < n; i++) {\n            parent[i] = i;\n        }\n    }\n\n    public int find(int x) {\n        if (parent[x] == x) {\n            return x;\n        }\n        return parent[x] = find(parent[x]);\n    }\n\n    public boolean union(int x, int y) {\n        int xset = find(x), yset = find(y);\n        if (xset != yset) {\n            if (rank[xset] < rank[yset]) {\n                parent[xset] = yset;\n            } else {\n                parent[yset] = xset;\n            }\n            if (rank[xset] == rank[yset]) {\n                rank[xset]++;\n            }\n            return true;\n        }\n        return false;\n    }\n}\n\nclass Solution {\n    public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {\n        DSU dsu = new DSU(n);\n        for (int i = 0; i < queries.length; i++) {\n            queries[i] = new int[] { queries[i][0], queries[i][1], queries[i][2], i };\n        }\n\n        Arrays.sort(queries, (a, b) -> Integer.compare(a[2], b[2]));\n        Arrays.sort(edgeList, (a, b) -> Integer.compare(a[2], b[2]));\n\n        int i = 0;\n        boolean[] res = new boolean[queries.length];\n        for (int[] q : queries) {\n            while (i < edgeList.length && 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\n        return res;\n    }\n}\n```\n```Python []\nclass DSU:\n    def __init__(self, n):\n        self.parent = list(range(n))\n        self.rank = [0] * n\n\n    def find(self, x):\n        if self.parent[x] == x:\n            return x\n        self.parent[x] = self.find(self.parent[x])\n        return self.parent[x]\n\n    def union(self, x, y):\n        xset, yset = self.find(x), self.find(y)\n        if xset != yset:\n            if self.rank[xset] < self.rank[yset]:\n                self.parent[xset] = yset\n            else:\n                self.parent[yset] = xset\n            if self.rank[xset] == self.rank[yset]:\n                self.rank[xset] += 1\n            return True\n        return False\n\nclass Solution:\n    def distanceLimitedPathsExist(self, n: int, edgeList: List[List[int]], queries: List[List[int]]) -> List[bool]:\n        dsu = DSU(n)\n        for i, q in enumerate(queries):\n            queries[i].append(i)\n\n        queries.sort(key=lambda q: q[2])\n        edgeList.sort(key=lambda e: e[2])\n\n        i = 0\n        res = [False] * len(queries)\n        for q in queries:\n            while i < len(edgeList) and edgeList[i][2] < q[2]:\n                dsu.union(edgeList[i][0], edgeList[i][1])\n                i += 1\n\n            if dsu.find(q[0]) == dsu.find(q[1]):\n                res[q[3]] = True\n\n        return res\n```\n
['Union Find', 'Graph', 'C++', 'Java', 'Python3']