首页 > 代码库 > poj 2236

poj 2236

并查集水题

 

 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 using namespace std; 8 int n,m,t; 9 struct node10 {11     int x,y;12 }A[1005];13 int rank[1005],pre[1005];14 int dist(node A,node B)15 {16     return (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y);17 }18 void init(int n)19 {20     for(int i=1;i<=n;i++)21     {22         rank[i]=1;23         pre[i]=i;24     }25 }26 int find(int x)27 {28     if(pre[x]!=x)    pre[x]=find(pre[x]);29     return pre[x];30 }31 void Union(int x,int y)32 {33     x=find(x);34     y=find(y);35     if(x==y)    return;36     if(rank[x]>=rank[y])37     {38         pre[y]=x;39         rank[x]+=rank[y];40     }41     else42     {43         pre[x]=y;44         rank[y]+=rank[x];45     }46 }47 int main()48 {49     int i,j,k;50     //freopen("1.in","r",stdin);51     scanf("%d",&n);52     init(n);53     scanf("%d",&k);54     for(i=1;i<=n;i++)   scanf("%d%d",&A[i].x,&A[i].y);55     char s[20];56     int aa[1005],tot=0;57     while(scanf("%s",s)!=EOF)58     {59         if(s[0]==O)60         {61             int q;62             scanf("%d",&q);63             for(i=0;i<tot;i++)64             {65                 if(dist(A[aa[i]],A[q])<=k*k)66                 {67                    // printf("%d %d\n",aa[i],q);68                     Union(aa[i],q);69                 }70             }71             aa[tot++]=q;72         }73         else74         {75             int p,q;76             scanf("%d%d",&p,&q);77             p=find(p);78             q=find(q);79             if(p==q)    printf("SUCCESS\n");80             else printf("FAIL\n");81         }82     }83     return 0;84 }

 

poj 2236