HDU 1213 - How Many Tables(并查集 or dfs)


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

题目大意
n n n个人参加派对,在他们之间有 m m m条相识关系,并且这种相识关系是可以传递的A —> B、B —> C可以得出A —> C,而题目要求就是为这些人安排一个桌子的坐法,使得每章桌子上的人都认识(或通过传递认识),然后给出需要的桌子个数。


首刷自解:dfs遍历

可以将每个人看作是图上的一个结点,如果两个人相识,就说明他们之间需要加上一条边,且注意这条边是无向边,可以直观想象,当所有边都加上之后,满足可以坐在同一张桌子上的人构成了一棵,因为在这树上任意两个结点都是可达的,即他们之间有机会相识。而图中构成的树其实也就是图中的连通分量,基于此,只需要将图中连通分量的个数统计出来就可以。有一个较快的统计方法就是利用dfs:若在图中dfs几遍,就是有几个连通分量

C++代码

#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
#define Max_N 1010

//所有人的关系共同组成一个图,每个相互认识的团体构成一个连通分量
//几张桌子就是几个连通分量,计算一下dfs的次数就行 

int T, N, M;
int g[Max_N][Max_N];
bool visit[Max_N];

void dfs(int n){
	visit[n] = true;
	for(int i = 1;i <= N;i ++){
		if(g[n][i] && !visit[i])
			dfs(i);
	}
}

int dfsCount(){
	int count = 0;
	for(int i = 1;i <= N;i ++){
		if(!visit[i]){
			count ++;
			dfs(i);
		}
	}
	return count;
}


int main(){
	cin >> T;
	int A, B;
	while (T --){
		cin >> N >> M;
		memset(g, 0, sizeof(g));
		memset(visit, false, sizeof(visit));
		
		while(M --){
			cin >> A >> B;
			g[A][B] = 1;
			g[B][A] = 1;	//初始化图 
		}
		int count = dfsCount();
		cout << count << endl;
	}
	
    return 0;
}

二刷自解:并查集

上面考虑连通分量的做法实际上就是在统计集合的个数,既然是涉及到集合,就可以考虑利用并查集优化,将每对存在相识关系的人都放入到一个集合内,最后再统计一下有多少个单独的集合。

注*: 当满足x == find(x)时说明找到了一个集合的根节点,此时计数+1。

C++代码

#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1010;

int n, m, T;
int fa[N];

int find(int x){
    if(x != fa[x])      fa[x] = find(fa[x]);
    return fa[x];
}

void Union(int x, int y){
    x = find(x), y = find(y);
    if(x != y){
        fa[x] = y;
    }
}

int main(){
    cin >> T;
    while(T --){
        cin >> n >> m;
        for(int i = 1;i <= n;i ++)      fa[i] = i;
        
        for(int i = 1;i <= m;i ++){
            int a, b;
            cin >> a >> b;
            Union(a, b);
        }
        
        int cnt = 0;
        for(int i = 1;i <= n;i ++)
            if(i == find(i))        cnt ++;     //统计集合个数,即找各个集合的个数的根结点
        
        cout << cnt << endl;
    }
    
    return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值