hd1213 How Many Tables

来源:互联网 发布:300英雄淘宝四周年 编辑:程序博客网 时间:2024/06/13 20:25

How Many Tables

题目信息:

Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

也就是说,生日宴会上,互相认识的人才会坐一起吃饭,若A认识B,B认识C,那么ABC可以坐一起吃饭,问共需多少个桌子

#include<stdio.h>#include<string.h>#define max 1010int pre[max];int find(int x){int a=x,b;while(pre[x]!=x)x=pre[x];if(a!=x){b=pre[a];pre[a]=x;a=b;}return x;}int main(){int t,n,m,i,count,a,b,f1,f2;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);count=0;for(i=1;i<=n;++i)pre[i]=i;    for(i=0;i<m;++i)    {    scanf("%d%d",&a,&b);        f1=find(a);        f2=find(b);    //    pre[f1]=f2;    if(f1!=f2){     pre[f1]=f2;     count++;    }    }   /* for(i=1;i<=n;++i)    {    if(pre[i]==i)count++;    }*/   // printf("%d\n",count);    printf("%d\n",n-count);}return 0;}


0 0