Disjoint-set --POJ-2236

Title meaning

Given the coordinates of a bunch of bad computer and a series of operations

If the distance is less good computer can be viewed as a set d

O is then COMPUTER, S is not a query set of two computer

Topic analysis

Obviously disjoint-set problem, but to identify the good and bad of the computer, and then only in a good computer with a disjoint-set

Note: If you let [new] father repaired computer pointing [computer], then repaired before

May be wrong, that this collection of computers in a different collection

In this case, put the computer repaired before [] father [point] new computer repaired so will not be wrong

Topic Code

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=1007;
int n,d,p,q,repair[maxn],f[maxn];
char s[2];
struct node{
    int x,y,ok;
}com[maxn];
int dis(node a,node b){
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int get(int x){
    if(f[x]==x)return x;
    else return f[x]=get(f[x]);
}
int main(){
    scanf("%d%d",&n,&d),d=d*d;
    for(int i=1;i<=n;i++){
        scanf("%d%d",&com[i].x,&com[i].y);
        com[i].ok=0;
        f[i]=i;
    }
    int cnt=0;
    while(~scanf("%s%d",&s,&p)){
        if(s[0]=='O'){
            com[p].ok=1;
            repair[cnt++]=p;
            for(int i=0;i<cnt-1;i++){
                int fx=get(repair[i]),fy=get(p);
                if(dis(com[p],com[repair[i]])<=d)
                    f[fx]=fy;
            }
        }
        else if(s[0]=='S'){
            scanf("%d",&q);
            int fx=get(p),fy=get(q);
            if(fx==fy&&com[p].ok&&com[q].ok)printf("SUCCESS\n");
            else printf("FAIL\n");
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/helman/p/11234278.html