How many tables?(简单并查集)
来源:互联网 发布:西安交大网络信息门户 编辑:程序博客网 时间:2024/06/08 17:34
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.
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
2
5 3
1 2
2 3
4 5
5 1
2 5
Sample Output
2
4
code
#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int FRIEND_SIZE = 1010;int pic[FRIEND_SIZE];bool mark[FRIEND_SIZE];void init(){ for(int i = 0; i < FRIEND_SIZE; ++i){ pic[i] = i; } memset(mark, false, sizeof(mark));}int find(int p){ if(p == pic[p]) return p; else return find(pic[p]);}void Union(int a, int b){ int x = find(a); int y = find(b); mark[a] = true; mark[b] = true; if(x != y){ pic[x] = y; }}int main(){ int T; scanf("%d", &T); while(T--){ init(); int m, n, cnt = 0; scanf("%d%d", &n, &m); for(int i = 0; i < m; ++i){ int a, b; scanf("%d%d", &a, &b); Union(a, b); } for(int i = 1; i <= n; i ++){ if(i == pic[i]){ cnt++; } } printf("%d\n", cnt); }}
- How many tables?(简单并查集)
- HDU1213 How Many Tables(简单的并查集)
- Hud 1213 How Many Tables[简单并查集]
- HDU - 1213 How Many Tables (简单并查集)
- hdu 1213 How Many Tables(UFS 简单并查集)
- 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【简单并查集】
- How Many Tables (简单并查集)
- 【hdu1213】How Many Tables【并查集】
- How Many Tables(并查集)
- How Many Tables hdu1213 并查集
- hdu1213(How Many Tables 并查集)
- hdu_1213 how many tables (并查集)
- Hdu1213 - How Many Tables - 并查集
- RequireJS 入门指南
- 《JSP极简教程》在jsp页面获取session对象
- SSM框架搭建
- 《SpringBoot极简教程》JSP格式化输出日期
- 《Spring Boot极简教程》附录1 计算机简史
- How many tables?(简单并查集)
- 《Spring Boot极简教程》附录2 编程的本质
- 《Spring Boot极简教程》第1章 Spring Boot史前简述
- 《Spring Boot极简教程》第2章 Spring Boot简史
- 《Spring Boot极简教程》第3章 一键启动应用程序
- 《Spring Boot极简教程》第4章 零XML配置的Spring Boot Application
- 《Spring Boot极简教程》第5章 Spring Boot自动配置原理
- 《Spring Boot极简教程》 第6章 Springboot数据库集成
- 各种加密算法比较