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

2021-04-24 17:27

阅读:385

标签: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 1. "O p" (1 2. "S p q" (1
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  2 #include  3 #include  4 #include  5 #include  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 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 i){
37                 if (i == tx || !vis[i]) continue;
38                 double dist = dis(x[i],y[i],x[tx],y[tx]);
39                 if (dist 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


评论


亲,登录后才可以留言!