HDOJ1213 How Many Tables 并查集裸题
来源:互联网 发布:传奇霸业战神装备数据 编辑:程序博客网 时间:2024/06/07 20:45
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33675 Accepted Submission(s): 16840
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
Author
Ignatius.L
Source
杭电ACM省赛集训队选拔赛之热身赛
Recommend
Eddy | We have carefully selected several similar problems for you: 1856 1325 1198 1879 1875
裸题,程序就是标准的模板
#include <iostream>#include <cmath>#include <ctime>using namespace std;const int maxn = 1005;int n,m,i,j,k,tot;int root[maxn];void init(){ for (i=1; i<=n; i++) root[i] = i; tot = 0;}int Find(int x){ int r = x; while (r!=root[r]) r = root[r]; int i = x, k; while (root[i] != r) { k = root[i]; root[i] = r; i = k; } return r;}void Merge(int x, int y){ int fx = Find(x), fy = Find(y); if (fx != fy) root[fx] = fy;}int main(){ std::ios::sync_with_stdio(false); int t; cin >> t; while (t--) { cin >> n >> m; init(); for (k=1; k<=m; k++) { cin >> i >> j; Merge(i,j); } for (i=1; i<=n; i++) if (root[i]==i) tot++; cout << tot << endl; } return 0;}
阅读全文
0 0
- HDOJ1213 How Many Tables 并查集裸题
- hdoj1213 How many tables?(并查集)
- HDOJ1213 How Many Tables(并查集)
- How Many Tables<hdoj1213>
- 【HDOJ1213】How Many Tables[并查集模板题]
- HDOJ1213. How many tables(并查集连通块计数)
- How Many Tables hdu1213(并)
- 【hdu1213】How Many Tables【并查集】
- How Many Tables(并查集)
- How Many Tables hdu1213 并查集
- hdu1213(How Many Tables 并查集)
- hdu_1213 how many tables (并查集)
- Hdu1213 - How Many Tables - 并查集
- HDU1213 How Many Tables 【并查集】
- 并查集---How Many Tables
- HDU1213 How Many Tables【并查集】
- HDU1213 How Many Tables 【并查集】
- [并查集]How Many Tables
- 8-19 DAIRY
- 关注民生民情——华北水利水电大学“情艺”国情社情调查
- Ring HDU
- asus 华硕 N46VM 无法开机进入BIOS
- 利用豆瓣短评数据生成词云
- HDOJ1213 How Many Tables 并查集裸题
- Java框架基础——反射(reflect)
- cookie 和session 的区别
- Django文件上传和下载
- 基本运算电路之---反向比例运算电路(1)
- 杭电oj 1068
- Djang学习05---自定义filter以及tag
- JavaSE + bluecove 蓝牙连接
- 《剑指offer》笔记-第5章(2)