POJ2236 Wireless Network 并查集 好题

本文介绍了一种在地震后修复无线网络的算法,重点在于如何判断两台计算机能否通过直接或间接方式通信。该算法涉及到距离计算、并查集等关键技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


题意:现在有n个电脑,按1开始编号,规定2个电脑可以直接通讯的条件:
   1.2个电脑都有维修过
   2.2个电脑的距离<=d
   当然,2个电脑也可以通过其他电脑间接联系

现在给出了n,d,
然后给出n台电脑的坐标(2维),然后再给出操作:
O a :维修电脑a
S a b :test电脑a和b是否可以连接

思路:
先算好dis[i][j] 表示i与j的距离(这个要先预处理好,不然等下会重复计算很多次,会tle)
O a 则vis[a]=true 然后遍历一遍,把vis[i]==true&&dis[a][i]<=d 的点i 和a 合并
S a b 若a和b的祖先节点相同,则SUCCESS,否则FAIL

学到了,先对数据进行预处理。

 1 #include<cstdio>
 2 #include<cmath>
 3 using namespace std;
 4 
 5 const int maxn=1005;
 6 double dis[maxn][maxn];
 7 int father[maxn];
 8 bool vis[maxn];
 9 int n,d;
10 struct Point
11 {
12     int x,y;
13 }point[maxn];
14 
15 void init_distance()
16 {
17     for(int i=1;i<=n;i++)
18         for(int j=1;j<=n;j++)
19          dis[i][j]=sqrt((long double)(point[i].x-point[j].x)
20 *(point[i].x-point[j].x)+(long double)(point[i].y-point[j].y)*(point[i].y-point[j].y));
21 }
22 
23 void init()
24 {
25     for(int i=1;i<=n;i++)
26     {
27         father[i]=i;
28         vis[i]=false;
29     }
30 }
31 
32 int find_set(int x)
33 {
34     if(father[x]==x)
35         return x;
36     else
37         return father[x]=find_set(father[x]);
38 }
39 
40 void union_set(int x,int y)
41 {
42     int fax=find_set(x);
43     int fay=find_set(y);
44     if(fax==fay)
45         return ;
46     father[fax]=fay;
47 }
48 
49 int main()
50 {
51     scanf("%d%d",&n,&d);
52     for(int i=1;i<=n;i++)
53         scanf("%d%d",&point[i].x,&point[i].y);
54 
55     init();
56     init_distance();
57     char str[3];
58     int a,b;
59     while(scanf("%s",&str)!=EOF)
60     {
61         if(str[0]=='O')
62         {
63             scanf("%d",&a);
64             vis[a]=true;
65             for(int i=1;i<=n;i++)
66             {
67                 if(vis[i]&&dis[i][a]<=d)
68                 {
69                     union_set(i,a);
70                 }
71             }
72         }
73         else{
74             scanf("%d%d",&a,&b);
75             if(find_set(a)==find_set(b))
76                 printf("SUCCESS\n");
77             else
78                 printf("FAIL\n");
79         }
80     }
81     return 0;
82 }
1172ms

 













  


转载于:https://www.cnblogs.com/-maybe/p/4435945.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值