Wireless Network

Wireless Network

题目链接:http://poj.org/problem?id=2236

注意:题目中说的是 1~n,所以,在初始化根节点的时候不要弄成 0 ~ n-1这种(for(int i = 0;i < n;i++) 这种是不对的,如果这样,则造成n没有对应的根节点,因为初始化的时候根本没有初始化到n

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 using namespace std;
 6 
 7 const int maxn = 1010;
 8 
 9 struct node {
10     int x,y;
11     bool open;
12 }arr[maxn];
13 int f[maxn];
14 int n,d;
15 
16 void Init(int n) {
17     for(int i = 1;i <= n;i++)
18         f[i] = i;
19 }
20 
21 int FindRoot(int x) {
22     if(x == f[x])
23         return x;
24     f[x] = FindRoot(f[x]);
25     return f[x];
26 }
27 
28 double Count(node a,node b) {
29     return sqrt(pow(a.x - b.x,2) + pow(a.y - b.y,2));
30 }
31 
32 void Union(int x) {
33     int a = FindRoot(x);
34     for(int i = 1;i <= n;i++) {
35         int b = FindRoot(i);
36         if(arr[i].open && Count(arr[i],arr[x]) <= d)
37             f[b] = a;
38     }
39 }
40 
41 int main() {
42     while(scanf("%d%d",&n,&d) != EOF) {
43         Init(n);
44         for(int i = 1;i <= n;i++) {
45             scanf("%d%d",&arr[i].x,&arr[i].y);
46             arr[i].open = false;
47         }
48         char ch;
49         int x,y;
50         while(cin>>ch) {
51             if(ch == 'O') {
52                 scanf("%d",&x);
53                 arr[x].open = true;
54                 Union(x);
55             }
56             else {
57                 scanf("%d%d",&x,&y);
58                 if(FindRoot(x) == FindRoot(y))
59                     printf("SUCCESS\n");
60                 else
61                     printf("FAIL\n");
62             }
63         }
64     }
65     return 0;
66 }

 

posted @ 2019-02-13 23:42 Youpeng 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值