Skip to content

Commit 3edb5b3

Browse files
author
lucifer
committed
feat: uf
1 parent 7456f1c commit 3edb5b3

File tree

3 files changed

+52
-58
lines changed

3 files changed

+52
-58
lines changed
Binary file not shown.

public/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "leetcode cheatsheet",
44
"description": "刷题小助手,made by 力扣加加",
5-
"version": "0.6.1",
5+
"version": "0.6.2",
66
"browser_action": {
77
"default_popup": "index.html",
88
"default_title": "力扣加加"

src/codeTemplates/uf.js

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
logo: ufLogo,
66
list: [
77
{
8-
text: "乞丐版",
8+
text: "不带权并查集",
99
problems: [
1010
{
1111
title: "547. 朋友圈",
@@ -28,74 +28,68 @@ export default {
2828
{
2929
language: "py",
3030
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+
`,
5154
},
5255
],
5356
},
5457
{
55-
text: "带路径压缩(递归)",
58+
text: "带权并查集",
5659
problems: [
5760
{
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",
7263
},
7364
],
7465
codes: [
7566
{
7667
language: "py",
7768
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+
`,
9993
},
10094
],
10195
},

0 commit comments

Comments
 (0)