poj 2236 Wireless Network 并查集

Wireless Network
Time Limit: 10000MS
Memory Limit: 65536K
Total Submissions: 20499
Accepted: 8608

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

1.字符串的输入:1.1    使用%s代替%c,,如果单个的话取m[0]
                               1.2   使用"\n"代替getchar(),来读取空格
2.并查集:            2.1   访问祖先节点使用find()代替node[i].par!!!!
3.语法知识:        3.1  a^2代表的是a与2进行异火运算而不是a的平方,,要平方只能a*a
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Node{
  int x,y,par;
}node[1005];
int init(int n)
{
    for(int i=1;i<=n;i++)
        node[i].par=-1;
}
int find(int n)
{
    if(node[n].par!=n)
        node[n].par=find(node[n].par);
    return node[n].par;
}
int unit(int x,int y)
{
    int rx=find(x);
    int ry=find(y);
    if(rx!=ry)
        node[ry].par=rx;
    return 0;
}
int main()
{
    int n,d;
    cin>>n>>d;
    init(n);
    for(int i=1;i<=n;i++)
        cin>>node[i].x>>node[i].y;
    char m[3];//1.1
    int k,s,e,q;
    while(scanf("\n%s",m)!=EOF)//1.2
    {
        if(m[0]=='O')
           {
                cin>>k;
                node[k].par=k;
                for(int i=1;i<=n;i++)
                    if(node[i].par!=-1)
                     if((node[i].x-node[k].x)*(node[i].x-node[k].x)+
                        (node[i].y-node[k].y)*(node[i].y-node[k].y)<=d*d) //3.1
                        unit(i,k);
           }
        else if(m[0]=='S')
           {
                cin>>s>>e;
                if(node[s].par!=-1&&node[e].par!=-1&&
                   find(node[s].par)==find(node[e].par))//2.1
                     {
                         cout<<"SUCCESS"<<endl;
                         continue;
                     }
                    else
                    {
                         cout<<"FAIL"<<endl;
                         continue;
                    }
                }
    }
    return 0;
}


<span style="color:#3333ff;"></span>

posted @ 2015-12-24 15:59  快点说我帅  阅读(142)  评论(0编辑  收藏  举报