码迷,mamicode.com
首页 > Web开发 > 详细

poj2236(Wireless Network)并查集

时间:2015-08-18 19:37:41      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:acm   并查集   

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

题意:给你n个坏的电脑的坐标,如果两个修好的电脑距离不大于d,他们之间连通。两台电脑可以通过第三台电脑间接连通。给出若干次询问,一种是修哪台电脑,一种是询问两台电脑是否连通。


题解:典型的并查集,合并时我也没有加rank数组优化,可以自己加一下,不过10s时间绰绰有余了。


#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <limits>
#include <new>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;

const int maxn = 1010;

int f[maxn];
int px[maxn], py[maxn];
int vis[maxn];
int n, d;

int Find(int x)
{
    return x == f[x] ? x : (f[x] = Find(f[x]));
}

void join(int x, int y)
{
    int fx = Find(x), fy = Find(y);
    f[fx] = fy;
}

int dis(int x, int y)
{
    return (px[x]-px[y])*(px[x]-px[y])+(py[x]-py[y])*(py[x]-py[y]);
}

int main()
{
    cin >> n >> d;
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d%d", &px[i], &py[i]);
        f[i] = i;
    }
    memset(vis, 0, sizeof(vis));
    char op[5];
    while (~scanf("%s", op))
        if (op[0] == 'O')
        {
            int p;
            scanf("%d", &p);
            vis[p] = 1;
            for (int i = 1; i <= n; ++i)
                if (i != p && vis[i] && dis(i, p) <= d*d)
                    join(i, p);
        }
        else
        {
            int x, y;
            scanf("%d%d", &x, &y);
            int fx = Find(x), fy = Find(y);
            if (fx == fy)
                printf("SUCCESS\n");
            else
                printf("FAIL\n");
        }
    return 0;
}


版权声明:本文为博主原创文章,转载请注明出处。

poj2236(Wireless Network)并查集

标签:acm   并查集   

原文地址:http://blog.csdn.net/god_weiyang/article/details/47753935

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!