HDU 1213 How Many Tables
来源:互联网 发布:证件照 软件 编辑:程序博客网 时间:2024/06/07 06:30
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 34520 Accepted Submission(s): 17204
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
并查集
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <string>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <map>#include <set>using namespace std;typedef long long ll;int f[1000] = {0};void init(int n) {for(int i = 1;i <= n;i++) {f[i] = i;}}int getf(int v) {if(f[v] == v) {return v;} else {f[v] = getf(f[v]);return f[v];}}void merge(int v,int u) {int t_1;int t_2;t_1 = getf(v);t_2 = getf(u);if(t_1 != t_2) {f[t_2] = t_1;}int main() {int T;cin >> T;while(T--) { int m; int n; int sum = 0; cin >> n >> m; init(n); for(int i = 0;i < m;i++) { int a; int b; cin >> a >> b; merge(a,b); } for(int i = 1;i <= n;i++) { if(f[i] == i) { sum++; } } cout << sum << endl;}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
- html对页面布局的初步理解
- java设计模式--Template模板模式
- 可平移缩放旋转的ImageView的实现
- 10个重要的Linux ps命令实战
- 泛型
- HDU 1213 How Many Tables
- centos 6.5 cannot find a valid baseurl for repo base异常
- 谈Spring事务隔离级别
- 解决eclipse导出javadoc时的“错误: 编码GBK的不可映射字符”问题
- 谈IT电影《模仿游戏》
- 用起来贼爽的Android Studio插件推荐
- Mybatis
- Android 自定义控件之 带刻度的进度条
- scp命令