码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2020-01-25 20:46:01      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:RoCE   cal   proc   following   ack   台电脑   att   rsh   with   

总时间限制: 
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 }

 

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

标签:RoCE   cal   proc   following   ack   台电脑   att   rsh   with   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!