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

在一个集合里就仅仅有当横坐标相等时。纵坐标相差为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 (并查集)的更多相关文章

  1. poj 2524 (并查集)

    http://poj.org/problem?id=2524 题意:在一所学校里面的人,都有宗教信仰,不过他们的宗教信仰有可能相同有可能不同,但你又不能直接去问他们,但你可以问他们和谁是同一个宗教.通 ...

  2. [POJ 2588]--Snakes(并查集)

    题目链接:http://poj.org/problem?id=2588 Snakes Time Limit: 1000MS   Memory Limit: 65536K   Description B ...

  3. poj 1456 Supermarket - 并查集 - 贪心

    题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...

  4. poj 2492(关系并查集) 同性恋

    题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...

  5. poj 1182 (关系并查集) 食物链

    题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...

  6. Poj(1182),种类并查集

    题目链接:http://poj.org/problem?id=1182 再次熟练种类并查集,又积累点经验,和技巧,rank 0 2 1 先计算father[x] ,再更新rank[x]; #inclu ...

  7. Poj(1703),种类并查集

    题目链接:http://poj.org/problem?id=1703 已经不是第一次接触种类并查集了,直到今天才搞懂. 感谢红黑联盟,感谢杰哥!!! 每个节点只要关系确定,不管是不是同一个集合里面, ...

  8. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  9. poj 2513 欧拉回路+并查集推断是否联通+Trie树

    http://poj.org/problem? id=2513 最初看到 第一感觉---map  一看250000的数据量 果断放弃 然后记得曾经看过.trie取代map.尤其当数据量特别大的时候 学 ...

随机推荐

  1. Sencha Touch 2 在MAC下详细的开发流程

    在不久的将来我相信Web App会流行的非常广, 能看到未来才能主宰未来.对于我们开发人员来说我觉得想成就一件伟大的事情,需要过硬的技术和好的想法,再加上决不放弃的精神,一定可以成功的. 以下在Mac ...

  2. 美化 - DropDownList控件

    地址:http://www.baidufe.com/component/dropdownlist/index.html 效果: 引用JS文件: <link href="Js/DropD ...

  3. uva 816 BFS求最短路的经典问题……

    一开始情况没有考虑周全,直接WA掉了, 然后用fgets()出现了WA,给改成scanf就AC了 题目不是很难,用心就好…… #include <iostream> #include &l ...

  4. android在其他线程中访问UI线程的方法

    1.Activity.runOnUiThread( Runnable ) 2.View.post( Runnable ) 3.View.postDelayed( Runnable, long ) 4. ...

  5. C语言深度解剖读书笔记(6.函数的核心)

    对于本节的函数内容其实就没什么难点了,但是对于函数这节又涉及到了顺序点的问题,我觉得可以还是忽略吧. 本节知识点: 1.函数中的顺序点:f(k,k++);  这样的问题大多跟编译器有关,不要去刻意追求 ...

  6. 在tornado中使用celery实现异步任务处理之中的一个

    一.简单介绍 tornado-celery是用于Tornado web框架的非堵塞 celeryclient. 通过tornado-celery能够将耗时任务增加到任务队列中处理, 在celery中创 ...

  7. 特殊的Windows消息

    WM_CREATE消息 该消息是Windows发送给视图的第一个消息.由于当应用程序框架调用Create函数时该消息就会被发送,而此时窗口创建还未完成,窗口还不可见,因此在控制函数OnCreate内部 ...

  8. 14.6.1 Creating InnoDB Tables 创建InnoDB 表:

    14.6.1 Creating InnoDB Tables 创建InnoDB 表: 创建一个InnoDB 表,使用CREATE TABLE 语句,你不需要指定 ENGINE=InnoDB子句 如果In ...

  9. Emotion英语学习

    英语学习断断续续也快两年了,以前也蜻蜓点水地写过几篇总结,但是因为思考的少,只能得到一些表面的收获.从今年三月初到现在,口语阶段已经有三个月,感触较多,所以写这篇博客对这段时间英语学习的一个整体总结. ...

  10. c/c++数组名和指针区别深入探索

    指针是C/C++语言的特色,而数组名与指针有太多的相似,甚至很多时候,数组名可以作为指针使用.于是乎,很多程序设计者就被搞糊涂了.而许多的大学老师,他们在C语言的教学过程中也错误得给学生讲解:&quo ...