POJ1838 banana 并查集
来源:互联网 发布:志鸿优化设计历史答案 编辑:程序博客网 时间:2024/06/10 22:59
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.
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.
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.
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.
The output will contain on the first line the maximum number of banana trees that can be obtained by connecting the k regions.
10 37 101 1101 12 2102 17 11200 2022 13 2103 1
9
题意大致就是说给你一些点,然后相邻的点联通,比如(1,2),与(1,3)或是(1,3)与(2,3)
然后给你一个数,代表总共的区域,用例中为3,让你求最大的联通的点的个数。
比如用例中应该是 1 1 2 2 2 1 3 2是一个区域 101 1 102 1 103 1是一个区域 7 10 7 11是一个区域 200 202是一个区域
那么取前三个4+3+2就是9
大致的思路就是搞一个struct 把坐标x,y存进去,然后再来个num为每个点标号,就转化成了普通的并查集。
然后按照x由小到大 和y由小到大分别sort一遍,相当于对于y相邻的和x相邻的分别unite一下。之后把结果再sort一下,输出最大的前m个的和就好了。
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;const int N=16050;int fa[N],rankk[N];struct node{ int num,x,y;}pos[N];void init(){ for(int i=0;i<N;i++) fa[i]=i;}bool cmp1(const node &a, const node &b){ if(a.x==b.x) return a.y<b.y; else return a.x<b.x;}bool cmp2(const node &a, const node &b){ if(a.y==b.y) return a.x<b.x; else return a.y<b.y;}bool cmp3(const int a,const int b){ return a>b;}int findd(int x){ if(x==fa[x]) return x; else return fa[x]=findd(fa[x]);}void unite(int x,int y){ x=findd(x); y=findd(y); if(x==y) return ; else fa[x]=y;}int main(){ //freopen("in.txt","r",stdin); memset(rankk,0,sizeof(rankk)); int n,m; scanf("%d%d",&n,&m); for(int i=0;i<n;i++) { scanf("%d%d",&pos[i].x,&pos[i].y); pos[i].num=i+1; } init(); sort(pos,pos+n,cmp1); for(int i=0;i<n-1;i++) { if(pos[i].x==pos[i+1].x&&pos[i].y==pos[i+1].y-1) { unite(pos[i].num,pos[i+1].num); } } sort(pos,pos+n,cmp2); for(int i=0;i<n-1;i++) { if(pos[i].y==pos[i+1].y&&pos[i].x==pos[i+1].x-1) { unite(pos[i].num,pos[i+1].num); } } for(int i=0;i<=n;i++) { fa[i]=findd(i); } for(int i=0;i<N;i++) { rankk[fa[i]]++; } sort(rankk,rankk+n+1,cmp3); int ans=0; for(int i=0;i<m;i++) { ans+=rankk[i]; } printf("%d\n",ans); return 0;}
0 0
- POJ1838 banana 并查集
- POJ1838 Bananas 并查集
- 【poj1838】Bananas——并查集
- poj1838 并查集 抽象模型
- POJ 1838 Banana(并查集)
- POJ 1838 Banana (并查集)
- POJ 1838 Banana(并查集)
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 第6章 图的基本算法
- 你都付出了哪些努力
- HDU2026 首字母变大写
- Is it a Tree? 并查集 坑多
- POJ 3051 Satellite Photographs 可能会
- POJ1838 banana 并查集
- bzoj4730
- BZOJ2124 等差子序列
- 虚拟机设置固定IP地址,并且能访问外网
- BZOJ1128: [POI2008]Lam
- (拓扑排序)HDU 1285 确定比赛名次
- POJ 3055 Digital Friends 再想想
- Navicat 连接Oracle时 报错:Instant Client package is required for Baic and TNS connection
- 产品需求说明书 PRD模版