Next Spaceship

Driving into future…

POJ 1838 Banana Report

| Comments

View this problem on POJ: 1838 Banana.

This problem can be categorized as the Union-Find problem.

1
2
3
4
5
6
7
8
9
10
11
12
First, sort (xi,yi) by (x,y).
Find those has the same x,but y is 1 more than its precedence.
Find the roots of the two two-tuples.
Union the two roots.
Store the number of this set in the root node.

Second,sort (xi,yi) by (y,x).
Do the similar thing.

Third,find all the roots, and then sort them.
Find the first m roots.
Output the sum.

Here is my source code for this problem:

POJ 1838 Banana
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <algorithm>
using namespace std;
// Created by Leon in Feb 2011
int n,k;
int a[16010],x[16010],y[16010],b[16010];
bool cmp1(int i,int j){
    if(x[i]<x[j])return true;
    if(x[i]>x[j])return false;
    if(y[i]<y[j])return true;
    return false;
}
bool cmp2(int i,int j){
    if(y[i]<y[j])return true;
    if(y[i]>y[j])return false;
    if(x[i]<x[j])return true;
    return false;
}
int Find(int i){
    if(b[i]<0)return i;
    int p=Find(b[i]);
    b[i]=p;
    return p;
}
void Union(int i,int j){
    int x=Find(i);
    int y=Find(j);
    if(x==y)return;
    b[y]=b[x]+b[y];
    b[x]=y;
};
int main(){
    cin>>n>>k;
    int i;
    for(i=0;i<n;i++){
        scanf("%d%d",&x[i],&y[i]);
        a[i]=i;
        b[i]=-1;
    }
    sort(a,a+n,cmp1);
    for(i=1;i<n;i++){
        int t1=a[i],t2=a[i-1];
        if(x[t1]==x[t2]&&y[t1]==y[t2]+1)
            Union(t1,t2);
    }
    sort(a,a+n,cmp2);
    for(i=1;i<n;i++){
        int t1=a[i],t2=a[i-1];
        if(y[t1]==y[t2]&&x[t1]==x[t2]+1)
            Union(t1,t2);
    }
    int m=0;
    for(i=0;i<n;i++){
        if(b[i]<0){
            a[m++]=-b[i];
        }
    }
    sort(a,a+m);
    int s=0;
    for(i=0;i<k;i++){
        s+=a[m-1-i];
    }
    cout<<s;
    return 0;
}

Comments