POJ - 1856 -- More is better 【并查集+简单离散化】

More is better
Problem Description

Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.
Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang’s selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.

Input

The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)

Output

The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.

Sample Input

4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8

Sample Output

4
2

Hint

A and B are friends(direct or indirect), B and C are friends(direct or indirect),
then A and C are also friends(indirect).

In the first sample {1,2,5,6} is the result.
In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.

题意

给定n中好友关系,起初1到1e7都在各自的房间,然后,确定时好友关系后,可以在一个房间,问一个房间能容纳最多的人是多少?

思路:

在集合合并的时,如果两个集合不同,那么在合并成同一个集合的时候,
将被合并的集合里的个数加到这个集合里面,本题给定的编号1e7以内,但是数据太水了,数据范围只有1e5以内。
坑点:就是当输入n为0时,答案为1,因为当没有朋友对时,各自在自己的房间,所以说答案为1

正解:

并查集 + 离散化,本题离散化就只存 A,B在集合中的大小,
例如 :【100, 4 ,9 ,231, 3】, 那么离散过后就是 【1,2,3,4,5】,由于本题是用并查集找集合大小,和数据的相对大小没关系。所以可以这样写。
把给定的A,B离散存储到map里面,然后在用并查集的做。

AC代码:
#include<iostream>
#include<cstdio>
#include<map>
#include<utility>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 5;
map<int, int> mp;
int fa[maxn], num[maxn], ans = 0;
void init(int n){
	for (int i = 1; i <= n; ++i){
		fa[i] = i;
		num[i] = 1;
	}
}
int find(int x){
	return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);
}
void _union(int x, int y){
	x = find(x);
	y = find(y);
	if(x != y){
		num[y] += num[fa[x]];
		ans = max(ans, num[y]);
		fa[x] = y;
	}
}
void solve(){
	int n, x, y;
	while (scanf("%d", &n) != EOF){
		int cnt = 0;
		ans = 0;
		init(maxn); 
		mp.clear();
		if (n == 0){
			puts("1");
			continue;
		}
		while (n--){
			scanf("%d%d", &x, &y);
			if(!mp[x])	mp[x] = ++cnt;
			if(!mp[y])	mp[y] = ++cnt;
			_union(mp[x], mp[y]);
		}
		printf("%d\n", ans);
	}
}
int main(){
	solve();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空皓月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值