描述

n台电脑,如果两台电脑间的距离的d范围内,则两台电脑能够连通。

如果AB连通,BC连通,则认为AC连通。

已知电脑台数N,最大距离d,以及每个电脑的坐标。有如下两种操作:

O i 表示修复编号为i的电脑

S i j 表示判断电脑ij能否连通。

对每一个S操作,如果电脑ij能连通,输出SUCCESS,否则输出FAIL

思路

典型的并查集。

初始:每个点的父节点都是自身
 
每修复一个点,让所有能与这个点连通的点的根节点都指向这个点的根节点
每查询两个点,取得它们的根节点,判断是否相同,如果相同,输出SUCCESS,否则,输出FALSE

代码

  1. 1 #include<cstdio>
  2. 2 #include<iostream>
  3. 3
  4. 4 using namespace std;
  5. 5
  6. 6 #define MAXN 1002
  7. 7
  8. 8 int par[MAXN];
  9. 9 int height[MAXN];
  10. 10 bool mode[MAXN];
  11. 11 bool dis[MAXN][MAXN];
  12. 12
  13. 13
  14. 14 void Init(int n)
  15. 15 {
  16. 16 for (int i = 0; i < n; i++)
  17. 17 {
  18. 18 par[i] = i;
  19. 19 height[i] = 0;
  20. 20 }
  21. 21 }
  22. 22
  23. 23 int find(int x)
  24. 24 {
  25. 25 if (par[x] == x)
  26. 26 return x;
  27. 27 else
  28. 28 return par[x] = find(par[x]);
  29. 29 }
  30. 30
  31. 31 void unite(int x,int y)
  32. 32 {
  33. 33 x = find(x);
  34. 34 y = find(y);
  35. 35
  36. 36 if (x!=y)
  37. 37 {
  38. 38 if (height[x] < height[y])
  39. 39 par[x] = y;
  40. 40 else
  41. 41 {
  42. 42 par[y] = x;
  43. 43 if (height[x] == height[y])height[x]++;
  44. 44 }
  45. 45 }
  46. 46
  47. 47 }
  48. 48
  49. 49 bool isSame(int x, int y)
  50. 50 {
  51. 51 return find(x) == find(y);
  52. 52 }
  53. 53
  54. 54
  55. 55 int main()
  56. 56 {
  57. 57 //freopen("C:\\Users\\zgwng\\Desktop\\2236.txt", "r",stdin);
  58. 58
  59. 59 int n, d;
  60. 60 cin >> n >> d;
  61. 61 int *x = new int[n];
  62. 62 int *y = new int[n];
  63. 63
  64. 64 for (int i = 0; i < n; i++)
  65. 65 {
  66. 66 cin >> x[i] >> y[i];
  67. 67 mode[i] = false;
  68. 68 }
  69. 69
  70. 70
  71. 71 int d2 = d*d;
  72. 72 for(int i=0;i<n;i++)
  73. 73 for (int j = 0; j < n; j++)
  74. 74 {
  75. 75 if ((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]) <= d2)
  76. 76 dis[i][j] = dis[j][i] = true;
  77. 77 else
  78. 78 dis[i][j] = dis[j][i] = false;
  79. 79
  80. 80 }
  81. 81 delete[]x;
  82. 82 delete[]y;
  83. 83
  84. 84 Init(n);
  85. 85 //开始处理输入操作
  86. 86 char oper;
  87. 87 while (cin>>oper)
  88. 88 {
  89. 89 int i, j;
  90. 90 if (oper == 'O')
  91. 91 {
  92. 92 //修复
  93. 93 cin >> i;
  94. 94 i--;
  95. 95 mode[i] = true;
  96. 96 for (int k = 0; k < n; k++)
  97. 97 {
  98. 98 if (dis[i][k] && mode[k])
  99. 99 unite(i, k);
  100. 100 }
  101. 101
  102. 102 }//end if oper==o
  103. 103 else
  104. 104 {
  105. 105 cin >> i >> j;
  106. 106 i--;
  107. 107 j--;
  108. 108 if (isSame(i, j))
  109. 109 cout << "SUCCESS" << endl;
  110. 110 else
  111. 111 cout << "FAIL" << endl;
  112. 112 }
  113. 113 }
  114. 114
  115. 115 return 0;
  116. 116 }

POJ 2236:Wireless Network的更多相关文章

  1. POJ 2236:Wireless Network(并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 36363   Accepted: 150 ...

  2. 【POJ - 2236】Wireless Network (并查集)

    Wireless Network 这接翻译了 Descriptions 地震发生在东南亚.ACM(亚洲合作医疗团队)已经与膝上电脑建立了无线网络,但是一次意外的余震袭击,网络中的所有计算机都被打破了. ...

  3. POJ2236:Wireless Network(并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 39772   Accepted: 164 ...

  4. POJ 1459:Power Network(最大流)

    http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...

  5. POJ 1459:Power Network 能源网络

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 25414   Accepted: 13247 D ...

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

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  7. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  8. poj 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

  9. POJ 2236 Wireless Network (并查集)

    Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...

随机推荐

  1. eclipse gradle创建java项目

    参考: https://blog.csdn.net/weixin_33733810/article/details/92438913 一 : 需要在 https://gradle.org/releas ...

  2. Html实现背景科技流星雨效果

    <!doctype html><html><head><meta charset="utf-8" /><title>流星 ...

  3. vue项目环境搭建(webpack4从零搭建)--仅个人记录

    一.nodejs环境搭建 首先需要下载node.js(推荐直接官网找最新的版本),下载完成后会自带npm包管理工具,打开cmd命令行界面,输入npm -v .node -v检查node.js与npm是 ...

  4. Chapter03 Java变量

    Chapter03 变量 目录 Chapter03 变量 3.1 为什么需要变量 3.1.1 一个程序就是一个世界 3.1.2 变量是程序的基本组成单位 3.1.3 简单原理图 3.2 变(变化)量( ...

  5. 在矩池云上复现 PaddleGAN 照片转油画风格教程

    我选用的是cuda10.1纯镜像 案例来自paddlepaddle官方PaddleGAN库中,可以查看 https://github.com/PaddlePaddle/PaddleGAN/ https ...

  6. MySQL配置主从分离

    主服务器 192.168.176.110 从服务器 192.168.176.120 主数据库操作(ip:192.168.176.110)  配置MySQL主服务器的配置文件 [root@localho ...

  7. [NOIP2013 普及组] 表达式求值

    [NOIP2013 普及组] 表达式求值 给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值. Input 一行,为需要你计算的表达式,表达式中只包含数字.加法运算符"+" ...

  8. SpringBoot中常用的45个注解

    1.SpringBoot/spring @SpringBootApplication: 包含@Configuration.@EnableAutoConfiguration.@ComponentScan ...

  9. VuePress 博客之 SEO 优化(六)站长工具

    前言 在 <一篇带你用 VuePress + Github Pages 搭建博客>中,我们使用 VuePress 搭建了一个博客,最终的效果查看:TypeScript 中文文档. 本篇接着 ...

  10. Spring——自动装配的三种实现方式

    依赖注入的本质是装配,装配是依赖注入的具体行为 spring会在上下文中自动寻找,并自动给bean装配属性 自动装配的三种方式 (1).在xml中显式的装配 (2).在java中显式的装配 (3).隐 ...