POJ2236(KB5-A)

Wireless Network

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 28617   Accepted: 11842

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

Sample Output

FAIL
SUCCESS

Source

POJ Monthly,HQM

简单并查集

 1 //2017-07-17
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5
 6 using namespace std;
 7
 8 const int N = 1010;
 9 int n, d;
10 int fa[N], x[N], y[N];
11 bool isRepaired[N];
12
13 void init(){
14     for(int i = 0; i < N; i++){
15         fa[i] = i;
16         isRepaired[i] = false;
17     }
18 }
19
20 int getfa(int x){
21     if(fa[x] == x)return x;
22     return fa[x] = getfa(fa[x]);
23 }
24
25 void merge0(int a, int b){
26     int af = getfa(a);
27     int bf = getfa(b);
28     if(af != bf){
29         fa[bf] = af;
30     }
31 }
32
33 int main(){
34     char op[3];
35     int a, b;
36     while(scanf("%d%d", &n, &d) != EOF){
37         init();
38         for(int i = 1; i <= n; i++){
39             scanf("%d%d", &x[i], &y[i]);
40         }
41         getchar();
42         while(scanf("%s", op)!=EOF){
43             if(op[0] == ‘O‘){
44                 scanf("%d", &a);
45                 isRepaired[a] = true;
46                 for(int i = 1; i <= n; i++){
47                     if(isRepaired[i] && (x[i]-x[a])*(x[i]-x[a])+(y[i]-y[a])*(y[i]-y[a])<=d*d){
48                         merge0(i, a);
49                     }
50                 }
51             }else{
52                 scanf("%d%d", &a, &b);
53                 int af = getfa(a);
54                 int bf = getfa(b);
55                 if(af == bf) printf("SUCCESS\n");
56                 else printf("FAIL\n");
57             }
58         }
59     }
60
61     return 0;
62 }
时间: 2024-10-05 23:09:15

POJ2236(KB5-A)的相关文章

POJ2236 wireless network 【并查集水题】

前端开发whqet,csdn,王海庆,whqet,前端开发专家 今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁.结婚已经五年,但是也不可以偷懒,于是有了这个效果. 在线研究点这里,下载收藏点这里.程序猿and程序媛,大胆秀出你的爱吧. 利用html5 canvas实现动态的文字粒子效果,效果如下. OK,简单看看原理,首先我们需要在canvas里面实现描边文字,然后利用getImageData获得描边文字的像素矩阵,将粒子效果绑定在描边文章上. 整个效果如下. html文

POJ-2236(并查集)

Wireless NetWork POJ-2236 需要注意这里的树的深度需要初始化为0. 而且,find函数需要使用路径压缩,这里的unint合并函数也使用了优化(用一开始简单的合并过不了). #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<cmath> using namespac

poj-2236 Wireless Network (基础并查集)

http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由于硬件的限制,一台电脑和另一台电脑能够相连当他们之间的距离小于d,或者还有一台电脑当中介,分别与两台电脑相连. 在修复的过程中,工作者会有两种操作,修复电脑和询问电脑a和电脑b是否相连.当询问的时候输出答案. 因为输入数据很大,需要快速判断电脑a和电脑b相连,所以自然想到用并查集. 初始时候 全部电

POJ2236 Wireless Network 并查集 好题

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 broke

poj2236(并查集)

题目连接 题意:一张图上分布着n台坏了的电脑,并知道它们的坐标.两台修好的电脑如果距离<=d就可以联网,也可以通过其他修好的电脑间接相连.给出操作“O x”表示修好x,给出操作“S x y”,请你判断x和y在此时有没有连接上. 分析:把每次修好的电脑能到达的都扔进一个集合里,然后直接判断即可. #include <cstdio> #include <cstring> #include <string> #include <cmath> #include

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

并查集 poj2236

网址:http://poj.org/problem?id=2236 题意:有n台坏的电脑,如果每两台电脑的距离不能超过d,那么这两台电脑有联系,用字符串O 表示标记第x台电脑维修了,用S判断从X到y是否有联系... 题解:用并查集记录和查找每个点的父亲节点,每次输入的同时遍历该点和其他点是否有联系(即距离小于等于的)..... 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using name

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

poj2236

专心和细心真的很重要 #include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<ctype.h>#include<stdlib.h>#include<limits.h>#include<math.h>#include<queue>#include<stack>#include<v