A - Wireless Network POJ - 2236-kuangbin带你飞

A - Wireless Network

POJ - 2236

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 50348   Accepted: 20619

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

[Submit]   [Go Back]   [Status]   [Discuss]

题意

   有n个坏掉的卫星,每次操作‘O’可以修好一个,如果卫星之间都是‘修好的’状态且间距小于‘d‘,就可以通讯。

思路

   并查集的时候判断一下两卫星之间距离即可。

CODE

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <iostream>
 6
 7 #define dbg(x) cout << #x << "=" << x << endl
 8
 9 using namespace std;
10 const int maxn = 1007;
11
12 int fa[maxn], dis[maxn];
13 int n,m,ans,cnt;
14 int d;
15 int canuse[maxn];
16
17 struct node {
18     int x, y;
19 }a[maxn];
20
21 void init()
22 {
23     for(int i = 1; i <= n; i++) {
24         fa[i] = i;
25     }
26 }
27
28 double cal(node a, node b) {
29     return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y));
30 }
31
32 int fid(int x)
33 {
34     int r = x;
35     while(fa[r] != r) {
36         //if(cal(a[fa[r]],a[r]) <= d)
37             r = fa[r];
38     }
39     int i,j;///路径压缩
40     i = x;
41     while(fa[i] != r) {
42         //if(cal(a[fa[i]],a[r]) <= d) {
43             j = fa[i];
44             fa[i] =  r;
45             i = j;
46
47     }
48     return r;
49 }
50
51 void join(int r1, int r2)///合并
52 {
53     int fidroot1 = fid(r1), fidroot2 = fid(r2);
54     if(fidroot1 != fidroot2) {
55         fa[fidroot2] = fidroot1;
56     }
57 }
58
59 int main()
60 {
61     scanf("%d %d",&n, &d);
62     init();
63     for(int i = 1; i <= n; ++i) {
64         scanf("%d %d",&a[i].x, &a[i].y);
65     }
66     getchar();
67     char ch[2];
68     int p,q;
69     int cnt = 1;
70     while(scanf("%s", ch) != EOF) {
71         //dbg(ch[0]);
72         if(ch[0] == ‘O‘) {
73             scanf("%d",&p);
74             //getchar();
75             canuse[cnt++] = p;
76             for(int i = 1; i < cnt-1; ++i) {
77                 if(cal(a[canuse[i]],a[p]) <= double(d)) {
78                     join(canuse[i],p);
79                 }
80             }
81         }
82         if(ch[0] == ‘S‘) {
83             scanf("%d %d",&p,&q);
84             //getchar();
85
86             //join(p,q);
87             if(fid(p) == fid(q)) {
88                 puts("SUCCESS");
89             }
90             else {
91                 puts("FAIL");
92             }
93         }
94     }
95     return 0;
96 }

原文地址:https://www.cnblogs.com/orangeko/p/12264155.html

时间: 2024-10-13 04:34:02

A - Wireless Network POJ - 2236-kuangbin带你飞的相关文章

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

(并查集) Wireless Network --POJ --2236

链接: http://poj.org/problem?id=2236 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#problem/A 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> #include<iostream>

Wireless Network POJ 2236

http://poj.org/problem?id=2236 题意:现有一些电脑(编号从 1 - N),在修理好某台电脑并且当这台电脑与其他电脑距离不超过 D 的情况下, 其他电脑可以由这台电脑控制.       有两种操作:一是修理编号为 x 的某台电脑, 而是询问你编号从 p - q 的电脑是否都彼此联通 #include <iostream> #include <cstdio> #include <cmath> #include <algorithm>

A - Wireless Network POJ - 2236

题目大意:有n台坏掉的电脑,给出每台电脑的坐标,然后每次询问输入0(字符) x,表示电脑x恢复正常,输入S x y 询问x和y是否可以联网.只要是x和y的距离小于距离d,那么就可以联网,如果有个中介c使得x到c的距离小于d,y到c的距离小于d,那么x和y也可以联网. 题解:当修复好一台电脑后,然后判断与之前修好的电脑的距离,小于d的话,用并查集连在一起.(没敢这样想,感觉这样会T....) #include<cstdio> #include<iostream> #include&l

Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two m

Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonal

POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y均能C通信,则x和y可以通信.现在给出若干个操作, O p 代表修复编号为p的电脑 S p q代表询问p和q是不是能通信. 思路: 并查集即可.. 如果修复了一台电脑,则把与它相连距离不超过d的且修复了的放在一个集合里面. #include<cstdio> #include<cstring&

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher :G - Power Strings POJ - 2406(kmp简单循环节)

[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher G - Power Strings POJ - 2406 题目: Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of

跟着chengyulala刷题之[kuangbin带你飞]之&#39;并查集&#39;专题/斜眼笑

[kuangbin带你飞] 专题1-23 https://vjudge.net/article/187 专题五 并查集 POJ 2236 Wireless Network  http://poj.org/problem?id=2236POJ 1611 The Suspects  http://poj.org/problem?id=1611HDU 1213 How Many Tables  http://acm.hdu.edu.cn/showproblem.php?pid=1213HDU 3038