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 ...
随机推荐
- oj教程--贪心
贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解. 贪心算法不是对所有问题都能得到整体最优解,关键是 ...
- VSCode好用的插件
一.Chinese: 汉化 二.Live Server: Live Serve这是一个为静态和动态页面启动具有实时重新加载功能的开发本地服务器. 安装好后,在html页面右键:Open With Li ...
- omnet++:cMessage、cSimpleModule、cGate
cMessage Message可以被调度(self-message).取消.从一个Gate发送出去.直接发给另一个module:所有以上,都是通过cSimpleModule来实现的. Message ...
- 无状态子域名爆破工具:ksubdomain
概述 开源地址:https://github.com/knownsec/ksubdomain 二进制文件下载:https://github.com/knownsec/ksubdomain/releas ...
- 爬虫之Beautfulsoup模块及新闻爬取操作
今日内容概要 IP代理池的概念及使用 requests其他方法补充 Beautifulsoup模块 避免你自己写正则表达式 利用该模块爬取京东的商品信息 今日内容详细 IP代理池的概念及使用 1.有很 ...
- 《MySQL实战45讲》个人笔记-基础篇
拜读了林晓斌大佬的<MySQL实战45讲>,特意做个知识点总结,以便后期回忆. 01.基础架构:一条SQL查询语句是如何执行的? Server 层包括连接器.查询缓存.分析器.优化器.执行 ...
- Python简单入门心得(一)
很久之前就对Python感兴趣了,但是一直没时间学习,最近两天还有点时间,于是网上看了下视频,Python不愧是强类型的编程语言,对每一行的缩进的都有很严格的要求,比如一个判断,如果条件语句else不 ...
- Docker - 安装&测试
一.什么是Docker Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中 ...
- mysql命令flush privileges命令
修改一个用户的密码,命令如下 mysql> update user set password=password('新密码') where user='用户名'; 执行后报错: ERROR 105 ...
- MATLAB菜鸟入门笔记【作图章】
1.函数标签[legend] http://blog.csdn.net/wangcj625/article/details/6287735/