HDU-1213-How Many Tables(并查集)
2016-05-18 20:36
513 查看
A - How Many Tables
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
HDU 1213
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
题意:首行给出T,代表T组数据。每组数据第一行给出N,M,代表有N个人,接下来有M行,每行两个数A和B,代表A和B相互认识,或者理解成,A和B在同一集合内,输出一共有多少个集合
思路:并查集模板
代码
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
HDU 1213
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
题意:首行给出T,代表T组数据。每组数据第一行给出N,M,代表有N个人,接下来有M行,每行两个数A和B,代表A和B相互认识,或者理解成,A和B在同一集合内,输出一共有多少个集合
思路:并查集模板
代码
#include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> using namespace std; //并查集 const int maxn=1005; int num[maxn]; int N; int M; void init() { for(int i=0; i<=N; i++) num[i]=i; } int Query(int x)//查询x的祖先 { while(x!=num[x])//祖先不是自己就进入循环 x=num[x];//路径压缩 return x; } void Merge(int x,int y)//x,y合并到同一集合 { int flag_x=Query(x); int flag_y=Query(y);//获取两者祖先 if(flag_x!=flag_y)//如果不在同一祖先内 num[flag_x]=flag_y;//y放到x集合内 } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&N,&M); init(); int A,B; for(int i=1; i<=M; i++) { scanf("%d%d",&A,&B); Merge(A,B); } int count=0; for(int i=1; i<=N; i++) if(num[i]==i) count++; printf("%d\n",count); } return 0; }
相关文章推荐
- Spring整合MyBatis
- cf 558C C. Amr and Chemistry (51nod 1483)
- jeesite 下拉菜单
- NOIp2000 方格取数
- LocalBroadcastManager源码解析
- Mybatis学习--spring和Mybatis整合
- 【SSH】S2SH框架搭建
- 软件工程—开发模型汇总
- springmvc 的上传功能
- 为tabview headerview设置背景色
- 说说Android中的ANR
- jquery-实现div层逐渐显现出来
- lintcode ---- 搜索二维矩阵
- 【原】iOS学习之XMPP环境搭建
- ZigBee、GPRS在充电桩中的应用
- Js中的天坑----JS:parseInt("08")和“09”返回0
- bash引号使用
- 一只青蛙一次只能跳一阶或两阶台阶,总共要跳n阶,求总共有多少总跳法
- 使用Webpack和Babel来搭建React应用程序
- Uva 10480 Sabotage 最大流