原题链接:http://poj.org/problem?id=2236

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 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

Source

POJ Monthly,HQM

题意:地震了,电脑都坏了不工作了,现在要修复这些电脑,和询问这些电脑是否可以通信(如果距离不大于D就可以通信,超过D则不行)先输入N,D;N表示有多少台电脑,D表示距离;然后N行输入这些电脑所在的X,Y坐标;下面有一些询问或者修复操作以EOF结尾(在这里错了好几次);

1)如果第一个字母为‘S’则后边跟两个数字p,q表示询问p和q之间能不能通信,能通信输出SUCCESS,不能输出FAIL;

2)如果第一个字母为‘O’则后边跟一个数字p,表示修复p这台电脑;(一开是没看懂题,因为把这个O跟0(零)看成一样的)

思路:并查集;用结构体存这些电脑信息,X,Y坐标;输入的时候初让每个电脑的父亲节点等于自己;vis数组表示是否修好;check函数判断两个电脑是否满足要求(距离&&好坏)。

如果为S的话直接判断p,q的父亲节点是否一样即可(Find);如果为‘O’的话vis数组标记为1表示修好,把这个电脑跟所有的电脑进行合并操作,能合并的条件为两台电脑都被修复且距离不超过d(check)。注意操作的输入是以文件结束结尾的。(在这里我就wa了一发)

ac代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#define N  1010
#define mm(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=a;i<=n;i++)
#define RE(i,a) for(int i=a;i>=0;i--)
using namespace std;
int pre[N],n,d;
bool vis[N];
struct node
{
    int x,y;
} ans[N];
void init()
{
    for(int i=1; i<=n; i++)
        pre[i]=i;
}
int Find(int x)
{
    return pre[x]==x?x:x=Find(pre[x]);
}
void join(int x,int y)
{
    int fx=Find(x),fy=Find(y);
    if(fx!=fy)
        pre[fx]=fy;
}
int check(int x1,int y1,int x2,int y2)
{
    if(d*d>=((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)))
        return 1;
    return 0;
}
int main()
{
//    freopen("C:\\Users\\nuoyanli\\Desktop\\acminput.txt","r",stdin);
//    freopen("C:\\Users\\nuoyanli\\Desktop\\acmoutput.txt","w",stdout);
    cin>>n>>d;
    init();
    rep(i,1,n)
    cin>>ans[i].x>>ans[i].y;
    char ss[10];
    while(cin>>ss)
    {
        int x,y;
        if(ss[0]=='O')
        {
            cin>>x;
            vis[x]=1;
            rep(i,1,n)
            if(vis[i]&&check(ans[i].x,ans[i].y,ans[x].x,ans[x].y)&&x!=i)
                join(i,x);
        }
        else
        {
            cin>>x>>y;
            if(Find(x)==Find(y))
                cout<<"SUCCESS\n";
            else
                cout<<"FAIL\n";
        }
    }
    return 0;
}

希望读者有所帮助。