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
- hd1213 How Many Tables
- How Many Tables
- HDOJ How Many Tables
- hduoj1213:How Many Tables
- hdu How Many Tables
- HDU1213:How Many Tables
- HDU1213--How Many Tables
- hdu1213-How Many Tables
- hdu1213 How Many Tables
- HDU How Many Tables
- hdu_1213 How Many Tables
- hdu1213 How Many Tables
- How Many Tables
- How Many Tables
- HDU How Many Tables
- HDU1213 How Many Tables
- hdu1213 How Many Tables
- How Many Tables
- LTE-uplink 迭代检测
- Spark学习之4:SparkContext执行过程
- listcontrol不能显示列名以及不能显示后续列内容的问题
- GitHub命令行操作
- TFS-更改访问级别。增加团队聊天室功能
- hd1213 How Many Tables
- RelativeLayout相对布局
- QGraphicsView类
- 黑马程序员----java---面向对象1
- 《2015-04-24》一些基本知识点
- java 合并图片:将多个图片竖向合并在一起
- 文档兼容性之浏览器模式
- ssh 强制key登录(即禁止密码登录)
- mysql安装