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.尤其当数据量特别大的时候 学 ...
随机推荐
- FZU 1894 (双端队列)
Problem 1894 志愿者选拔 Accept: 1166 Submit: 3683 Time Limit: 1500 mSec Memory Limit : 32768 KB Pr ...
- VC++ WIN32 sdk实现按钮自绘详解.
网上找了很多,可只是给出代码,没有详细解释,不便初学者理解.我就抄回冷饭.把这个再拿出来说说. 实例图片: 首先建立一个标准的Win32 Application 工程.选择a simple Wi ...
- 让delphi程序不受WINDOWS日期格式的影响(使用SetLocaleInfo函数和Application.UpdateFormatSettings)
如果WINDOWS系统的短日期格式为“yyyy/m/d”,执行下面的代码会报错:2013-01-29 00:00:00不是合法的日期procedure TFrmQuerySale.FormShow(S ...
- 【IACV】边缘检测技术传统的方法与理论
1.边缘检测的目的 边缘检测是图像分析中使用到的最常见的操作之一,而且相比其他任何主题来说,文献中提到的与边缘增强(edge enhancement)[1]与边缘检测(edge detection)[ ...
- [每日一题] 11gOCP 1z0-052 :2013-08-30 差异的增量备份.....................................................A1
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/10669381 正确答案:AD 执行增量备份操作时,首先需要的是增量基本备份(increme ...
- 【ASP.NET Web API教程】5.4 ASP.NET Web API批处理器
原文:[ASP.NET Web API教程]5.4 ASP.NET Web API批处理器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内容. ...
- HDU4869:Turn the pokers(费马小定理+高速幂)
Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. S ...
- Swift - 各种手势检测大全(UIGestureRecognizer及其子类)
UIGestureRecognizer有许多子类,用于监听一些常见的手势事件,这些子类主要有: 1,UISwipeGestureRecognizer:滑动(快速移动) 1 2 3 4 5 6 7 8 ...
- linux下安装node.js
1.下载 wget http://nodejs.org/dist/v0.10.32/node-v0.10.32-linux-x64.tar.gz 2.解压 tar -xvf node-v0.10.32 ...
- Erlang cowboy 处理不规范的client
Erlang cowboy 处理不规范的client Cowboy 1.0 參考 本章: Dealing with broken clients 存在很多HTTP协议的实现版本号. 很多广泛使用的cl ...