POJ - 2236 -Wireless Network (简单并查集)

An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B. 


In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations. 
Input
The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats: 
1. "O p" (1 <= p <= N), which means repairing computer p. 
2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate. 

The input will not exceed 300000 lines. 
Output
For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
Sample Input
4 1
0 1
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4
Sample Output
FAIL
SUCCESS

题意:地震后,有N个电脑需要互联,由于缺乏硬件设施,修复好一台电脑后,只能和已修复的电脑中距离不超过d的电脑连接,但一台电脑可作为其他电脑连接的媒介。如A->B,B->C,则A->C.。“O”,A表示 修复电脑A,S A B表示查询A与B是否互联。

题解:简单并查集,将已修复后的电脑用数组存起来,当再修复一台电脑时,遍历数组,距离小于D的合并。然后将这个点加入数组,查询时只需要判断是否为一个祖先即可。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,d;
int f[2002];
struct note
{
    int x,y;
} q[2002];
int st[2002];
int cnt;
void init()
{
    for(int i=0; i<=n; i++)
        f[i]=i;
}
int getf(int v)
{
    if(f[v]==v)return v;
    else
    {
        f[v]=getf(f[v]);
        return f[v];
    }
}
int merge(int v,int u)
{
    int t1=getf(v);
    int t2=getf(u);
    if(t1!=t2)
    {
        f[t2]=t1;
        return 1;
    }
    return 0;
}
int main()
{
    while(~scanf("%d%d",&n,&d))
    {
        cnt=0;
        init();
        for(int i=1; i<=n; i++)
            scanf("%d%d",&q[i].x,&q[i].y);
        char s[5];
        int a,b;
        while(~scanf("%s",&s))
        {
            if(s[0]=='O')
            {
                scanf("%d",&a);
                for(int i=0; i<cnt; i++)
                {
                    if((q[st[i]].x-q[a].x)*(q[st[i]].x-q[a].x)+(q[st[i]].y-q[a].y)*(q[st[i]].y-q[a].y)<=d*d)
                    {
                        merge(st[i],a);
                    }
                }
                st[cnt++]=a;
            }
            else
            {
                scanf("%d%d",&a,&b);
                if(getf(a)==getf(b))
                    printf("SUCCESS\n");
                else printf("FAIL\n");
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zitian246/article/details/80088885
今日推荐