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 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   :维修电脑aS 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


  
时间: 2024-11-20 19:59:42

POJ2236 Wireless Network 并查集 好题的相关文章

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)并查集

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

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

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

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

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

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

POJ2236 wireless network 【并查集水题】

前端开发whqet,csdn,王海庆,whqet,前端开发专家 今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁.结婚已经五年,但是也不可以偷懒,于是有了这个效果. 在线研究点这里,下载收藏点这里.程序猿and程序媛,大胆秀出你的爱吧. 利用html5 canvas实现动态的文字粒子效果,效果如下. OK,简单看看原理,首先我们需要在canvas里面实现描边文字,然后利用getImageData获得描边文字的像素矩阵,将粒子效果绑定在描边文章上. 整个效果如下. html文

POJ2236 Wireless Network 【并查集】

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

G - Brain Network (easy)(并查集水题)

G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C1 Description One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know