首页 > 代码库 > POJ2236 Wireless Network 【并查集】
POJ2236 Wireless Network 【并查集】
Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 16885 | Accepted: 7091 |
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.
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.
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
Sample Output
FAIL SUCCESS
Source
POJ Monthly,HQM
并查集简单题。#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 【并查集】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。