HD-1213-How Many Tables(并查集)
来源:互联网 发布:淘宝店铺装修设计方案 编辑:程序博客网 时间:2024/06/15 09:32
Problem Description
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.
Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
Sample Input
25 31 22 34 55 12 5
Sample Output
24
这是一道并查集的模板题,只需要套用模板就行
代码如下:
#include <cstdio>int i[1005];int check(int x){ int r = x; while(r!=i[r]) r = i[r]; return r;}void binchaji(int x,int y){ int fx,fy; fx = check(x); fy = check(y); if(fx!=fy) i[fx] = fy;}int main(){ int n; int p,q; scanf("%d",&n); int x,y; while(n--) { scanf("%d%d",&p,&q); for(int a = 1; a <= p; a ++) i[a] = a; for(int a = 0; a < q; a ++) { scanf("%d%d",&x,&y); binchaji(x,y); } int num = 0; for(int a = 1; a <= p; a ++) { if(i[a]==a) num++; } printf("%d\n",num); } return 0;}
阅读全文
0 0
- HD 1213 How Many Tables(裸 并查集)
- HD-1213-How Many Tables(并查集)
- HD 1213 How Many Tables 【并查集】
- How Many Tables(并查集)
- How Many Tables(并查集)
- How Many Tables(并查集)
- HDU 1213 How Many Tables(并查集)
- hdu 1213 How Many Tables(并查集练习)
- [ACM] hdu 1213 How Many Tables(并查集)
- [ACM] hdu 1213 How Many Tables(并查集)
- 杭电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(并查集)
- Spring事务管理机制的实现原理
- x264和ac编译
- HNU OJ题库 1003D相邻数对问题
- BZOJ 2957: 楼房重建 [分块][线段树]
- 第4周项目5(1)猴子选大王
- HD-1213-How Many Tables(并查集)
- [BZOJ3339]Rmq Problem 线段树应用
- 动态规划算法之:最长公共子序列 & 最长公共子串(LCS)
- RMQ算法小结(st实现)
- 8、C#里面的异常处理
- poj1556 The Doors(建图spfa+判断线段交)
- Javaweb学习总结-(六)
- 欢迎使用CSDN-markdown编辑器
- bzoj 1014 [JSOI2008]火星人prefix 哈希+splay