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. perl 贪婪匹配小例子

    redis01:/root# cat x2.pl my $str="a19823a456123"; if ($str =~/a(.*)23/){print "1----& ...

  2. 基于visual Studio2013解决面试题之0407数组差

     题目

  3. Redis C客户端API - God's blog - 博客频道 - CSDN.NET

    Redis C客户端API - God's blog - 博客频道 - CSDN.NET Redis安装步骤: 1.redis server安装 wget http://redis.googlecod ...

  4. JSTL解析——005——core标签库04

    直接入主题,标签讲解 1.<c:import>标签 JSP里面有<% file include="XX"%> 与<jsp:include>,JS ...

  5. C 函数 strstr 的高效实现

          C函数库中有一个函数 strstr(char*, char*),它实现的是在一个原字符串中查找一个子串.假设找到这种一个子串,返回这个子串在原字符串中的起始位置,若没有找到这种一个子串.则 ...

  6. ThinkPHP 3 的CURD管理用户信息 修改和删除

    本节课大纲: 一.ThinkPHP 3 的CURD管理用户信息 http://localhost:8080/thinkphp/index.php/User/index 访问User类的index方法 ...

  7. C中程序的内存分配

    一.预备知识—程序的内存分配 一个由c/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. ...

  8. Android自适应不同屏幕几种方法

        因为Android设备的屏幕尺寸.分辨率区别很大.假设希望我们的应用可以在不同屏幕尺寸或分辨率的Android设备上执行,即更换Android设备后界面和字体不会因此变得混乱.则须要考虑屏幕的 ...

  9. 浅析Delphi Container库(有开源的DCLX)

    与Java和C++相比,Delphi对容器的支持实在少得可怜.Java有强大的集合框架,C++更有STL,Delphi有什么呢,不就是TList几个小巧的列表类,而TCollection系列的类更多只 ...

  10. CentOS6-釋放ip重新分配,centos7 ifconifg没有ip

    http://bbs.csdn.net/topics/390725823 系统win7 ,dhcp自动获取ip虚拟机是10.0 安装之后我装了ubuntu  选用 NAT网络, 刚装完我能上网 ,但是 ...