病毒 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 322(54 users) Total Accepted: 87(45 users) Special Judge: No Description 某种病毒袭击了某地区,该地区有N
#include<stdio.h>
int next[50010];
int find(int a)
{
return a==next[a]?a:(find(next[a]));//用到数据结构并查集的两种用法;
}
int main()
{
int n,m,i,a,b,sum,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
sum=0;
for(i=0;i<n;i++)
next[i]=i;
while(m--)
{
scanf("%d%d",&a,&b);
x=find(a);y=find(b);
if(x!=y)
{
if(x<y) next[y]=x;
else next[x]=y;
}
}
for(i=1;i<n;i++)
{
if(find(i)==0)
sum++;
}
printf("%d\n",sum+1);
}
return 0;
}