【HDU1214】【并查集】【通过f[x] == -1 统计树的数量】
2015-08-24 20:21
253 查看
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19040 Accepted Submission(s): 9442
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.
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
Author
Ignatius.L
Source
杭电ACM省赛集训队选拔赛之热身赛
Recommend
Eddy
#include <iostream> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <list> #include <map> #include <set> #include <string> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define mp push_back int T; int n,m; const int maxn = 1010; int F[maxn]; int find(int x) { if(F[x] == -1) return x; return F[x] = find(F[x]); } void merge(int x,int y) { int fx = find(x); int fy = find(y); if(fx != fy) F[fx] = fy; } int main() { scanf("%d",&T); while(T--) { memset(F,-1,sizeof(F)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); merge(u,v); } int ans = 0; for(int i=1;i<=n;i++) { if(F[i] == -1) ans++; } printf("%d\n",ans); } return 0; }
相关文章推荐
- Java设计模式----工厂模式(二)
- asp.net mvc ページ ボタン submitの2度押しの二重送信防止
- Go web之旅(Request分析)
- *** Please tell me who you are.
- 找新朋友(hdoj--1286--欧拉函数)
- 在线网页文本编辑器ckeditor的使用
- python实现简单爬虫功能
- Java中的byte/char/string数据类型转换
- c++ volatile
- poj 1287 Networking 【MST】【练模板】
- eclipse中一键生成C头文件.h,以及一键使用NDK交叉编译
- 打印一个0到1之间的数的二进制表示
- JAVA------java概述
- 2015-08-24 part3 uboot.bin烧录
- 链表
- 找新朋友(hdoj--1286--欧拉函数)
- svn cleanup failed 解决办法
- POJ - 1157 LITTLE SHOP OF FLOWERS
- phpstorm 快捷键
- iOS的isnan函数