-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnion_Find_Tree.cpp
More file actions
76 lines (67 loc) · 2.05 KB
/
Union_Find_Tree.cpp
File metadata and controls
76 lines (67 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bits/stdc++.h>
using namespace std;
// クエリを表す
constexpr int UnionCmd = 0;
constexpr int IsSameCmd = 1;
// IsSameCmdの結果を表す
constexpr int SameCmdResNo = 0;
constexpr int SameCmdResYes = 1;
// Union-Findの構造体
//
// ※縦に長い木構造になっていた場合は
// このコードのままだと処理に時間がかかってしまう。
// 高速化したい場合は、
// ・経路圧縮
// ・ランク
// の処理を追加すること
struct UnionFind {
// par[i] = jなら
// データ:iの親はjであるの意
vector<int> par;
// データtgtの属する木の根を返す
int root(int tgt) {
if (par[tgt] == tgt) return tgt;
int nextdata = par[tgt];
// 再帰的に根を探す
return root(nextdata);
}
// 同じ木でない2つの木を併合する
void Unite(int x, int y) {
int root_x = root(x);
int root_y = root(y);
// 既に同じ木に属しているなら併合する必要はない
if(root_x == root_y) return;
// 異なる木に属していた場合は片方の根に付け替える
par[root_x] = root_y;
}
// 2つのデータが同じ木に属しているか判定する
bool IsSameTree(int x, int y) {
int root_x = root(x);
int root_y = root(y);
return root_x == root_y? true : false;
}
UnionFind(int N) : par(N) {
// 最初は全て自分が根として初期化する
for (int i = 0; i < N; i++) par[i] = i;
}
};
int main() {
int n, q;
cin >> n >> q;
UnionFind Union(n);
for (int i = 0; i < q; i++)
{
int command(0), x(0), y(0);
cin >> command >> x >> y;
if(command == UnionCmd) {
Union.Unite(x, y);
} else if(command == IsSameCmd) {
int res = Union.IsSameTree(x, y)? SameCmdResYes : SameCmdResNo;
cout << res << "\n";
} else {
// この処理に来ることはありえないはず
;assert(0);
}
}
return 0;
}