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.
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 37 101 1101 12 2102 17 11200 2022 13 2103 1
Sample Output
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
原创粉丝点击