int parent[MAX];
void init(){
for(int i = 0; i < MAX; ++i)
parent[x] = x; // 각각 자기 자신만 존재하는 set 구성
}
int find(int x) {
if (x == parent[x])
return x;
else
return find(parent[x]);
}
void union(int a, int b){
int rootA = find(a);
int rootB = find(b);
parent[rootB] = rootA; // b's root connected to a's root
}
void union_find() {
int a, b;
cin >> a >> b;
int rootA = find(a);
int rootB = find(b);
if (rootA == rootB)
return;
if (node_rank[rootA] < node_rank[rootB]) {
parent[rootA] = rootB;
} else if (node_rank[rootA] > node_rank[rootB]) {
parent[rootB] = rootA;
} else {
parent[rootB] = rootA;
++node_rank[rootA];
}
}
int find(int x) {
if (x != parent[x])
parent[x] = find(parent[x]); // flatten the tree
return parent[x];
}