# 只判斷是否同群
#include <iostream> | |
using namespace std; | |
int p[1000005]; | |
void init(int n){ | |
for(int i = 0;i<n;i++) | |
p[i] = i; | |
} | |
int find_root(int x){ | |
return (p[x]==x)?x:p[x] = find_root(p[x]); | |
if (p[x] == x) return x; | |
return p[x] = find_root(p[x]); | |
} | |
void joint(int a, int b){ | |
int rootA = find_root(a); | |
int rootB = find_root(b); | |
if(rootA!=rootB) | |
p[rootB] = rootA; | |
} | |
bool same(int a,int b){ | |
int rootA = find_root(a); | |
int rootB = find_root(b); | |
return (rootA==rootB) ? true : false; | |
} |
# 紀錄數量用的 DSU
#include <iostream> | |
using namespace std; | |
int p[1000005]; | |
int number(int a),times,query; | |
void init(int n){ | |
for(int i = 0;i<n;i++) | |
p[i] = -1; | |
} | |
int find_root(int x){ | |
return p[x]<0?x:p[x] = find_root(p[x]); | |
} | |
void joint(int a, int b){ | |
int rootA = find_root(a); | |
int rootB = find_root(b); | |
if(rootA!=rootB){ | |
p[rootA] += p[rootB]; | |
p[rootB] = rootA; | |
} | |
} | |
int number(int a){ | |
return -p[find_root(a)]; | |
} |