POJ 2236 Wireless Network(并查集)#by zh

简单并查集,之前看错题了,样例都没跑对,后来发现查询的两台电脑必须都是修好的才行,如果不是就不用加到并查集里了,代码慢的一比,不知道哪里写挫了……
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int n,d;
int pos[1005][2],pa[1005];
bool repair[1005];
int find(int k)
{
    if(pa[k]==k)
        return k;
    return pa[k]=find(pa[k]);
}
int main()
{
    //freopen("input.txt","r",stdin);
    scanf("%d%d",&n,&d);
    memset(repair,0,sizeof(repair));
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&pos[i][0],&pos[i][1]);
        pa[i]=i;
    }
    char str[5];
    while(scanf("%s",&str)!=EOF)
    {
        if(str[0]=='O')
        {
            int v;
            scanf("%d",&v);
            repair[v]=true;
            for(int i=1;i<=n;i++)
            {
                if(i==v)
                    continue;
                if(repair[i]&&sqrt((pos[i][0]-pos[v][0])*(pos[i][0]-pos[v][0])+(pos[i][1]-pos[v][1])*(pos[i][1]-pos[v][1]))<=d)
                {
                    int x=find(i),y=find(v);
                    if(x!=y)
                        pa[x]=y;
                }
            }
        }
        else
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(find(x)==find(y))
                printf("SUCCESS\n");
            else printf("FAIL\n");
        }
    }
}

你可能感兴趣的:(POJ 2236 Wireless Network(并查集)#by zh)