poj2236(Wireless Network)并查集

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;
}


算法码上来 文章被收录于专栏

公众号「算法码上来」。godweiyang带你学习算法,不管是编程算法,还是深度学习、自然语言处理算法都一网打尽,更有各种计算机新鲜知识和你分享。别急,算法码上来。

全部评论

相关推荐

牛油们,这个号码是哪个啊,漏接了,gg
牛客251106521号:破案了,上海智元
点赞 评论 收藏
分享
牛客915519934号:差不多得了 ,真以为我们好忽悠呢?当初就是听了你们的话没有赶上风口入行Java,现在还想再忽悠我呢?这明显就是一个新风口,国家大力发展制造业,以后这个圈子的钱只会越来越多,不管是入门还是大佬,只要进来少说有你一口饭吃,一个个自私自利自己上了车就劝退其他人,钱都让你赚得了呗。就这点东西,入门很容易的,学个pcb,单片机就可以去找工作了,少说一万五起,以后只会越来越高,以后想进阶就去FPGA,linux,给的钱吊打互联网,再说说你们一直说数电模电难?实际呢也不过一个月就能拿下的事情,你不需要学的多深,只需要入门就足够了,就按我说的学出来少说两万起,最好报个培训班,入门更快,兄弟们跟着我冲就完事了,趁着这个机会,狠狠赚他一笔。
点赞 评论 收藏
分享
有趣的牛油果开挂了:最近这个阶段收到些杂七杂八的短信是真的烦
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务