只能基础查询到根节点的距离
struct DSU{
int fa[N], siz[N], deep[N];
void init(){
for(int i = 1; i <= n; i++){
fa[i] = i;
siz[i] = 1;
deep[i] = 0;
}
}
int find(int x){
int tmpfa = fa[x];
if(fa[x] != x){
fa[x] = find(fa[x]); // 先把当前父亲节点的更新了,再去计算
deep[x] += deep[tmpfa];
}
return fa[x];
}
void merge(int x, int y){
int fx = find(x), fy = find(y);
if(fx != fy){
deep[fx] += siz[fy];
siz[fy] += siz[fx];
fa[fx] = fy;
}
}
bool same(int x, int y){
return find(x) == find(y);
}
int size(int x){
return siz[find(x)];
}
} dsu;
标签:DSU,struct,int,查集,基础,带权
From: https://www.cnblogs.com/9102qyy/p/18425428