(K - Wireless Network(25) )-边更新图边union合并

K - Wireless Network(25)

 POJ - 2236 


#include<iostream>
#include<cmath>
using namespace std;
#define maxn 10005
struct node
{
    int order;
    double x,y;
    bool flag;
} a[maxn];
int mmp[maxn][maxn];
int father[maxn];
int n,u,v;
double d;
void cal(node one,node two)
{

    double temp=sqrt((one.x-two.x)*(one.x-two.x)+(one.y-two.y)*(one.y-two.y));
    if(temp>d)
        mmp[one.order][two.order]=mmp[two.order][one.order]=0;
    else
        mmp[one.order][two.order]=mmp[two.order][one.order]=1;
}
int fond(int x)
{
    return x==father[x]?x:father[x]=fond(father[x]);
}
void uon(int xx,int yy)
{
    father[fond(xx)]=fond(yy);
}
int main()
{
    char str;
    cin>>n>>d;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i].x>>a[i].y;
        a[i].flag=0;
        a[i].order=i;
        father[i]=i;
    }
    for(int i=1; i<=n; i++)
    {
        mmp[i][i]=1;
        for(int j=i+1; j<=n; j++)
        {
            cal(a[i],a[j]);
        }
    }
    while(cin>>str)
    {
        if(str=='O')
        {
            cin>>u;
            a[u].flag=1;
            for(int i=1; i<=n; i++)
                if(a[i].flag==1&&mmp[u][i]==1)
                {
                    uon(i,u);
                }
        }
        else if(str=='S')
        {
            cin>>u>>v;
            if(a[u].flag==0||a[v].flag==0)
            {
                cout<<"FAIL"<<endl;
            }
            else
            {
                if(fond(u)==fond(v))
                    cout<<"SUCCESS"<<endl;
                else
                    cout<<"FAIL"<<endl;
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/BePosit/article/details/81568739