POJ - 2236 - Wireless Network(并查集)

题目链接:https://vjudge.net/problem/POJ-2236

题目大意:有一个计算机网络的所有线路都坏了,网络中有n台计算机,现在你可以做两种操作,修理(O)和检测两台计算机是否连通(S),只有修理好的计算机才能连通。连通有个规则,两台计算机的距离不能超过给定的最大距离D(一开始会给你n台计算机的坐标)。检测的时候输出两台计算机是否能连通。

只要每次修好一台计算机的同时把周围距离小于等于d的计算机加入集合就行

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) fill(a, a+maxn, INF);
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, int> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 1e3+10;
int p[maxn], vis[maxn];
P cp[maxn];
int __find(int root) {
    int son = root, temp;
    while(root != p[root])
        root = p[root];
    while(son != root) {
        temp = p[son];
        p[son] = root;
        son = temp;
    } 
    return root;
}
void merge(int son1, int son2) {
    p[__find(son1)] = p[__find(son2)];
}
int main(void) {
    IOS;
    int n, d;
    while(cin >> n >> d) {
        zero(vis);
        for (int i = 0; i<n; ++i)
            p[i] = i;
        for (int i = 0; i<n; ++i)
            cin >> cp[i].first >> cp[i].second;
        string s;
        while(cin >> s) {
            if (s[0] == 'O') {
                int pos;
                cin >> pos;
                vis[pos-1] = true;
                for (int i = 0; i<n; ++i)
                    if (i != pos-1 && vis[i] && (cp[i].first-cp[pos-1].first)*(cp[i].first-cp[pos-1].first) + (cp[i].second-cp[pos-1].second)*(cp[i].second-cp[pos-1].second) <= d*d)
                        merge(i, pos-1);
            }
            else if (s[0] == 'S') {
                int a, b;
                cin >> a >> b;
                if (__find(a-1) == __find(b-1))
                    printf("SUCCESS\n");
                else 
                    printf("FAIL\n");
            }
        }
    }
    return 0;
}

 

posted @ 2020-02-29 19:04  shuitiangong  阅读(134)  评论(0编辑  收藏  举报