hdu1213(并查集)
来源:互联网 发布:js继承的作用 编辑:程序博客网 时间:2024/06/05 15:43
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10977 Accepted Submission(s): 5409
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解题思路:这题是典型的并查集应用,我考虑过两种方法,一种是将所有关系找到,然后查找有几个连通图,有几个就可以输出几个表代码如下:#include<iostream>using namespace std;int num[1002],pre[1002],sum;int find1(int a){while(a!=pre[a])a=pre[a];return a;}int main(){int n;cin>>n;int a,b,i,p1,p2,f1,f2;while(n--){cin>>a>>b;for(i=0;i<a;i++)//初始源点pre[i]=i;sum=0;for(i=0;i<b;i++){cin>>p1>>p2;f1=find1(p1);f2=find1(p2);pre[f1]=pre[f2];//形成多棵树}for(i=0;i<a;i++){if(pre[i]==i)//找到多颗树即多个连通图sum++;}cout<<sum<<endl;}return 0;}
下面这种想法是找到将图全部连通最少还要多少个关系(初始是n-1,即全部都不通)
代码如下:#include<iostream>using namespace std;int num[1002],pre[1002],sum;int find1(int a){while(a!=pre[a])a=pre[a];return a;}int main(){int n;cin>>n;int a,b,i,p1,p2,f1,f2;while(n--){cin>>a>>b;for(i=1;i<=a;i++)pre[i]=i;sum=a-1;for(i=0;i<b;i++){cin>>p1>>p2;f1=find1(p1);f2=find1(p2);if(f1!=f2)//不通将他们连通然后sum自减{pre[f1]=f2;sum--;}}cout<<sum+1<<endl;//应为要求表的个数所以加上自己才完整}return 0;}
0 0
- hdu1213(并查集)
- hdu1213 并查集
- HDU1213并查集
- HDU1213 并查集
- HDU1213(并查集)
- 并查集-hdu1213
- hdu1213 并查集
- hdu1213(并查集模板)
- hdu1232&& hdu1213(简单并查集)
- hdu1213并查集模板
- hdu1213(并查集复习)
- hdu1213(并查集模板)
- HDU1213并查集基础
- HDU1213 基础并查集
- HDU1213基础并查集
- HDU1213 并查集模板
- hdu1213(基础并查集)
- HDU1213基础并查集
- Delaunay三角剖分算法
- 第二周上机实践项目——有序的结构体数组
- 在那薪火点燃的地方
- HttpWatch工具简介及使用技巧
- java日期操作
- hdu1213(并查集)
- c++继承中的内存布局
- 实例化spring IoC容器
- int main(argc,char *argv[])的含义
- Maven pom.xml
- linux 命令缩写解析
- gson 解析json 字符串 获取对应的键值
- 【转】ACM 训练大纲
- 实例讲解Hadoop Map-Reduce过程