poj 2236并查集

Posted ╰追憶似水年華ぃ╮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2236并查集相关的知识,希望对你有一定的参考价值。

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并查集的主要内容,如果未能解决你的问题,请参考以下文章

并查集——poj2236(带权并查集)

poj2236 (并查集)

poj2236(并查集)

POJ-2236(并查集)

POJ 2236Wireless Network [并查集]

POJ - 2236Wireless Network (并查集)