POJ1838 Bananas 并查集

1.题目描述:

Banana
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 2364 Accepted: 867

Description

Consider a tropical forrest, represented as a matrix. The cell from the right top corner of the matrix has the coordinates (1,1), and the coordinates of the other cells are determinated by the row and the column on which the cell is. In some cells of the matrix are placed banana trees; a cell can contain no more than a banana tree. More banana trees which are neighbours on horizontal or vertical form a region of banana trees. In this kind of region, monkey CEKILI is moving easily, with her well-known agility, from a banana tree to another. 
CEKILI is eager and the bananas from a single region are not enough for her. Tarzan wants to help his friend. For that, he may connect exactly k banana tree regions knoting more lianas and so CEKILI could move from a region to another using lianas. Obviously, Tarzan must choose the regions so that the total number of banana trees from those k regions must be maximum. 

Detemine maximum number of banana trees which Tarzan can obtain connecting exactly k regions. 

Input

The input has the following structure: 
Nr K 
x(1) y(1) 
y(2) y(2) 
... 
x(Nr) y(Nr) 
Nr is the number of banana trees. K is the number of zones which can be connected. x(i) is the row of the i-th banana tree, while y(i) is the column of the i-th banana tree. 
There are Constraints: 
• 1 <= Nr <= 16000; 
• 1 <= x(i), y(i) <= 10000; 
• In the tests used for grading k will never be bigger than the number of regions; 
• Two positions are horizontally neighbours if they are on the same row and consecutive columns, respectively vertically neighbours if they are on the same column and on consecutive rows. 

Output

The output will contain on the first line the maximum number of banana trees that can be obtained by connecting the k regions.

Sample Input

10 3
7 10
1 1
101 1
2 2
102 1
7 11
200 202
2 1
3 2
103 1

Sample Output

9

Source

2.题意概述:

一只猴子喜欢吃香蕉, 每次他只能爬到相邻位置,然后给你一张地图,若干坐标有香蕉,然后猴子有个特技就是能穿越,每次可以穿越到不相邻的香蕉园,但是只有k次使用机会,问你能吃到的最大香蕉数

3.解题思路:

一开始往搜索方面想了,但实际上可用并查集解决,一个区域表示为一个并查集,两个区域相邻时,合并这两个并查集。同时记录并查集中的点数目。 
初始时,每个点为一个并查集。对x坐标相同、y坐标相差为1的点,合并它们所在的并查集,合并时需要判断两个点是否位于同一个集合。合并时需要先找到各自集合的根节点,然后让其中一个根节点指向另一个根节点完成合并。对y坐标相同、x坐标相差为1的点,合并它们所在的并查集。 
当所有相邻点进行了合并操作之后,对并查集点数目从大到小排序,取前K个值的总和作为结果输出。 

4.AC代码:

#include <stdio.h>
#include <algorithm>
#include <functional>
#define maxn 20200
using namespace std;
int pa[maxn], nm[maxn], ranks[maxn], pnm[maxn];
struct point
{
	int x, y, id;
}p[maxn];
void init()
{
	for (int i = 0; i < maxn; i++)
	{
		pa[i] = i;
		p[i].id = i;
		nm[i] = 1;
		ranks[i] = 0;
	}
}
int findset(int x)
{
	while (pa[x] != x)
		x = pa[x];
	return x;
}
void unionset(int a, int b)
{
	int a1 = findset(a);
	int b1 = findset(b);
	if (a1 != b1)
	{
		if (ranks[a1] < ranks[b1])
		{
			pa[a1] = b1;
			nm[b1] += nm[a1];
		}
		else
		{
			pa[b1] = a1;
			nm[a1] += nm[b1];
			if (ranks[a1] == ranks[b1])
				ranks[a1]++;
		}
	}
}
int cmpx(point a, point b)
{
	if (a.x == b.x)
		return a.y < b.y;
	return a.x < b.x;
}
int cmpy(point a, point b)
{
	if (a.y == b.y)
		return a.x < b.x;
	return a.y < b.y;
}

int main()
{
	int n, k, cnt = 0, ans = 0;
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; i++)
		scanf("%d%d", &p[i].x, &p[i].y);
	init();
	sort(p, p + n, cmpx);
	for (int i = 0; i < n - 1; i++)
		if (p[i].x == p[i + 1].x && p[i + 1].y - p[i].y == 1)
			unionset(p[i].id, p[i + 1].id);
	sort(p, p + n, cmpy);
	for (int i = 0; i < n - 1; i++)
		if (p[i].y == p[i + 1].y && p[i + 1].x - p[i].x == 1)
			unionset(p[i].id, p[i + 1].id);
	for (int i = 0; i < n; i++)
		if (pa[i] == i)
			pnm[cnt++] = nm[i];
	sort(pnm, pnm + cnt, greater<int>());
	for (int i = 0; i < k; i++)
		ans += pnm[i];
	printf("%d\n", ans);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值