# 添加了 1971. 寻找图中是否存在路径 的Python并查集解法

This commit is contained in:
CH Ye
2023-09-15 16:06:48 -04:00
parent 4316022814
commit a0462c4c25

View File

@ -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)
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>