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): 15086    Accepted Submission(s): 7364

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

#include<stdio.h>
int pre[1005];
int find(int root)
{ return root == pre[root] ? root : pre[root] = find(pre[root]); }

void unionroot(int root1,int root2)
{
	int x,y;
	x=find(root1);
	y=find(root2);
	if(x!=y);
	pre[x]=y;

}

int main()
{
	int i,ncase,n,m,tmp,root1,root2,x,y;
	scanf("%d",&ncase);
	while(ncase--)
	{
		tmp=0;
		scanf("%d %d",&n,&m);
		for(i=1;i<=n;i++)
		pre[i]=i;

	while(m--)
	{
			scanf("%d %d",&root1,&root2);
			x=find(root1);
			y=find(root2);
			unionroot(x,y);
	}

		for(i=1;i<=n;i++)
		{
			if(pre[i]==i)
			tmp++;
		}
		printf("%d\n",tmp);

	}
}

  

时间: 2024-10-12 21:35:08

HDU 1213 How Many Tables【并查集】的相关文章

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://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋友都可以坐同一张桌,没有关系的则要另开一桌,问需要多少张桌子(小明不坐,不考虑小明与其他人的关系)? 思路:常规的并查集.要求出所有人的老大,有几个老大就要几张桌子.那么有关系的都归为同一个老大.用数组实现,再顺便压缩路径. 1 #include <bits/stdc++.h> 2 #define

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

hdu 1213 求连通分量(并查集模板题)

求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include <queue> 7 # define LL long

[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 3635 延缓更新的并查集

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2839    Accepted Submission(s): 1097 Problem Description Five hundred years later, the number of dragon balls will increase unexpecte

HDU 3461 Code Lock(并查集的应用+快速幂)

* 65536kb,只能开到1.76*10^7大小的数组.而题目的N取到了10^7,我开始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 其实路径压缩也可以不用.............  题目的大意: 一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个.再给你M个区间[L,M],表示该区间的字母可以一起同步"增加"(从'a'变为'b'为增1,'z'增1为'a').假如一组密码按照给定的区间进行有限

HDU 1558 Segment set (并查集+线段非规范相交)

题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. 1 //1558 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <cmath> 6 #define eps 1e-8 7 #define zero(x) (((x) > 0 ? (x) : (-x)) < e

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