POJ 2236 Wireless Network (并查集)

时间:2023-03-08 21:40:40
POJ 2236 Wireless Network (并查集)
Wireless Network
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 18066   Accepted: 7618

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 今天学了并查集,并查集的模板题。
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
int FATHER[SIZE],RANK[SIZE];
int N,D;
pair<int,int> G[SIZE];
vector<int> OK;
double DIS[SIZE][SIZE]; void ini(int);
int find_father(int);
void unite(int,int);
bool same(int,int);
double dis(pair<int,int>,pair<int,int>);
int main(void)
{
int x,y;
char ch; while(scanf("%d%d",&N,&D) != EOF)
{
ini(N);
for(int i = ;i <= N;i ++)
scanf("%d%d",&G[i].first,&G[i].second);
for(int i = ;i <= N;i ++)
for(int j = ;j <= N;j ++)
DIS[i][j] = dis(G[i],G[j]); while(scanf(" %c",&ch) != EOF)
if(ch == 'O')
{
scanf("%d",&x);
for(int i = ;i < OK.size();i ++)
if(DIS[x][OK[i]] <= D)
unite(x,OK[i]);
OK.push_back(x);
}
else
{
scanf("%d%d",&x,&y);
printf("%s\n",same(x,y) ? "SUCCESS" : "FAIL");
}
} return ;
} void ini(int n)
{
for(int i = ;i <= n;i ++)
{
FATHER[i] = i;
RANK[i] = ;
}
} int find_father(int n)
{
if(FATHER[n] == n)
return n;
return FATHER[n] = find_father(FATHER[n]);
} void unite(int x,int y)
{
x = find_father(x);
y = find_father(y); if(x == y)
return ;
if(RANK[x] < RANK[y])
FATHER[x] = y;
else
{
FATHER[y] = x;
if(RANK[x] == RANK[y])
RANK[y] ++;
}
} bool same(int x,int y)
{
return find_father(x) == find_father(y);
} double dis(pair<int,int> a,pair<int,int> b)
{
return sqrt(pow(a.first - b.first,) + pow(a.second - b.second,));
}