1. 程式人生 > >poj 2236【並查集】

poj 2236【並查集】

proc cau pos exp con computer from size ble

poj 2236

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

題意:有一堆壞電腦,每個電腦都有一個二維坐標。有兩種操作,修一臺電腦和在線詢問兩臺電腦是否可以連通。若兩臺電腦相距小於等於d或者可以經過第三方電腦連通則連通。給出詢問操作的答案。
題解:顯然並查集。因為修和詢問都是實時在線操作,所以修完一臺就看看有沒有能和它相連的。
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 const int maxn = 1060;
 8 
 9 int dx[maxn], dy[maxn], Rank[maxn], par[maxn], re[maxn];
10 double dis[maxn][maxn];
11 int N;
12 double D;
13 
14 void init()
15 {
16     for (int i = 1; i <= N; i++) {
17         Rank[i] = 0; par[i] = i;
18     }
19 }
20 
21 int find(int x)
22 {
23     if (par[x] == x) return x;
24     else return find(par[x]);
25 }
26 
27 void unite(int xx, int yy)
28 {
29     int x = find(xx); int y = find(yy);
30     if (x == y) return;
31     if (Rank[x] < Rank[y]) par[x] = y;
32     else {
33         par[y] = x;
34         if (Rank[x] == Rank[y]) Rank[x]++;
35     }
36 }
37 
38 bool same(int x, int y)
39 {
40     return find(x) == find(y);
41 }
42 
43 
44 int main()
45 {
46     cin >> N;
47     cin >> D;
48     init();
49     for (int i = 1; i <= N; i++) cin >> dx[i] >> dy[i];
50     for(int i=1;i<=N;i++)
51         for (int j = i + 1; j <= N; j++) {
52             dis[i][j] = dis[j][i] = sqrt((double)(dx[i] - dx[j])*(dx[i] - dx[j]) + (double)(dy[i] - dy[j])*(dy[i] - dy[j]));
53         }
54     char op; int p, q;
55     int cnt = 0;
56     while (cin>>op)
57     {
58         if (op == O) {
59             cin >> p;
60             re[cnt++] = p;
61             for (int i = 0; i < cnt - 1; i++)
62                 if (dis[re[i]][p] <= D)
63                     unite(re[i], p);
64         }
65         else
66         {
67             cin >> p >> q;
68             if (same(p, q))
69                 cout << "SUCCESS" << endl;
70             else cout << "FAIL" << endl;
71         }
72     }
73     return 0;
74 }

poj 2236【並查集】