Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb633d44

Browse files
Update
1 parent5a766d5 commitb633d44

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎problems/算法模板.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,35 @@ void backtracking(参数) {
238238
239239
```
240240

241+
##并查集
242+
243+
int n = 1005; // 更具题意而定
244+
int father[1005];
245+
246+
// 并查集初始化
247+
void init() {
248+
for (int i = 0; i < n; ++i) {
249+
father[i] = i;
250+
}
251+
}
252+
// 并查集里寻根的过程
253+
int find(int u) {
254+
return u == father[u] ? u : father[u] = find(father[u]);
255+
}
256+
// 将v->u 这条边加入并查集
257+
void join(int u, int v) {
258+
u = find(u);
259+
v = find(v);
260+
if (u == v) return ;
261+
father[v] = u;
262+
}
263+
// 判断 u 和 v是否找到同一个根
264+
bool same(int u, int v) {
265+
u = find(u);
266+
v = find(v);
267+
return u == v;
268+
}
269+
```
270+
271+
241272
(持续补充ing)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp