【poj1838】Bananas——并查集

解决一个算法问题,关于如何找出并连接最多数量的香蕉树区域,使用数据结构与算法技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Banana
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 2220 Accepted: 806

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


描述:给定一个点集,将与某点距离不超过1的点视为一个块,求给定图中点数最多的前k个块,并求出块中点的数目。


题解:点的相邻分为两种情况,第一是横坐标相同,纵坐标相差为1;第二是纵坐标相同,横坐标相差为1。那么解题时我们分别对点根据横纵坐标排序,比较纵横坐标的差,差为1时合并,之后给每个节点添加一个计数值来记录所在块的点的数目,合并时加在根节点上,注意以上操作进行了两次,就是说一个点要被算两次,第一次被合并之后它的num值就没有意义不能重复计算,所以在合并并累加之后被合并节点的num值要清零。最后对num排序取前k位累加即可。


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 16100;
int fa[maxn], deep[maxn], num[maxn];

struct point
{
	int x;
	int y;
	int No;
}p[maxn];

bool cmpx(point a, point b)               //按横坐标降序排
{
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}

bool cmpy(point a, point b)              //按纵坐标降序排
{
	if (a.y != b.y)
		return a.y < b.y;
	return a.x < b.x;
}

bool cmpu(int a, int b)
{
	return a > b;
}

void init(int size)
{
	for (int i = 0; i < size; i++)
	{
		fa[i] = i;
		deep[i] = 0;
		num[i] = 1;
	}
}

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

void unite(int x, int y)
{
	x = find(x);
	y = find(y);

	if (x == y)
		return;
	if (deep[x] < deep[y])
	{
		fa[x] = y;
		num[y] += num[x];
		num[x] = 0;                     //注意清零
	}
	else
	{
		fa[y] = x;
		if (deep[x] == deep[y])
			deep[x]++;
		num[x] += num[y];
		num[y] = 0;
	}

}

bool same(int x, int y)
{
	return find(x) == find(y);
}

int main()
{
	//freopen("input.txt", "r", stdin);
	int n, k;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		p[i].No = i;
		cin >> p[i].x >> p[i].y;
	}
	init(n);

	sort(p, p + n, cmpx);
	for (int i = 0; i < n - 1; i++)                 //注意循环边界
	{
		point cur = p[i];
		point next = p[i + 1];
		if (cur.x == next.x && next.y - cur.y == 1)
			unite(cur.No, next.No);
	}

	sort(p, p + n, cmpy);
	for (int i = 0; i < n - 1; i++)
	{
		point cur = p[i];
		point next = p[i + 1];
		if (cur.y == next.y && next.x - cur.x == 1)
			unite(cur.No, next.No);
	}

	sort(num, num + n, cmpu);                    //注意要写降序的cmp
	int sum = 0;
	for (int i = 0; i < k; i++)
	{
		sum += num[i];
	}

	cout << sum << endl;

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值