POJ 2236 Wireless Network

/* 此题是并查集的应用,只不过是增加了一个判断距离的
* 条件,这样每次输入一个点时,就需要和所有以前的点
* 进行判断,因此时间有点多,没有想到很好的方法,真不
* 知道100ms是怎么做出来的。。。。
* */
#include<iostream>
using namespace std;

struct Node{
    
int x,y;
}node[
1010];
int op[1010],tree[1010],n,d;
void make_set()
{
    
for(int i=0;i<=n;++i)
        tree[i] 
= -1;
}
int f(const int& x,const int& y,const int& xx,const int& yy)
{
    
int tmp = (x-xx)*(x-xx)+(y-yy)*(y-yy);
    
return tmp>d?0:1;
}
int find_set(int x)
{
    
int root = x;
    
int tmp;
    
while(tree[root]>=0)
        root 
= tree[root];
    
while(x!=root)
    {
        tmp 
= tree[x];
        tree[x] 
= root;
        x 
= tmp;
    }
    
return root;
}
void union_set(const int& root1,const int& root2)
{
    
int a = tree[root1];
    
int b = tree[root2];
    
if(a>b)
    {
        tree[root1] 
= root2;
        tree[root2] 
= a+b;
    }
else
    {
        tree[root2] 
= root1;
        tree[root1] 
= a+b;
    }
}
int main()
{
    
int i,j,root1,root2,n1,n2,cnt=0;
    
char ch;
    cin
>>n>>d;
    d 
= d*d;
    
for(i=1;i<=n;++i)
        scanf(
"%d%d",&node[i].x,&node[i].y);
        make_set();
    
while(scanf("%c",&ch) != EOF)
    {
    
        
if(ch == 'O')
        {
            scanf(
"%d",&op[cnt]);
            
for(i=0;i<cnt;++i)
                
if(f(node[op[cnt]].x,node[op[cnt]].y,node[op[i]].x,node[op[i]].y))
                {
                    root1 
= find_set(op[cnt]);
                    root2 
= find_set(op[i]);
                    
if(root1!=root2)
                        union_set(root1,root2);
                }
                
++cnt;
        }
else if(ch == 'S')
        {
            scanf(
"%d%d",&n1,&n2);
            root1 
= find_set(n1);
            root2 
= find_set(n2);
            
if(root1!=root2)
                printf(
"FAIL\n");
            
else
                printf(
"SUCCESS\n");
        }
    }
    
return 0;
}
原文地址:https://www.cnblogs.com/lvpengms/p/1662790.html