[POJ - 2236] Wireless Network (disjoint-set)

Wireless Network

This is then translated

Descriptions

The earthquake in Southeast Asia. ACM (Asia cooperative medical team) has established a wireless network and laptops, but an unexpected aftershock hit, all the computers on the network have been broken. One by one computer repair, network gradually began to work. Due to hardware limitations, each computer can only communicate directly with the computer it is not far away. However, each computer can be regarded as a communication intermediary between two other computers, that is, if computer A and computer B may communicate directly or may communicate with the computer A C and A, then the computer A and computer B may communicate. B. 

In the process of repairing the network, staff can carry out both operations at any time, whether computer repair or test the two computers can communicate. Your job is to answer all the test operations. 

Input

The first line contains two integers N and d (1 <= N <= 1001,0 <= d <= 20000). Where N is the number of computers, numbered from 1 to N, D is the maximum distance of the two computers can communicate directly. In the next N rows, each row comprising two integers xi, N computers coordinate yi (0 <= xi, yi <= 10000), that is. From the (N + 1) line to the input end, operations performed individually. Each row contains operation of one of two formats: 
1. "O p" (1 <= p <= N), computer repair represents p. 
2. "S pq" (1 < = p, q <= N), whether it means that the test computer p and q may communicate. 

Enter not more than 300,000 lines. 

Output

For each test operation, if two computers can communicate with the print "SUCCESS", otherwise print "FAIL".

Sample Output

4 1
0 1
0 2
0 3
0 4
1
O 2
O 4
S 1 4
O 3
S 1 4

Sample Output

FAIL
SUCCESS

Topic Link

https://vjudge.net/problem/POJ-2236

 

Every fix a computer when he traversed it all repaired computer, to see whether the distance <= d, if the explanation can be connected in line with the set of merger where two computers.
Every check the time to determine what if both computers to the same collection.
The computer then coordinates are given number n + 1 is. Such as O 3, he actually repair is No. 2 computer because the computer is numbered starting from 0
 
Not disjoint-set can look at this
 
AC Code
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#defineLong Long LL
 #define INF 0x3f3f3f3f
 #define MEM (X, Y) Memset (X, Y, the sizeof (X))
 #define MAXN. 5 100000 +
 the using  namespace STD;
 int n-, D;
 int DX [MAXN]; // coordinate 
int Dy [MAXN];
 int PAR [MAXN]; // PAR [i] i in root 
int pairs [MAXN]; // pairs [i] = P i-th computer repaired number P 
int findr ( int X) // query root 
{
     IF (PAR [X] == X)
         return X;
     return PAR [X] = findr (PAR [X]); 
} 
void Unite ( int X, int Y) // merge 
{ 
    X = findr (X); 
    Y = findr (Y);
     IF (X == Y) // root canal without the same 
        return ; 
    PAR [X] = Y; // If all different, y incorporated in x, y is the root of x, y Similarly incorporated here x can be freely 
}
 Double DIS ( int a, int B) // request from 
{
     return sqrt (( Double ) ( (DX [A] -dx [B]) * (DX [A] -dx [B]) + (Dy [A] -dy [B]) * (Dy [A] - Dy [B]))); 
} 
int main () 
{ 
    MEM (pairs, 0);
    cin>>n>>d;
    for(int i=0; i<n; i++)
        cin>>dx[i]>>dy[i];//集合初始化
    for(int i=0; i<n; i++)
        par[i]=i;
    int p,q,len=0;
    char op;
    while(cin>>op)
    {
        if(op=='O')
        {
            cin>>p;
            P - ;
            pairs [len ++] = P;
             for ( int I = 0 ; I <len; I ++ ) 
            { 
                IF (DIS (pairs [I], P) <= ( Double ) D) // determine the distance 
                    Unite ( pairs [I], P); // combined set 
            } 
        } 
        the else 
        { 
            CIN >> >> P Q; 
            P -, q - ;
             IF (findr (P) == findr (Q)) // check the root 
                cout << " SUCCESS " << endl;
            else
                cout<<"FAIL"<<endl;
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/sky-stars/p/11332511.html