0
点赞
收藏
分享

微信扫一扫

POJ 1611(基础并查集)


​​The Suspects​​

问题分析

基础并查集,就是加了个人数统计。

#include <cstdio>

using namespace std;

const int N = 3e4+3;
int n,m,k, total[N], f[N], father, son;

int GetF(int v)
{
if(f[v]==v){
return v;
}
return f[v] = GetF(f[v]);
}

void merge(int u,int v)
{
int t1 = GetF(u);
int t2 = GetF(v);
if(t1!=t2){
f[t2] = t1; //t2归属于t1
total[t1] += total[t2]; //所以t2找到组织后t1的总人数要加上t2的总人数
}
}

int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)&&(n+m)){
for(int i = 0; i < n; ++i){
f[i] = i;
total[i] = 1;
}
for(int i = 0; i < m; ++i){
scanf("%d%d",&k,&father);
for(int j = 1; j < k; ++j){
scanf("%d",&son);
merge(father,son);
}
}
printf("%d\n",total[GetF(0)]);//注意这里不可以写total[f[0]];
}

return 0;
}


举报

相关推荐

0 条评论