poj 2236(并查集的应用)

Wireless Network

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 35832   Accepted: 14863

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
 1 #include<cstdio>
 2 #include<cmath>
 3 #include<cstring>
 4 using namespace std;
 5 const int maxn=1100;
 6 bool flag[maxn];
 7 int fa[maxn],x[maxn],y[maxn];
 8 int n,d;
 9
10 bool dis(int numa,int numb)
11 {
12     int opa,opb;
13     opa=x[numa]-x[numb];
14     opb=y[numa]-y[numb];
15     return opa*opa+opb*opb<=d*d;
16 }
17
18 int find_fa(int op)
19 {
20     if(op!=fa[op]) fa[op]=find_fa(fa[op]);
21     return fa[op]; ///此处有压缩
22 }
23
24 int main()
25 {
26     scanf("%d%d",&n,&d);
27     for(int i=1;i<=n;i++){
28         fa[i]=i;
29         flag[i]=false;
30     }
31     for(int i=1;i<=n;i++)
32         scanf("%d%d",&x[i],&y[i]);
33
34     char order[3];
35     int a,b;
36     while( ~scanf("%s%d",order,&a)){
37         if(order[0]==‘O‘){
38             flag[a]=true;
39             for(int i=1;i<=n;i++){
40                 if(i!=a&&flag[i]&&dis(i,a)){
41                     int iop,aop;
42                     iop=find_fa(i);
43                     aop=find_fa(a);
44                     fa[aop]=iop;
45                 }
46             }
47         }
48         else{
49             scanf("%d",&b);
50             int aop,bop;
51             aop=find_fa(a);
52             bop=find_fa(b);
53             if(aop==bop) printf("SUCCESS\n");
54             else printf("FAIL\n");
55         }
56     }
57     return 0;
58 }


原文地址:https://www.cnblogs.com/ZQUACM-875180305/p/9091732.html

时间: 2024-10-14 06:17:45

poj 2236(并查集的应用)的相关文章

Wireless Network (poj 2236 并查集)

Language: Default Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17602   Accepted: 7418 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network wit

poj 2236(并查集)

题意:有n个电脑坏了,编号从1到n,现在要修电脑且使电脑间能够通信,如果两台修好的电脑之间直接距离小于等于d就可以通信,如果电脑a和电脑c的距离大于d但a和c都可以与电脑b通信,那么a和c就可以通信了.O a,修好电脑a.S a b询问a和b是否能通信. 题解:每修好一台电脑,就过一遍所有电脑,把修好且能通信的电脑放到一个集合里,询问时只要判断是否在集合内就行了. #include <stdio.h> #include <math.h> const int N = 1005; st

POJ 2524 并查集

Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23580 Accepted: 11609 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding o

poj 2513 并查集,Trie(字典树), 欧拉路径

- Colored Sticks POJ - 2513 You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

POJ 2492 并查集扩展(判断同性恋问题)

G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2492 Appoint description: Description BackgroundProfessor Hopper is researching the sexual behavior of a rare species of bugs. H

poj 1417 并查集+dp

转自:点我 题目:给出p1+p2个人,其中p1个是好人,p2个是坏人.然后有一些关系 ,a说b是好人(坏人).其中没有矛盾的,判断是否有唯一解判断哪些人是好人,哪些人是坏人. 其中比较重要的是,好人总说真话,坏人总说假话.不需要判断矛盾.唯一解 http://poj.org/problem?id=1417 其中好人说真话,坏人说假话这点很重要. 那么如果一个人说另一个人是好人,那么如果这个人是好人,说明 对方确实是好人,如果这个是坏人,说明这句话是假的,对方也是坏人. 如果一个人说另一个人是坏人

POJ 2492 并查集应用的扩展

A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and

poj 1984 并查集

题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 1 /* 2 POJ 1984 3 并查集 4 */ 5 6 #include <stdio.h> 7 #include <string.h> 8 #include <iostream> 9 #include <algorithm> 10 #include <math.h> 11 using namespace std; 12

源哥每日一题第十八弹 poj 1182 并查集

题目链接:http://poj.org/problem?id=1182 题意:看不懂?退群吧 比平常的并查集加了一个判断集合间关系的操作: 开一个数组记录当前点所在集合的次序(第几个集合)用012表示 比较简单的思路,不过体现了并查集的精妙 #include <iostream> #include <cstring> #include <cstdio> using namespace std; int dad[500005]; int st[50005]; int fi

poj 2912 并查集(食物链加强版)

题目:给出n个人玩剪刀石头布的游戏,其中有一个人是裁判,剩下的人分为3组,每一组的人只出某一种手型,裁判可以任意出.问是否能判断出哪个人是裁判 链接:点我 分分钟看吐血,先把食物链看懂吧 枚举裁判,然后并查集判断 裁判由于可以任意出,所以可能属于任意一个集合,所以有裁判参与的会合不考虑,然后并查集部分和食物链很相似. 如果某个裁判那里出现了矛盾,则记录一下在哪出问题. 然后判断是否只有一个裁判没有出现问题.如果只有一个,说明可以确定,那么就是剩下的人出问题的最大值.因为只有否定了其它所有人,才能