标签:poj2236
| Time Limit: 10000MS | Memory Limit: 65536K | |
| Total Submissions: 16885 | Accepted: 7091 |
Description
Input
Output
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
Sample Output
FAIL SUCCESS
Source
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
#define maxn 1002
#define inf 0x3f3f3f3f
double G[maxn][maxn];
int N, D, pre[maxn];
struct Node {
int x, y;
} node[maxn];
bool opd[maxn];
int ufind(int k) {
int a = k, b;
while(pre[k]) k = pre[k];
while(a != k) {
b = pre[a];
pre[a] = k;
a = b;
}
return k;
}
bool same(int a, int b) {
return ufind(a) == ufind(b);
}
bool unite(int a, int b) {
a = ufind(a);
b = ufind(b);
if(a == b) return false;
pre[a] = b;
return true;
}
double calDist(int i, int j) {
double x = node[i].x - node[j].x;
double y = node[i].y - node[j].y;
return sqrt(x * x + y * y);
}
int main() {
// freopen("stdin.txt", "r", stdin);
int x, y, i, j;
char ch[2];
scanf("%d%d", &N, &D);
for(i = 1; i <= N; ++i) {
for(j = 1; j <= N; ++j)
G[i][j] = inf;
if(i == j) G[i][j] = 0.0;
}
for(i = 1; i <= N; ++i) {
scanf("%d%d", &x, &y);
node[i].x = x;
node[i].y = y;
for(j = 1; j < i; ++j) {
G[i][j] = G[j][i] = calDist(i, j);
}
}
while(scanf("%s%d", ch, &x) != EOF) {
if(*ch == 'O') {
opd[x] = true;
for(i = 1; i <= N; ++i)
if(opd[i] && G[x][i] <= D) unite(x, i);
}
else {
scanf("%d", &y);
if(same(x, y)) printf("SUCCESS\n");
else printf("FAIL\n");
}
}
return 0;
}POJ2236 Wireless Network 【并查集】
标签:poj2236
原文地址:http://blog.csdn.net/chang_mu/article/details/41673777