题目大意:给你几个同学的集合,0为传染源,和他同组的都被传染,问有多少人被传染
最简单的并查集
结果忘了 if (rr1==rr2) return
RE了两次
#include<stdio.h>
#include<stdlib.h>
int parent[30001];
int k;
int findset(int x)
{
int tmp,s;
s=x;
while (parent[s]>=0) s=parent[s];
while (s!=x)
{
tmp=parent[x];
parent[x]=s;
x=tmp;
}
return s;
}
void unionset(int r1,int r2)
{
int rr1,rr2;
rr1=findset(r1);
rr2=findset(r2);
int tmp=parent[rr1]+parent[rr2];
if (rr1==rr2) return;
if (parent[rr1]>parent[rr2]) {
parent[rr1]=rr2;
parent[rr2]=tmp;
}
else {
parent[rr2]=rr1;
parent[rr1]=tmp;
}
return;
}
int main()
{
int a,b,n,m;
scanf("%d%d",&n,&m);
while (n!=0 || m!=0)
{
for (int i=0;i<=n;i++) parent[i]=-1;
for (int i=1;i<=m;i++)
{
scanf("%d%d",&k,&a);
for (int j=2;j<=k;j++)
{
scanf("%d",&b);
unionset(a,b);
}
}
printf("%d\n",-parent[findset(0)]);
scanf("%d%d",&n,&m);
}
}