POJ 1838 Banana (并查集)
Description
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
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
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
在一个集合里就仅仅有当横坐标相等时。纵坐标相差为1。 或者是纵坐标相等时,横坐标差为1。
那么仅仅需分别合并横坐标相等的,和纵坐标相等的情况。用并查集解决。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#include <stack>
#include <cctype>
#include <algorithm>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;
const int mod = 99999997;
const int MAX = 0x3f3f3f3f;
const int maxn = 16005;
int n, k, f[maxn], rank[maxn];
struct C {
int x, y, id;
} in[maxn];
int Find(int x) {
return x == f[x] ? x : f[x] = Find(f[x]);
}
bool cmp0 (C a, C b) {
if(a.x != b.x) return a.x < b.x;
return a.y < b.y;
}
bool cmp1 (C a, C b) {
if(a.y != b.y) return a.y < b.y;
return a.x < b.x;
}
bool cmp2 (int a, int b) {
return a > b;
}
void Union (int p, int q) {
int i = Find(p), j = Find(q);
if(i == j) return;
rank[i] += rank[j];
f[j] = i;
rank[j] = 0;
}
int main()
{
cin >> n >> k;
for(int i = 0; i < n; i++) {
in[i].id = i;
scanf("%d%d", &in[i].x, &in[i].y);
}
for(int i = 0; i < n; i++) rank[i] = 1, f[i] = i;
sort(in, in+n, cmp0);
for(int i = 0; i < n-1; i++) {
C cur = in[i], next = in[i+1];
if(cur.x == next.x && next.y-cur.y == 1)
Union(cur.id, next.id);
}
sort(in, in+n, cmp1);
for(int i = 0; i < n-1; i++) {
C cur = in[i], next = in[i+1];
if(cur.y == next.y && next.x-cur.x == 1)
Union(cur.id, next.id);
}
sort(rank, rank+n, cmp2);
int sum = 0;
for(int i = 0; i < k; i++) sum += rank[i];
cout << sum << endl;
return 0;
}
POJ 1838 Banana (并查集)的更多相关文章
- poj 2524 (并查集)
http://poj.org/problem?id=2524 题意:在一所学校里面的人,都有宗教信仰,不过他们的宗教信仰有可能相同有可能不同,但你又不能直接去问他们,但你可以问他们和谁是同一个宗教.通 ...
- [POJ 2588]--Snakes(并查集)
题目链接:http://poj.org/problem?id=2588 Snakes Time Limit: 1000MS Memory Limit: 65536K Description B ...
- poj 1456 Supermarket - 并查集 - 贪心
题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...
- poj 2492(关系并查集) 同性恋
题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...
- poj 1182 (关系并查集) 食物链
题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...
- Poj(1182),种类并查集
题目链接:http://poj.org/problem?id=1182 再次熟练种类并查集,又积累点经验,和技巧,rank 0 2 1 先计算father[x] ,再更新rank[x]; #inclu ...
- Poj(1703),种类并查集
题目链接:http://poj.org/problem?id=1703 已经不是第一次接触种类并查集了,直到今天才搞懂. 感谢红黑联盟,感谢杰哥!!! 每个节点只要关系确定,不管是不是同一个集合里面, ...
- POJ 1182 食物链 [并查集 带权并查集 开拓思路]
传送门 P - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit ...
- poj 2513 欧拉回路+并查集推断是否联通+Trie树
http://poj.org/problem? id=2513 最初看到 第一感觉---map 一看250000的数据量 果断放弃 然后记得曾经看过.trie取代map.尤其当数据量特别大的时候 学 ...
随机推荐
- 解决Java compiler level does not match the version of the installed Java project facet.问题
其实之前遇到过Java compiler level does not match the version of the installed Java project facet.这个问题,因为当时没 ...
- 慎用MonthsBetweent和MonthSpan
●function MonthsBetween(const ANow, AThen: TDateTime): Integer; 描述: 根据两个给定的TDateTime类型的参数ANow和AThen, ...
- WinExec函数,启动其他应用程序
WinExec The WinExec function runs the specified application. Note This function is provided only fo ...
- Atitit.软件仪表盘(2)--vm子系统--资源占用监測
Atitit.软件仪表盘(2)--vm子系统--资源占用监測 1. Jvisualvm.exe 2. jprofile 3. Heap //permgen monitor 作者::老哇的爪子At ...
- UVa11183 Teen Girl Squad, 最小树形图,朱刘算法
Teen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n teenage ...
- 【ASP.NET Web API教程】5.2 发送HTML表单数据:URL编码的表单数据
原文:[ASP.NET Web API教程]5.2 发送HTML表单数据:URL编码的表单数据 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...
- VK Cup 2012 Qualification Round 1---C. Cd and pwd commands
Cd and pwd commands time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- HealthKit教程 Swift版:锻炼信息
原文:HealthKit Tutorial with Swift: Workouts 作者:Ernesto García 译者:Mr_cyz ) 欢迎回到我们的HealthKit系列教程! 在我们系列 ...
- [破解]java打包Exe工具 - Jar2Exe Wizard
打包java文件为exe的方法和软件有很多,还有一些开源的软件和一些免费的软件. 我用过的所有打包exe软件中,Jar2Exe Wizard是最好用的,但是只有一个月的试用期,需要的可以从官网下载. ...
- setjmp和longjmp函数使用详解
源地址:http://blog.csdn.net/zhuanshenweiliu/article/details/41961975 非局部跳转语句---setjmp和longjmp函数.非局部指的是, ...