【HDU1214】【并查集】【通过f[x] == -1 统计树的数量】
来源:互联网 发布:小米3破解4g网络 编辑:程序博客网 时间:2024/05/29 17:30
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.
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
#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_backint 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;}
0 0
- 【HDU1214】【并查集】【通过f[x] == -1 统计树的数量】
- 【并查集入门专题1】F
- ACDREAM 01F(并查集)
- codeforces 659F (并查集 bfs)
- ABAP提取两个内表交集以及统计一个内表的某个字段=X的数量
- POJ 3729 求f(x) = K的数量转化为求两个大于等于的差 + 刘汝佳后缀数组模板的细节
- HDU 4786 生成树 并查集+极大极小值 黑白边 确定选择白边的数量
- 第17周 oj 求和 f(x)=x^2+1;求 f(0)+f(1)+...........+f(n)
- 递归实现f(x)=2f(x-1)+x^2
- 随机生成小写字母并统计每个字母的数量
- C#:另一种判断质数的代码,并统计数量
- f(x,y)=ax*x+by+c 的抽象
- 计算函数F(X)=X*X+1, X从0到所输入数n的函数值之和
- //求函数f(x)=x*x+2*x+1在【0,2】上的定积分//
- poj 2524 并查集(信仰数量)
- hdu3018Ant Trip【欧拉道路数量 并查集】
- POJ-2524-Ubiquitous Religions(并查集 集合数量)
- 求f(x)=1-x的2次方的定积分
- *** Please tell me who you are.
- poj 1287 Networking 【MST】【练模板】
- svn cleanup failed 解决办法
- POJ - 1157 LITTLE SHOP OF FLOWERS
- Easyui-Datagrid—表头灵活显示
- 【HDU1214】【并查集】【通过f[x] == -1 统计树的数量】
- phpstorm 快捷键
- c++ volatile
- asp.net mvc ページ ボタン submitの2度押しの二重送信防止
- JAVA------java概述
- python实现简单爬虫功能
- 找新朋友(hdoj--1286--欧拉函数)
- Balanced Binary Tree
- poj 1274 The Perfect Stall 【二分匹配裸题】【练模板】