Wireless Network (并查集)

来源:互联网 发布:怎样下载打字软件 编辑:程序博客网 时间:2024/06/06 21:07
Problem 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: <br>1. "O p" (1 <= p <= N), which means repairing computer p. <br>2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate. <br> <br>The input will not exceed 300000 lines. <br>
 

Output
For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
 

Sample Input
4 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4
 

Sample Output
FAILSUCCESS
 
题意:电脑的网络全崩溃了,n   d代表,n台电脑,d是距离,给出电脑i的坐标,O代表repair修复电脑,如果修复的电脑周围d距离范围内也有,修复好的电脑,那么这两个电脑连接到同一个网络中,如果这个电脑已经与其他电脑已经连成一个网络,那么这个电脑(网络)将与这个网络连成一个大的网络。S是查询,两个电脑是否在同一个网络。
思路:普通并查集加一个for对所有求距离。
#include <iostream>#include <algorithm>#include <string.h>#include <stdio.h>#include <map>#include <cmath>#include <queue>using namespace std;const int maxn = 1005;struct Node {    int x, y;    int pre;}node[maxn];int repair[maxn];double distance(const Node& n1, const Node& n2) {    return sqrt((n1.x - n2.x) * (n1.x - n2.x) + (n1.y - n2.y) * (n1.y - n2.y));}int find(int x) {    if (x == node[x].pre)        return x;    node[x].pre = find(node[x].pre);    return node[x].pre;}void merge(int x, int y) {    int fx = find(x);    int fy = find(y);    if (fx != fy) {        node[fx].pre = fy;    }}int main() {        //freopen("in.txt", "r", stdin);    int n, d;    cin >> n >> d;    for (int i = 1; i <= n; ++i) {        cin >> node[i].x >> node[i].y;        node[i].pre = i;        repair[i] = 0;//初始状态,未修复    }    char ch[2];    while (cin >> ch) {        if (ch[0] == 'O') {            int a;            cin >> a;            repair[a] = 1;            for (int i = 1; i <= n; ++i) {                if (repair[i] && ::distance(node[a], node[i]) <= d) {                    merge(a, i);                }            }        } else {            int a, b;            cin >> a >> b;            int fa = find(a);            int fb = find(b);            if (fa == fb) {                cout << "SUCCESS" << endl;            } else                cout << "FAIL" << endl;        }    }}