1502021733-hdu-How Many Tables

How Many Tables

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 14   Accepted Submission(s) : 12

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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
题目大意
    有人请客聚餐,认识的人可以坐一桌,朋友的朋友可以坐一桌,如果这个桌子上一个人都不认识那么就得另坐一桌。
解题思路
    这是考察了并查集的知识。
代码
#include<stdio.h>
int ren[1100];
struct relation
{
	int sta,end;
}res[1100];
int relation(int a)
{
	if(ren[a]==a)
	    return a;
	else
	    return ren[a]=relation(ren[a]);
	    //retrn relation(ren[a])
	    //加上ren[a]可以优化路径
}
int main()
{
	int t;
	int n,m;
	int sum;
	int i,j,k,l;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		sum=n;
		for(i=1;i<=n;i++)
		    ren[i]=i;//初定义每个只跟自己有关系
		for(i=0;i<m;i++)
		    scanf("%d%d",&res[i].sta,&res[i].end);
		for(i=0;i<m;i++)
		{
			j=relation(res[i].sta);
			k=relation(res[i].end);
			//判断最初是不是一样
			if(j!=k)
			{
				sum--;
				ren[j]=k;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}

时间: 2024-10-07 11:01:45

1502021733-hdu-How Many Tables的相关文章

--hdu 1050 Moving Tables(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 AC code: #include<stdio.h> #include<string.h> int m[210]; int main(void) { int t,n,i,j,st,en,ma; scanf("%d",&t); while(t--) { memset(m,0,sizeof(m)); scanf("%d",&n)

HDU 1050 Moving Tables

Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23044    Accepted Submission(s): 7749 Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a

HDU 1050 Moving Tables (贪心)

Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20302    Accepted Submission(s): 6889 Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a

HDU 1213How Many Tables

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14169    Accepted Submission(s): 6949 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinn

HDU 1050.Moving Tables【细节与方法选取】【8月26】

Moving Tables Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. The floor has 200 rooms each on the north side and south side along the corridor. Recently the

HDU 1050 Moving Tables(贪心)

Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made

贪心/Hdu 1050 Moving Tables

#include<cstdio> #include<cstring> using namespace std; int a[220]; int cmax(int a,int b){return a>b?a:b;} int main() { int T; scanf("%d",&T); for (int t=1;t<=T;t++) { int n; scanf("%d",&n); int x,y,xx,yy; int

hdu 1050 Moving Tables(迷之贪心...)

题意:有400间房间按题目中图片所给的方式排列,然后给出要移动的n张桌子所要移动的范围,每张桌子要移动的范围不能出现重叠的区域:问最少要多少次才能移动完所有的桌子. 题解思路:把题目转换下,就是有n个区间,每次可以去除掉k个没有重叠部分的区间,最少要多少次能去掉所有区间.妥妥的,,贪心.可能会有人联想到经典的"区间调度问题".但很遗憾,在这里行不通:区间调度问题是按区间右端排序后尽可能选取结束时间早的,但这种贪心只是一次性选取尽可能多的区间,而本题是要求选取完所有区间所要花费次数最少.

[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 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