Banana

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.

输入

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.

输出

The output will contain on the first line the maximum number of banana trees that can be obtained by connecting the k regions.

并查集

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int MAXSIZE=16010;
struct hh//flag:原来的序号 
{
  int x,y,flag;
}a[16010];
int ans[16010],anss,vis[16010],nr,k;
int uset[MAXSIZE],rk[MAXSIZE];
int cmp1(const hh&a,const hh&b)//按横坐标排序,下同 
{
  if(a.x==b.x)return a.y<b.y;
  return a.x<b.x;
}
int cmp2(const hh&a,const hh&b)
{
  if(a.y==b.y)return a.x<b.x;
  return a.y<b.y;
}
int cmp(int a,int b)
{
  return a>b;
}
void makeSet(int size)//数组初始化 
{
    for(int i=1;i<=size;i++)
    {
      uset[i]=i;
      rk[i]=1;
      a[i].flag=i;
    }
}
int find(int x)//路径压缩 
{
    if (x!=uset[x])uset[x]=find(uset[x]);
    return uset[x];
}
void unionSet(int x, int y)//将y链到x上去 
{
    if ((x=find(x))==(y=find(y))) return;
      rk[x]+=rk[y];
      rk[y]=0;
      uset[y]=x;
}
int main()
{
  scanf("%d%d",&nr,&k);
  makeSet(nr);
  for(int i=1;i<=nr;i++)
  {
      scanf("%d%d",&a[i].x,&a[i].y);
  }
  sort(a+1,a+1+nr,cmp1);
  for(int i=1;i<=nr-1;i++)
   if(a[i].x==a[i+1].x&&a[i+1].y-a[i].y<=1)
    unionSet(a[i].flag,a[i+1].flag);
  sort(a+1,a+1+nr,cmp2);
  for(int i=1;i<=nr-1;i++)
   if(a[i].y==a[i+1].y&&a[i+1].x-a[i].x<=1)
    unionSet(a[i].flag,a[i+1].flag);
   int t=1;
   for(int i=1;i<=nr;i++)
   {
        if(uset[i]==i)ans[t++]=rk[i];
   }
   sort(ans+1,ans+t,cmp);
   for(int i=1;i<=k;i++)
   {
        anss+=ans[i];
   }
   printf("%d",anss);
   return 0;
}
原文地址:https://www.cnblogs.com/new-hand/p/7246430.html