并查集-E - Wireless Network

E - Wireless Network

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


 1 #include<iostream>
 2 #include<cmath>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 int x[1005], y[1005];//存放电脑的横纵坐标 
 8 int dis[1005][1005];//存放电脑i和电脑j之间的距离 
 9 int parent[1005];//并查集的关系联系数组 
10 int n, d, a, b;
11 int is[1005];//判断电脑是否已修复 
12 char ask[2];
13 
14 void init(int n){//联系数组初始化 
15     for(int i=1; i<=n; i++)
16         parent[i] = i;
17 }
18 
19 int find(int x){
20     if(x == parent[x])
21         return x;
22     return parent[x] = find(parent[x]);
23 }
24 
25 void unionit(int a, int b){
26     int fa = find(a);
27     int fb = find(b);
28     parent[fb] = fa;
29 }
30 
31 int main(){
32     scanf("%d %d", &n, &d);
33     for(int i=1; i<=n; i++){
34         scanf("%d %d",x+i, y+i);
35         is[i] = 0;
36     }    
37     for(int i=1; i<=n; i++)//求两电脑间距离 
38         for(int j=i+1; j<=n; j++)
39             dis[i][j] = dis[j][i] = (x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]);//此处求出的距离未开根 
40     
41     init(n);
42     while(~scanf("%s",ask)){
43         if(ask[0] == 'O'){
44             scanf("%d",&a);
45             if(is[a])    continue;//若电脑已修复,则没必要进行下面操作 
46             is[a] = 1;
47             for(int i=1; i<=n; i++)//逐一遍历,找到在可连接范围内的电脑并与之联系 
48                 if(is[i] && dis[i][a] <= d*d)
49                     unionit(i, a);
50         }else{
51             scanf("%d %d",&a, &b);
52             if(find(a) == find(b))//判断两台电脑是否可相互联系 
53                 printf("SUCCESS\n");
54             else
55                 printf("FAIL\n");
56         }
57     }
58     return 0;
59 }

 



上一篇:[北大机试F]:Wireless Network(并查集)


下一篇:745-你有一桶果冻,其中有黄色,绿色,红色三种,闭上眼睛,抓取两个同种颜色的果冻