poj2236_并查集_Wireless Network

Wireless Network

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 24497   Accepted: 10213

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

Source

POJ Monthly,HQM

 1 #include <iostream>
 2 #include <cstdio>
 3
 4 #define MAX_N 1000+5
 5
 6 using namespace std;
 7
 8 struct point{int x;int y;int jud;};
 9 point p[1005];
10 int par[MAX_N];//父节点
11 int depth[MAX_N];//深度
12
13 void init(int n){
14    for(int i=0;i<=n;i++){
15        par[i]=i;
16        depth[i]=1;
17    }
18 }
19 int find_father(int t){
20    if(t==par[t]){
21        return t;
22    }else{
23        return par[t]=find_father(par[t]);
24        //实现了路径压缩
25    }
26 }
27 void unite(int t1,int t2){
28    int f1=find_father(t1);
29    int f2=find_father(t2);
30    if(f1==f2){
31        return ;
32    }
33    if(depth[f1]<depth[f2]){
34        par[f1]=f2;
35    }else{
36        par[f2]=f1;
37        if(depth[f1]==depth[f2]){
38            depth[f1]++;
39            //记录深度
40        }
41    }
42 }
43
44 bool same(int x,int y){
45     return find_father(x)==find_father(y);
46 }
47
48 int distanc_2(point a,point b){
49     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
50 }
51
52
53 int main()
54 {
55     int n,d;
56     char c;
57     int t1,t2;
58     scanf("%d %d",&n,&d);
59     init(n);
60     for(int i=1;i<=n;i++){
61         scanf("%d %d",&p[i].x,&p[i].y);
62         p[i].jud=0;
63     }
64         getchar();
65     while(scanf("%c",&c)!=EOF){
66         if(c==‘O‘){
67             scanf("%d",&t1);
68             getchar();
69             p[t1].jud=1;
70
71             for(int i=1;i<=n;i++){
72                 if(i!=t1&&p[i].jud==1){
73                     if(distanc_2(p[t1],p[i])<=d*d){
74                         unite(t1,i);
75                     }
76                 }
77             }
78         }else{
79             scanf("%d %d",&t1,&t2);
80             getchar();
81             if(same(t1,t2)){
82                 printf("SUCCESS\n");
83             }else{
84                 printf("FAIL\n");
85             }
86         }
87      }
88     return 0;
89 }
时间: 2025-01-02 18:25:37

poj2236_并查集_Wireless Network的相关文章

POJ 2236 (简单并查集) Wireless Network

题意: 有n个电脑坏掉了,分别给出他们的坐标 有两种操作,可以O x表示修好第x台电脑,可以 S x y表示x y是否连通 两台电脑的距离不超过d便可连通,两台电脑是连通的可以直接连通也可以间接通过第三台电脑连通 思路: 每次修好一台电脑都和前面已经修好的电脑比较一下如果距离小于d而且不在同一网络,便合并在一起即可 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring&g

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

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

POJ1962:Corporative Network(并查集)

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

Corporative Network(带权并查集)

这个题的题意是  当输入'E'是查找操作,查找从后面这个数到他的父亲这边的值,'I'代表把后面的数作为前面数的父亲 然后他们两个的差值代表这两个边的权值 水水的题 #include <stdio.h> #include <string.h> int par[20005]; int rank1[20005]; int abs(int hh) { return (hh>0)?hh:-hh; } void init() { for(int i=0;i<20005;i++) {

[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

POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y均能C通信,则x和y可以通信.现在给出若干个操作, O p 代表修复编号为p的电脑 S p q代表询问p和q是不是能通信. 思路: 并查集即可.. 如果修复了一台电脑,则把与它相连距离不超过d的且修复了的放在一个集合里面. #include<cstdio> #include<cstring&

UVALive 3027 Corporative Network 带权并查集

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

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