POJ2236 Wireless Network

 

 

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 23293   Accepted: 9747

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 /**/
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<algorithm>
 7 using namespace std;
 8 const int mxn=5000;
 9 int x[mxn],y[mxn];
10 bool able[mxn];
11 int n,D;
12 double ans=0;
13 //
14 int fa[mxn],tot=0;
15 int find(int x){
16     if(fa[x]==x)return x;
17     return fa[x]=find(fa[x]);
18 }
19 //
20 double dis(const int a,const int b){
21     return sqrt((double)(x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
22 }
23 int main(){
24     scanf("%d%d",&n,&D);
25     int i,j;
26     for(i=1;i<=n;i++)scanf("%d%d",&x[i],&y[i]),fa[i]=i;
27     char ch[5];int op;
28     while(scanf("%s",ch)!=EOF){
29         if(ch[0]=='O'){
30             scanf("%d",&op);
31             able[op]=1;
32             int v=find(op);
33             for(i=1;i<=n;i++)
34              if(able[i] && dis(i,op)<=D && i!=op){
35                  int u=find(i);
36                  if(u!=v)fa[u]=v;
37              }
38         }
39         else if(ch[0]=='S'){
40             int u,v;
41             scanf("%d%d",&u,&v);
42             u=find(u);
43             v=find(v);
44             if(u==v) printf("SUCCESS\n");
45             else printf("FAIL\n");
46         }
47     }
48     return 0;
49 }

 

posted @ 2016-07-27 18:55  SilverNebula  阅读(185)  评论(0编辑  收藏  举报
AmazingCounters.com