POJ 2236:Wireless Network
描述
n台电脑,如果两台电脑间的距离的d范围内,则两台电脑能够连通。
如果AB连通,BC连通,则认为AC连通。
已知电脑台数N,最大距离d,以及每个电脑的坐标。有如下两种操作:
O i 表示修复编号为i的电脑
S i j 表示判断电脑ij能否连通。
对每一个S操作,如果电脑ij能连通,输出SUCCESS,否则输出FAIL
思路
典型的并查集。
代码
- 1 #include<cstdio>
- 2 #include<iostream>
- 3
- 4 using namespace std;
- 5
- 6 #define MAXN 1002
- 7
- 8 int par[MAXN];
- 9 int height[MAXN];
- 10 bool mode[MAXN];
- 11 bool dis[MAXN][MAXN];
- 12
- 13
- 14 void Init(int n)
- 15 {
- 16 for (int i = 0; i < n; i++)
- 17 {
- 18 par[i] = i;
- 19 height[i] = 0;
- 20 }
- 21 }
- 22
- 23 int find(int x)
- 24 {
- 25 if (par[x] == x)
- 26 return x;
- 27 else
- 28 return par[x] = find(par[x]);
- 29 }
- 30
- 31 void unite(int x,int y)
- 32 {
- 33 x = find(x);
- 34 y = find(y);
- 35
- 36 if (x!=y)
- 37 {
- 38 if (height[x] < height[y])
- 39 par[x] = y;
- 40 else
- 41 {
- 42 par[y] = x;
- 43 if (height[x] == height[y])height[x]++;
- 44 }
- 45 }
- 46
- 47 }
- 48
- 49 bool isSame(int x, int y)
- 50 {
- 51 return find(x) == find(y);
- 52 }
- 53
- 54
- 55 int main()
- 56 {
- 57 //freopen("C:\\Users\\zgwng\\Desktop\\2236.txt", "r",stdin);
- 58
- 59 int n, d;
- 60 cin >> n >> d;
- 61 int *x = new int[n];
- 62 int *y = new int[n];
- 63
- 64 for (int i = 0; i < n; i++)
- 65 {
- 66 cin >> x[i] >> y[i];
- 67 mode[i] = false;
- 68 }
- 69
- 70
- 71 int d2 = d*d;
- 72 for(int i=0;i<n;i++)
- 73 for (int j = 0; j < n; j++)
- 74 {
- 75 if ((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]) <= d2)
- 76 dis[i][j] = dis[j][i] = true;
- 77 else
- 78 dis[i][j] = dis[j][i] = false;
- 79
- 80 }
- 81 delete[]x;
- 82 delete[]y;
- 83
- 84 Init(n);
- 85 //开始处理输入操作
- 86 char oper;
- 87 while (cin>>oper)
- 88 {
- 89 int i, j;
- 90 if (oper == 'O')
- 91 {
- 92 //修复
- 93 cin >> i;
- 94 i--;
- 95 mode[i] = true;
- 96 for (int k = 0; k < n; k++)
- 97 {
- 98 if (dis[i][k] && mode[k])
- 99 unite(i, k);
- 100 }
- 101
- 102 }//end if oper==o
- 103 else
- 104 {
- 105 cin >> i >> j;
- 106 i--;
- 107 j--;
- 108 if (isSame(i, j))
- 109 cout << "SUCCESS" << endl;
- 110 else
- 111 cout << "FAIL" << endl;
- 112 }
- 113 }
- 114
- 115 return 0;
- 116 }
POJ 2236:Wireless Network的更多相关文章
- POJ 2236:Wireless Network(并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 36363 Accepted: 150 ...
- 【POJ - 2236】Wireless Network (并查集)
Wireless Network 这接翻译了 Descriptions 地震发生在东南亚.ACM(亚洲合作医疗团队)已经与膝上电脑建立了无线网络,但是一次意外的余震袭击,网络中的所有计算机都被打破了. ...
- POJ2236:Wireless Network(并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 39772 Accepted: 164 ...
- POJ 1459:Power Network(最大流)
http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...
- POJ 1459:Power Network 能源网络
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 25414 Accepted: 13247 D ...
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- [并查集] POJ 2236 Wireless Network
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 25022 Accepted: 103 ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
- POJ 2236 Wireless Network (并查集)
Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...
随机推荐
- eclipse gradle创建java项目
参考: https://blog.csdn.net/weixin_33733810/article/details/92438913 一 : 需要在 https://gradle.org/releas ...
- Html实现背景科技流星雨效果
<!doctype html><html><head><meta charset="utf-8" /><title>流星 ...
- vue项目环境搭建(webpack4从零搭建)--仅个人记录
一.nodejs环境搭建 首先需要下载node.js(推荐直接官网找最新的版本),下载完成后会自带npm包管理工具,打开cmd命令行界面,输入npm -v .node -v检查node.js与npm是 ...
- Chapter03 Java变量
Chapter03 变量 目录 Chapter03 变量 3.1 为什么需要变量 3.1.1 一个程序就是一个世界 3.1.2 变量是程序的基本组成单位 3.1.3 简单原理图 3.2 变(变化)量( ...
- 在矩池云上复现 PaddleGAN 照片转油画风格教程
我选用的是cuda10.1纯镜像 案例来自paddlepaddle官方PaddleGAN库中,可以查看 https://github.com/PaddlePaddle/PaddleGAN/ https ...
- MySQL配置主从分离
主服务器 192.168.176.110 从服务器 192.168.176.120 主数据库操作(ip:192.168.176.110) 配置MySQL主服务器的配置文件 [root@localho ...
- [NOIP2013 普及组] 表达式求值
[NOIP2013 普及组] 表达式求值 给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值. Input 一行,为需要你计算的表达式,表达式中只包含数字.加法运算符"+" ...
- SpringBoot中常用的45个注解
1.SpringBoot/spring @SpringBootApplication: 包含@Configuration.@EnableAutoConfiguration.@ComponentScan ...
- VuePress 博客之 SEO 优化(六)站长工具
前言 在 <一篇带你用 VuePress + Github Pages 搭建博客>中,我们使用 VuePress 搭建了一个博客,最终的效果查看:TypeScript 中文文档. 本篇接着 ...
- Spring——自动装配的三种实现方式
依赖注入的本质是装配,装配是依赖注入的具体行为 spring会在上下文中自动寻找,并自动给bean装配属性 自动装配的三种方式 (1).在xml中显式的装配 (2).在java中显式的装配 (3).隐 ...