HDU 1213 How Many Tables
来源:互联网 发布:Windows无响应 编辑:程序博客网 时间:2024/06/06 00:23
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.
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.
25 31 22 34 55 12 5
24
AC代码:
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int pre[1000],vis[1005];int find(int x){int r=x;while(pre[r]!=r){r=pre[r];}int i=x,j;while(i!=r){j=pre[i];pre[i]=r;i=j;}return r;}int main(){int i,t,n,m,a,b,fa,fb,ans;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for( i=1;i<=n;i++){pre[i]=i;}memset(vis,0,sizeof(vis));while(m--){scanf("%d%d",&a,&b);fa=find(a),fb=find(b);if(fa!=fb){pre[fb]=fa;}}ans=0;for(i=1;i<=n;i++){vis[find(i)]=1;}for(i=1;i<=n;i++){if(vis[i])ans++;}printf("%d\n",ans);}return 0;}
阅读全文
0 0
- hdu 1213 how many tables
- hdu 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU 1213 How Many Tables
- hdu 1213 How Many Tables
- Hdu 1213 How Many Tables
- hdu 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU 1213 how many tables
- hdu 1213 How Many Tables
- hdu 1213how many tables
- hdu 1213 How Many Tables
- Hdu 1213 - How Many Tables
- HDU 1213 How Many Tables
- HDU 1213 How Many Tables
- hdu 1213 How Many Tables
- 程序员应该吃什么
- ROS 创建服务和请求
- css : 标准文档流
- Linux学习笔记:用户和组管理
- 思诚90后程序员们对老爸说的话 | …
- HDU 1213 How Many Tables
- 说出你的偶像 | SCTV原创视频
- Keras学习之六:训练辅助及优化工具
- 【转】Linux 中的/lib和/usr/lib目录区别
- java关键字---this 和super
- tomcat日志简介
- selenium -- cssSelector
- F
- 字符串转整数,不使用任何C语言库函数