HDU 1213 How Many Tables 第一道并查集的题。

How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 15083    Accepted Submission(s): 7361

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

代码不是很长,可是我理解了很久很久,也不知道自己的理解是不是对的。反正分享一下。。

5 3

1 2

2 3

4 5

2

这个案例代表有编号为1到5的五个人, 三代表有三对朋友,下面分别代表1和2是朋友,2和3是朋友,4和5是朋友。。 输出2代表两张桌子。因为1和2是朋友,2和3是朋友,默认1和3也是朋友。所以三个人只要一张桌子。4和5一张桌子就够了。题目的意思就是这个。给你一个编号n,下面列出m对人的关系,问你最少需要准备多少张桌子。一个多月前我就做过这道题,那时很天真,随便想了个方法,结果了WA了,还跑去讨论区问别人(丢脸)。

以前天真的代码。。

现在我附上我所理解的代码。不知道有没理解错的。

#include <stdio.h>
int p[1005];
int find(int x)     //这个的作用就是下面的查找。
{
	if(x!=p[x])
		p[x]=find(p[x]);
	return p[x];
}
int hebing(int x,int y)  //这个的作用就是用来合并的。
{
	return  p[x]=y;     //假设a=2,b=3,此时应该有p[2]=p[3]=3。即2和3到同一张桌子了。
}
int main()
{
    int t,i,a,b;
	scanf("%d",&t);
	while(t--)
	{
		int n,m,ans=0;
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++)
			p[i]=i;   //初始化它,让编号为一的值为1,编号为2的值为2.以此类推。
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&a,&b);
			a=find(a);
			b=find(b);   //假设a=2,b=3,我认为经过这个查找之后p[2]就等于p[3]了。
			if(a!=b)
				hebing(a,b);  //合并为一个值。
		}
		for(i=1;i<=n;i++)
		{
			if(p[i]==i)   //经过M次合并之后,如果是朋友,或者间接朋友的,他们对应的值都为同一个。所以桌子就减少了。
				ans++;     //如果值还是对应的,那么就不是朋友关系,增加一张桌子。
		}
		printf("%d\n",ans);//输出注意格式,只有一个空行。
	}
	return 0;
}
时间: 2024-10-20 07:57:52

HDU 1213 How Many Tables 第一道并查集的题。的相关文章

HDU 1213 How Many Tables(模板——并查集)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 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 fri

1213 How Many Tables 简单的并查集问题

my code: #include <cstdio>#include <cstring>#include<iostream>using namespace std;int find(int num,int A []){while(num!=A[num])//{ num = A[num];return num;}// bool follow(int a,int b,int A[]){int i = find(a,A);int j = find(b,A);//cout<

[2016-03-15][HDU][1213][How Many Tables]

时间:2016-03-15 16:19:15 星期二 题目编号:[2016-03-15][HDU][1213][How Many Tables] 题目大意:请朋友吃饭,每个朋友都不喜欢和不认识的人在一桌,给出认识的关系,问至少要多少桌 输入: t组数 每组数据 n m m行 u v 表示u 和 v 认识 输出: 最少 分析:并查集,求集合的数目 #ifdef _WORK_ #include <vector> #include <list> #include <map>

hdu 1829 A Bug&#39;s Life (基础并查集)

题目: 链接:点击打开链接 题意: 给定虫子的交配关系,确定实验是否支持教授的假设即没有同性恋或者不符合假设. 思路: 是一道基础的并查集题目.存在两个集合异性和同性,给出多组关系,看这两个集合有木有联系,即是否有同性恋. 定义一个数组sex[],sex[i]表示与编号i的性别相反的虫子编号.然后将和i虫子有联系的合并为同一个集合(认为是同性的).如果findset(u) == findset(v),出现了反常行为. 代码: #include <iostream> #include <c

HDU 1829 A Bug&#39;s Life (并查集)

题目链接:请戳这里. 题目大意及思路:给定n个Bugs(shen me gui)和m对Bugs之间的关系,假定关系是两个Bugs的男女关系,那么问存不存在同性恋的情况. 那么若a与b是男女关系,b与c是男女关系,那么a和c的性别我们就可以认为是相同的.我们用可以建立两个并查集,一类放男男关系,一类放女女关系. 那么若男男关系中出现了环的情况(即有共同的根),那么同性恋关系就出现了. #include<iostream> #include<cstdio> #include<cs

hdu 1198 Farm Irrigation (搜索或并查集)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5818    Accepted Submission(s): 2521 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle

HDU 1272: 小希的迷宫(并查集)

小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25010    Accepted Submission(s): 7683 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是

HDU 2120 Ice_cream&#39;s world I【并查集】

解题思路:给出n对点的关系,求构成多少个环,如果对于点x和点y,它们本身就有一堵墙,即为它们本身就相连,如果find(x)=find(y),说明它们的根节点相同,它们之间肯定有直接或间接的相连,即形成环 样例的示意图 共3个环 Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 642    Acce

HDU 1102 Constructing Roads (裸的并查集)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13210    Accepted Submission(s): 4995 Problem Description There are N villages, which are numbered from 1 to N, and you should