POJ 2236 Wireless Network (并查集)

Wireless Network

题目链接:

http://acm.hust.edu.cn/vjudge/contest/123393#problem/A

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

题意:

给出n个点的坐标,一开始任意两点均不联通;
接着给出多个操作:

  1. 恢复点x的联通性,即x可与其他已恢复的点连接.
  2. 查询点x和y是否可达.
    (定义可达:距离小于d,或者经过多条小于d的边)

题解:

很明显的并查集模版题.
距离小于等于d即可合并;
查询时输出两点是否在同一集合.
(不要把FAIL输出成FALL).

注意:先将与点i距离不超过d的点存起来;
当恢复点i后,枚举可连接的点,只有两点都被恢复时才能合并.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 1200
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int fa[maxn];
int rank[maxn];

void init_set() {
    for(int i=0; i<maxn; i++) {
        fa[i] = i;
        rank[i] = 0;
    }
}

int find_set(int x) {
    return fa[x] = (x==fa[x]? x:find_set(fa[x]));
}

void unit_set(int x, int y) {
    x = find_set(x);
    y = find_set(y);
    if(rank[x] < rank[y]) swap(x, y);
    fa[y] = x;
    if(rank[x] == rank[y]) rank[x]++;
}

LL D;
bool dis[maxn][maxn];
LL x[maxn],y[maxn];
bool vis[maxn];

int main(int argc, char const *argv[])
{
    //IN;

    int n;
    while(scanf("%d %lld", &n,&D) != EOF)
    {
        D = D*D;
        init_set();
        memset(dis, 0, sizeof(dis));
        memset(vis, 0, sizeof(vis));
        for(int i=1; i<=n; i++) scanf("%lld %lld", &x[i],&y[i]);
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=n; j++) {
                if((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]) <= D) {
                    dis[i][j] = dis[j][i] = 1;
                }
            }
        }

        char c; int x,y;
        while(scanf("%c",&c) != EOF) {
            if(c=='O') {
                scanf("%d", &x);
                vis[x] = 1;
                for(int i=1; i<=n; i++) if(dis[x][i] && vis[i])
                    unit_set(i, x);
            }
            else if(c=='S') {
                scanf("%d %d", &x,&y);
                if(find_set(x) == find_set(y)) puts("SUCCESS");
                else puts("FAIL");
            }
        }
    }

    return 0;
}

原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5699043.html