POJ2236 Wireless Network

並查集

#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <map>
#include <queue>
#include <string.h>
#include <stack>

using namespace std;
const int MAX_N = 1010;
int par[MAX_N], height[MAX_N], c_ok[MAX_N];
int N, d;
int len = 0;

struct computer {
    int x, y;
};

computer c[MAX_N];

void init(int n) {
    for (int i = 0; i < n; i++) {
        par[i] = i;
        height[i] = 0;
    }
}

int find(int x) {
    if (par[x] == x) {
        return x;
    } else
        return find(par[x]);
}

void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
        return;
    if (height[x] < height[y])
        par[x] = y;
    else if (height[x] > height[y])
        par[y] = x;
    else {
        par[y] = x;
        height[x]++;
    }
}

bool same(int x, int y) {
    return find(x) == find(y);
}

int judge_distance(computer a, computer b) {
    int dis = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
    if (dis > d * d) {
        return 0;
    } else
        return 1;
}

int test(int a, int b) {
    return same(a, b);
}

int fix(int x) {
    for (int i = 0; i < len; i++) {
        if (judge_distance(c[c_ok[i]], c[x])) {
            unite(c_ok[i], x);
        }
    }
    c_ok[len++] = x;
    return 0;
}


int main() {
    scanf("%d %d", &N, &d);
    init(N);
    for (int i = 0; i < N; i++)
        scanf("%d %d", &c[i].x, &c[i].y);
    char ch;
    while (scanf("%c", &ch) != EOF) {
        if (ch == 'O') {
            int num;
            scanf("%d", &num);
            num = num - 1;
            fix(num);
        } else if (ch == 'S') {
            int a, b;
            scanf("%d %d", &a, &b);
            a = a - 1;
            b = b - 1;
            if (test(a, b))
                cout << "SUCCESS" << endl;
            else
                cout << "FAIL" << endl;
        }
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章