mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 0107.寻找存在的路径.md for python and go
Update 0107.寻找存在的路径.md for python and go
This commit is contained in:
@ -217,8 +217,127 @@ class DisJoint{
|
|||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
|
||||||
|
```python
|
||||||
|
|
||||||
|
class UnionFind:
|
||||||
|
def __init__(self, size):
|
||||||
|
self.parent = list(range(size + 1)) # 初始化并查集
|
||||||
|
|
||||||
|
def find(self, u):
|
||||||
|
if self.parent[u] != u:
|
||||||
|
self.parent[u] = self.find(self.parent[u]) # 路径压缩
|
||||||
|
return self.parent[u]
|
||||||
|
|
||||||
|
def union(self, u, v):
|
||||||
|
root_u = self.find(u)
|
||||||
|
root_v = self.find(v)
|
||||||
|
if root_u != root_v:
|
||||||
|
self.parent[root_v] = root_u
|
||||||
|
|
||||||
|
def is_same(self, u, v):
|
||||||
|
return self.find(u) == self.find(v)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
import sys
|
||||||
|
input = sys.stdin.read
|
||||||
|
data = input().split()
|
||||||
|
|
||||||
|
index = 0
|
||||||
|
n = int(data[index])
|
||||||
|
index += 1
|
||||||
|
m = int(data[index])
|
||||||
|
index += 1
|
||||||
|
|
||||||
|
uf = UnionFind(n)
|
||||||
|
|
||||||
|
for _ in range(m):
|
||||||
|
s = int(data[index])
|
||||||
|
index += 1
|
||||||
|
t = int(data[index])
|
||||||
|
index += 1
|
||||||
|
uf.union(s, t)
|
||||||
|
|
||||||
|
source = int(data[index])
|
||||||
|
index += 1
|
||||||
|
destination = int(data[index])
|
||||||
|
|
||||||
|
if uf.is_same(source, destination):
|
||||||
|
print(1)
|
||||||
|
else:
|
||||||
|
print(0)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
|
```go
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
const MaxNodes = 101
|
||||||
|
|
||||||
|
var n int
|
||||||
|
var father [MaxNodes]int
|
||||||
|
|
||||||
|
// 初始化并查集
|
||||||
|
func initialize() {
|
||||||
|
for i := 1; i <= n; i++ {
|
||||||
|
father[i] = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 并查集里寻根的过程
|
||||||
|
func find(u int) int {
|
||||||
|
if u == father[u] {
|
||||||
|
return u
|
||||||
|
}
|
||||||
|
father[u] = find(father[u])
|
||||||
|
return father[u]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断 u 和 v 是否找到同一个根
|
||||||
|
func isSame(u, v int) bool {
|
||||||
|
return find(u) == find(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 v->u 这条边加入并查集
|
||||||
|
func join(u, v int) {
|
||||||
|
rootU := find(u)
|
||||||
|
rootV := find(v)
|
||||||
|
if rootU != rootV {
|
||||||
|
father[rootV] = rootU
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var m, s, t, source, destination int
|
||||||
|
fmt.Scan(&n, &m)
|
||||||
|
initialize()
|
||||||
|
for i := 0; i < m; i++ {
|
||||||
|
fmt.Scan(&s, &t)
|
||||||
|
join(s, t)
|
||||||
|
}
|
||||||
|
fmt.Scan(&source, &destination)
|
||||||
|
if isSame(source, destination) {
|
||||||
|
fmt.Println(1)
|
||||||
|
} else {
|
||||||
|
fmt.Println(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
### Rust
|
### Rust
|
||||||
|
|
||||||
### Javascript
|
### Javascript
|
||||||
|
Reference in New Issue
Block a user