[北大机试F]:Wireless Network(并查集)

总时间限制: 
10000ms

内存限制: 
65536kB
描述
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.

输入
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.

输出
For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
样例输入
4 1
0 1
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4
样例输出
FAIL
SUCCESS

并查集,维修每台电脑时,暴力遍历查找与该电脑距离小于d的已维修电脑,将两者Union。查询时只需查看两台电脑是否在同一并查集中即可。
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cmath>
 4 #include <cstdlib>
 5 #include <cstring>
 6 using namespace std;
 7 int n,d;
 8 int x[1005],y[1005],vis[1005];
 9 int fa[1005];
10 double dis(int xa,int ya,int xb,int yb){
11     return sqrt((xa-xb)*(xa-xb) + (ya-yb)*(ya-yb));
12 }
13
14 int getfa(int x){
15     return ((x == fa[x]) ? x : fa[x] = getfa(fa[x]));
16 }
17
18 void Union(int x,int y){
19     int tx = getfa(x);
20     int ty = getfa(y);
21     fa[tx] = ty;
22 }
23
24 int main(){
25     scanf("%d%d",&n,&d);
26     for (int i = 1;i <= n;++i){
27         scanf("%d%d\n",x+i,y+i);
28         fa[i] = i;
29     }
30     memset(vis,0,sizeof(vis));
31     char q;
32     int tx,ty;
33     while(~scanf("%c %d",&q,&tx)){
34         if (q == ‘O‘){
35             vis[tx] = 1;
36             for (int i = 1;i <= n;++i){
37                 if (i == tx || !vis[i]) continue;
38                 double dist = dis(x[i],y[i],x[tx],y[tx]);
39                 if (dist < d || fabs(dist - d) < 1e-12)
40                     Union(i,tx);
41             }
42         }else{
43             scanf("%d",&ty);
44             if (getfa(tx) == getfa(ty)){
45                 puts("SUCCESS");
46             }else puts("FAIL");
47
48         }
49         getchar();
50     }
51     return 0;
52 }

原文地址:https://www.cnblogs.com/mizersy/p/12233316.html

时间: 2024-11-13 10:14:30

[北大机试F]:Wireless Network(并查集)的相关文章

北大ACM2236——Wireless Network~~并查集

这一题,题目的大概意思是:有N台电脑,彼此直接能通信的最大距离为T,这些电脑因为地震损坏了,需要修理.修理之后,就可以跟其他的修理过的距离小于等于T的电脑通信,你需要回答的是某两台电脑是否能够通信. 简单的并查集的应用,只是加了一个限制条件. 输入N和T,下面N行为N个电脑的坐标. 再下面的是一直到文件结尾,输入O k,表示k电脑进行修理.输入S i j ,表示问你这两台电脑是否能够通信. 我用一个dis数组来存每两台电脑之间的距离,方便后面的判断. 下面是AC的代码,有详细的注释: #incl

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

Wireless Network 并查集

Wireless Network 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 compu

POJ 2236 Wireless Network (并查集)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 7618 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

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 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

poj 2236 Wireless Network (并查集)

链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 联通的条件: 两台都修复好了,求距离小于d 思路: 数据很小,我们可以没修复一台就直接枚举已经修复的计算机找到距离小于d的,放到一个并查集里,查询的话直接查询是否再同一个并查集里就好了 实现代码: //while(scanf("\n%c", &ope) != EOF) #inclu

[LA] 3027 - Corporative Network [并查集]

A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the services, the

测试赛F - Dragon Balls(并查集)

F - Dragon Balls Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to