diff --git a/problems/1971.寻找图中是否存在路径.md b/problems/1971.寻找图中是否存在路径.md index 29e50ab8..5660233c 100644 --- a/problems/1971.寻找图中是否存在路径.md +++ b/problems/1971.寻找图中是否存在路径.md @@ -134,6 +134,22 @@ public: } }; ``` + +PYTHON并查集解法如下: +```PYTHON +class Solution: + def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: + p = [i for i in range(n)] + def find(i): + if p[i] != i: + p[i] = find(p[i]) + return p[i] + for u, v in edges: + p[find(u)] = find(v) + return find(source) == find(destination) +``` + +