Wireless Network 并查集

来源:互联网 发布:des算法加密过程 编辑:程序博客网 时间:2024/05/17 03:59

这是我第一个并查集问题 ,求得是电脑跟电脑之间如果距离小于 d 那么可以连接 ,电脑之间都是有编号顺序的 然后问你例如1  和 4 之间可以链接吗?它们之间的电脑连通了 就是sucess  所以我们把它运用并查集  联通的就放到一个圈子里  最后查找要求的两台电脑编号在一个圈子里是否就可以了  初次体验并查集 必须有找父亲节点的函数  还有合并两个数的函数 这是并查集基本的 其余的在主函数里自己构造就好了 

Description

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 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4

Sample Output

FAILSUCCESS


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1100
bool opr[N];
int d;
struct node
{
    int par;
    int x;
    int y;
}p[N];
int fond (int x) //找根
{
    return x == p[x].par ? x : fond(p[x].par);
}
void hebing(const node p1, const node p2) //两个数合并
{
    int temp1, temp2;
    temp1 = fond(p1.par);
    temp2 = fond(p2.par);
    if(temp1 != temp2)
        if((p1.x - p2.x)*(p1.x - p2.x)+(p1.y - p2.y)*(p1.y - p2.y) <= d * d)
        p[temp2].par = temp1;
}
int main()
{
    int n;
    int cmp;
    int from, to;
    char ok;
    scanf("%d%d",&n,&d);
    for(int i = 1; i <= n; i++)
        p[i].par = i;
    memset(opr,false,sizeof(opr));
    for(int i = 1; i <= n; i++)
        scanf("%d%d",&p[i].x,&p[i].y);
    while(scanf("\n%c",&ok) != EOF)//  \n接受回车
    {
        if(ok == 'O')
        {
            scanf("%d",&cmp);
            opr[cmp] = true;
            for(int i = 1; i <= n; i++)
                if(opr[i] && i != cmp)
                hebing(p[i],p[cmp]);
        }
        else
        {
            scanf("%d%d",&from,&to);
            if(fond(from) == fond(to))
                cout<<"SUCCESS"<<endl;
            else
                cout<<"FAIL"<<endl;

        }
    }

    return 0;
}


0 0
原创粉丝点击