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): 17994    Accepted Submission(s): 8853

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

题意:

有t组数据,每组数据开头为m,n。表示有m个人,以下n行表示a,b相互认识。认识能够传递,即a认识b,b认识c,那么a就认识c。

认识的人能够用同一张桌子,问一共须要多少张桌子。

题解:

将认识的人归为一组。能够用并查集解决。

參考代码:

#include<stdio.h>
#include<string.h>
#define N 1005
int father[N];
int f(int n)
{
	return father[n]==n?

n:father[n]=f(father[n]);
}
void merge(int x,int y)
{
	int fx,fy;
	fx=f(x);
	fy=f(y);
	if(fx!=fy)
		father[fx]=fy;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int m,n,a,b,sum;
		sum=0;
		for(int i=1;i<N;i++)
		    father[i]=i;
		scanf("%d%d",&m,&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&a,&b);
			merge(a,b);
		}
		for(int i=1;i<=m;i++)
		{
			if(father[i]==i)
				sum++;
		}
		printf("%d\n",sum);
	}
	return 0;
}
时间: 2024-11-07 05:22:46

HDU - 1213 How Many Tables 解题报告的相关文章

[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 1166 敌兵布阵 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目意思:给出 N 个数你,通过对某些数进行更改(或者 + 或者 -),当输入的是 Query 的时候,需要计算出 某个区间的和. 树状数组第一题,算是模板吧 ^_^ 有个小细节,wa 了几次,细心~细心~~~细心 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <

hdu 4932 Miaomiao&#39;s Geometry 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4932 题目意思:给出 n 个点你,需要找出最长的线段来覆盖所有的点.这个最长线段需要满足两个条件:(1)每个点是某条线段的左端点或右端点   (2)任意两条线段之间的重叠部分的长度为0.(一个点重叠默认长度为0,即[1,2] , [2, 3] 视为合法).还有一点我来补充吧,就是这个最大长度是固定的,看第3组测试数据  1 9 100 10,[-7,1] , [1,9] , [10,18] , [1

Hdu 4386 Play the Dice 解题报告

hdu 4586---Play the dice 解题思路:概率 题目描述:一个骰子有n面,每面朝上的概率相同,并且每一面上面都有一个数字,其中有m面是彩色的,代表掷到彩色面的时还可以继续掷下去,问最终掷得的数字的期望是多少? 解题方法: 方法一:只考虑单独掷每一次的情况,可以发现,每次掷到的期望是和先前无关的,假设a=sum/n(每掷一次的期望都是a),比如:掷第一次的时候期望是a,掷第二次的时候期望便是(m/n)*a,因为有(m/n)的概率能够掷第二次..依次可以继续下去,等比求和即可. u

hdu 1160 FatMouse&#39;s Speed 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题目意思:给出一堆老鼠,假设有 n 只(输入n条信息后Ctrl+Z).每只老鼠有对应的weight 和 speed.现在需要从这 n 只老鼠的序列中,找出最长的一条序列,满足老鼠的weight严格递增,speed严格递减. 我们可以用一个结构体来保存老鼠的信息,包括weight, speed 以及 id(这个 id 是未排序之前的,为了输出最后信息).那么首先对 weight 进行递增排序,如

HDU 1213 How Many Tables (并查集)

How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1213 Appoint description:  System Crawler  (2015-05-25) Description Today is Ignatius' birthday. He invites a lot of friends. Now

hdu 1213 How Many Tables(并查集算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13787    Accepted Submission(s): 6760 Problem Description Today is Ignatius' b

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

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): 23012    Accepted Submission(s): 11485 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's din