简单查并集 HDU 1213

原创 2018年04月15日 22:19:58

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. 
InputThe 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. OutputFor 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<iostream>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int main() {
	int N,n,num,w,i,j,p,a,b,ans,k[1005];
	cin>>N;	//样例数量 
	for(i=0;i<N;i++) {
		memset(k,0,sizeof(k));	//初始化,一开始都没有爸爸 
		ans=0;
		cin>>n>>num;	//人数,输入的连接数 
		for(j=1;j<=num;j++) {
			cin>>a>>b;
			if(k[a]==0) {
				if(k[b]!=0) { //仅b有爸爸时 
					k[a]=k[b]; //使a和b有共同爸爸 
				}
				if(k[b]==0) { //a和b都没有爸爸时 
					k[b]=b;	//指向自己 
					k[a]=k[b];	//使a和b有共同爸爸 
					ans++;	//多一个群 
				}
			} 
			else {
				if(k[b]==0) {	//仅a有爸爸时 
					k[b]=k[a];	//使a和b有共同爸爸 
				} 
				if(k[b]!=0&&k[a]!=k[b]) {
					w=k[b];
//因为后来合并时会改变k[b]的值,所以要先用w保留值 
					ans--;	//合并群时,减一 
					for(p=1;p<=n;p++) {	//暴力搜索一遍,合并群 
						if(k[p]==w) {
							k[p]=k[a];
						} 
					}
				}
			}
		}
		for(p=1;p<=n;p++) {	//对于没有被赋值的元素,独立成群 
			if(k[p]==0) {
				ans++;
			}
		}
		printf("%d\n",ans);
	}
} 

查并集(朋友圈问题)

假设有A,B,C,D,E,5个人A,B在一个朋友圈内,B,C在一个朋友圈内,这样A,B,C就在一个朋友圈内,总共有多少个朋友圈。 当人数较多时不容易算出。 可以利用数据结构查并集。 设5个人的初...
  • qq_35116353
  • qq_35116353
  • 2017-07-24 16:11:03
  • 1628

HDOJ 1232 查并集

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1232题意:还要修多少条路才能让n个小镇全部相连。查并集。用树来理解。在一个集合里,他们会有相同的根。有相同...
  • qq_38987374
  • qq_38987374
  • 2018-02-23 16:38:24
  • 6

并查集_hdu1213

How Many Tables http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignati...
  • whiskey_wei
  • whiskey_wei
  • 2017-04-12 18:59:47
  • 844

HDU 1863 并查集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863 题目意思:geng
  • u010008539
  • u010008539
  • 2014-08-09 17:36:34
  • 286

How Many Tables--hdu1213(并查集)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • nvliba
  • nvliba
  • 2015-08-20 16:34:00
  • 604

HDU1213_并查集

并查集水题,题意:一个人请人吃饭,相互认识的朋友在一张桌子,相互认识的朋友的意思是如果A认识B,B认识C,那么A、B、C是朋友,对于每组输入输出桌子的张数。贴代码: #include #inclu...
  • u013596211
  • u013596211
  • 2015-03-24 21:49:30
  • 873

并集查找算法设计与实现

并集算法的设计与实现
  • u011889952
  • u011889952
  • 2015-03-25 10:34:03
  • 12890

查并集

The White Belt Good developers who are familiar with the entire stack know how to make life easier f...
  • hang__xiu2016acm
  • hang__xiu2016acm
  • 2016-09-10 20:52:34
  • 716

查并集及优化

基础 对于今天要总结的算法,我想先通过一道题目来看一下: 假设现在我有一个任务交给你:要求你查看 id 为 x 和 id 为 y 的两个人是不是朋友, 在一开始我会在第一行中输入 3 个数字 ...
  • Hacker_ZhiDian
  • Hacker_ZhiDian
  • 2017-03-09 19:13:09
  • 1328

hdu 1213简单并查集

题目意思: 许多朋友
  • u013188850
  • u013188850
  • 2014-05-29 15:35:23
  • 258
收藏助手
不良信息举报
您举报文章:简单查并集 HDU 1213
举报原因:
原因补充:

(最多只允许输入30个字)