@@ -5,7 +5,7 @@ export default {
5
5
logo : ufLogo ,
6
6
list : [
7
7
{
8
- text : "乞丐版 " ,
8
+ text : "不带权并查集 " ,
9
9
problems : [
10
10
{
11
11
title : "547. 朋友圈" ,
@@ -28,74 +28,68 @@ export default {
28
28
{
29
29
language : "py" ,
30
30
text : `
31
- class UF:
32
- parent = {}
33
- cnt = 0
34
- def __init__(self, M):
35
- # 初始化 parent,size 和 cnt
36
- for i in range(M):
37
- self.parent[i] = i
38
- self.cnt += 1
39
-
40
- def find(self, x):
41
- while x != self.parent[x]:
42
- x = self.parent[x]
43
- return x
44
- def union(self, p, q):
45
- if self.connected(p, q): return
46
- self.parent[self.find(p)] = self.find(q)
47
- self.cnt -= 1
48
- def connected(self, p, q):
49
- return self.find(p) == self.find(q)
50
- ` ,
31
+ class UF:
32
+ def __init__(self, M):
33
+ self.parent = {}
34
+ self.cnt = 0
35
+ # 初始化 parent,size 和 cnt
36
+ for i in range(M):
37
+ self.parent[i] = i
38
+ self.cnt += 1
39
+
40
+ def find(self, x):
41
+ if x != self.parent[x]:
42
+ self.parent[x] = self.find(self.parent[x])
43
+ return self.parent[x]
44
+ return x
45
+ def union(self, p, q):
46
+ if self.connected(p, q): return
47
+ leader_p = self.find(p)
48
+ leader_q = self.find(q)
49
+ self.parent[leader_p] = leader_q
50
+ self.cnt -= 1
51
+ def connected(self, p, q):
52
+ return self.find(p) == self.find(q)
53
+ ` ,
51
54
} ,
52
55
] ,
53
56
} ,
54
57
{
55
- text : "带路径压缩(递归) " ,
58
+ text : "带权并查集 " ,
56
59
problems : [
57
60
{
58
- title : "547. 朋友圈" ,
59
- id : "friend-circles" ,
60
- } ,
61
- {
62
- title : "721. 账户合并" ,
63
- id : "accounts-merge" ,
64
- } ,
65
- {
66
- title : "990. 等式方程的可满足性" ,
67
- id : "satisfiability-of-equality-equations" ,
68
- } ,
69
- {
70
- title : "1202. 交换字符串中的元素" ,
71
- id : "smallest-string-with-swaps" ,
61
+ title : "399. 除法求值" ,
62
+ id : "evaluate-division" ,
72
63
} ,
73
64
] ,
74
65
codes : [
75
66
{
76
67
language : "py" ,
77
68
text : `
78
- class UF:
79
- parent = {}
80
- size = {}
81
- cnt = 0
82
- def __init__(self, M):
83
- # 初始化 parent,size 和 cnt
84
- for i in range(M):
85
- self.parent[i] = i
86
- self.size[i] = 1
87
- self.cnt += 1
88
- def find(self, x):
89
- while x != self.parent[x]:
90
- # 路径压缩
91
- self.parent[x] = self.parent[self.parent[x]];
92
- x = self.parent[x]
93
- return x
94
- def union(self, p, q):
95
- if self.connected(p, q): return
96
- self.parent[self.find(p)] = self.find(q)
97
- def connected(self, p, q):
98
- return self.find(p) == self.find(q)` ,
69
+ class UF:
70
+ def __init__(self, M):
71
+ # 初始化 parent,weight
72
+ self.parent = {}
73
+ self.weight = {}
74
+ for i in range(M):
75
+ self.parent[i] = i
76
+ self.weight[i] = 0
77
+
78
+ def find(self, x):
79
+ if self.parent[x] != x:
80
+ ancestor, w = self.find(self.parent[x])
81
+ self.parent[x] = ancestor
82
+ self.weight[x] += w
83
+ return self.parent[x], self.weight[x]
84
+ def union(self, p, q, dist):
85
+ if self.connected(p, q): return
86
+ leader_p, w_p = self.find(p)
87
+ leader_q, w_q = self.find(q)
88
+ self.parent[leader_p] = leader_q
89
+ self.weight[leader_p] = dist + w_q - w_p
90
+ def connected(self, p, q):
91
+ return self.find(p)[0] == self.find(q)[0]
92
+ ` ,
99
93
} ,
100
94
] ,
101
95
} ,
0 commit comments