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带你学习算法,不管是编程算法,还是深度学习、自然语言处理算法都一网打尽,更有各种计算机新鲜知识和你分享。别急,算法码上来。

全部评论

相关推荐

05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
屌丝逆袭咸鱼计划:心态摆好,man,晚点找早点找到最后都是为了提升自己好进正职,努力提升自己才是最关键的😤难道说现在找不到找的太晚了就炸了可以鸡鸡了吗😤早实习晚实习不都是为了以后多积累,大四学长有的秋招进的也不妨碍有的春招进,人生就这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务