poj2236(并查集水题)

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

一开始的计算机都不能用,每台计算机只能与距离不超过d米的计算机通信,但是可以通过中间的计算机来通信。现在开始修复计算机,工人们可以选择修复一台计算机,或者测试两台计算机能不能通信,测试时可以就输出success,不然就fail。
第一行包含两个整数N和d (1 <= N <= 1001, 0 <= d <= 20000)。这里N是计算机的数量,从1到N, D是两台计算机可以直接通信的最大距离。在接下来的N行中,每一行包含两个整数xi, yi (0 <= xi, yi <= 10000),这是N台计算机的坐标。接下来的每一行包含以下两种格式之一的操作:

1. “O p”(1 <= p <= N),表示修复计算机p。

2. “S p q”(1 <= p, q <= N),表示测试计算机p与q是否能够通信。

就是个简单的并查集,先设置个数组来说明每个计算机有没有修好,一开始都是0,然后每修复一个就让他变成1,然后从第一台计算机开始循环,看看有没有可以合成一个集合的。然后判断时就判断那两个计算机是否具有同一个老大,有就输出success。

 1 #include
 2 #include<string.h>
 3 #include
 4 #include
 5 #include
 6 using namespace std;
 7 int d,n,m;
 8 int b,c;
 9 char a;
10 int f[1005];
11 int x[1005],y[1005];
12 bool flag [1005];
13 void init(){
14     for(int i = 1 ; i <= n ; i++){
15         f[i] = i;
16     }
17 }
18 int getf(int i){
19     if( f[i] == i )
20         return f[i];
21     else{
22         f[i] = getf(f[i]);
23         return f[i];
24     }
25 }
26 void merge(int u,int v){
27     int t1 = getf(u);
28     int t2 = getf(v);
29     if(t1 != t2){
30         f[t2] = t1;
31     }
32 }
33 void judge(int a,int b){
34     int t1 = getf(a);
35     int t2 = getf(b);
36     if(t1 == t2)
37         cout<<"SUCCESS"<<endl;
38     else
39         cout<<"FAIL"<<endl;
40 }
41 int main(){
42     while(cin>>n>>d){
43         memset(f,0,sizeof(f));
44         memset(x,0,sizeof(x));
45         memset(y,0,sizeof(y));
46         memset(flag,0,sizeof(flag));
47         init();
48         for(int i = 1 ; i <= n; i++){
49             cin>>x[i]>>y[i];
50         }
51         while(cin>> a){
52             if(a == 'O'){
53                 cin>>b;
54                 flag[b] = 1;
55                 for(int i = 1 ; i <= n ; i++){
56                     if(flag[i] == 0)
57                         continue;
58                     double p = pow((x[i]-x[b])*(x[i]-x[b])+(y[i]-y[b])*(y[i]-y[b]),0.5);
59                     if(p <= d)
60                         merge(i,b);
61                 }
62             }
63             if(a == 'S'){
64                 cin>>b>>c;
65                 judge(b,c);
66             }
67         }
68     }
69     return 0;
70 }

 

转载于:https://www.cnblogs.com/-Leo/p/11239895.html

你可能感兴趣的:(poj2236(并查集水题))