diff --git a/problems/0684.冗余连接.md b/problems/0684.冗余连接.md index 8124cc7e..f5e84223 100644 --- a/problems/0684.冗余连接.md +++ b/problems/0684.冗余连接.md @@ -256,6 +256,23 @@ class Solution: return [] ``` +### Python简洁写法: + +```python +class Solution: + def findRedundantConnection(self, edges: List[List[int]]) -> List[int]: + n = len(edges) + p = [i for i in range(n+1)] + def find(i): + if p[i] != i: + p[i] = find(p[i]) + return p[i] + for u, v in edges: + if p[find(u)] == find(v): + return [u, v] + p[find(u)] = find(v) +``` + ### Go ```go 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) +``` + +