poj 2236 网络连接问题 并查集
题意:n台电脑,当两台之间的距离小于d的时候可以连接。 题目会进行操作“修复”还有“查询是否已经连接”。只要在查询的时候输出YES或者ON
思路:
- 把可以相互连接的 即两者之间的距离小于 d q[i].push_back(j) 还有 q[j].push_back(i) 这里面的q为动态数组 q[i]存储的是可以与i相连的机器
- 每台机器的初始根节点为i
- 修复操作的时候,进行更改根节点,如果i,j都已经被修复了,那么i j就可以连到同一个根节点上面
- 查询操作的时候,如果两者都在同一个根节点,就说明连接成功,否则连接失败
解决问题的代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
const int N = ;
int x[N], y[N], fa[N];
bool p[N];
vector<int> v[N];
int Find(int x)
{
if (fa[x] == x) return x;
return fa[x] = Find(fa[x]);
}
int main()
{
int n, d;
char s[];
memset(p, , sizeof(p));
scanf("%d%d", &n, &d);
for (int i = ; i <= n; i++)
{
scanf("%d%d", &x[i], &y[i]);
fa[i] = i;
}
for (int i = ; i <= n; i++)
for (int j = i + ; j <= n; j++)
if (((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j])) <= d * d)
{
v[i].push_back(j); v[j].push_back(i);
}
while (~scanf("%s", s))
{
int a, b;
if (s[] == '')
{
scanf("%d", &a);
p[a] = true;
for (int i = ; i < v[i].size(); i++)
{
if (p[v[a][i]])
{
b = Find(v[a][i]);
fa[b] = a;
}
}
}
else
{
scanf("%d%d", &a, &b);
int dx = Find(a);
int dy = Find(b);
if (dx == dy)
printf("SUCCESS\n");
else printf("FAIL\n");
}
}
}
poj 2236 网络连接问题 并查集的更多相关文章
- 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 ...
- POJ 2236 Wireless Network(并查集)
传送门 Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 24513 Accepted ...
- poj 2236 Wireless Network 【并查集】
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16832 Accepted: 706 ...
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 1182 食物链(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ...
- POJ 2762 tarjan缩点+并查集+度数
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15494 ...
- POJ 1182 (经典食物链 /并查集扩展)
(參考他人资料) 向量偏移--由"食物链"引发的总结 http://poj.org/problem?id=1182这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...
随机推荐
- (转)Linux内核参数设置sysctl命令详解
Linux内核参数设置sysctl命令详解 原文:https://www.zhukun.net/archives/8064 sysctl是一个允许您改变正在运行中的Linux系统的接口. 它包含一些 ...
- (转)CentOS之7与6的区别
CentOS之7与6的区别 原文:http://www.cnblogs.com/Csir/p/6746667.html http://blog.csdn.net/u012562943/article/ ...
- 如何更改Android的默认虚拟机地址(Android virtual driver路径设置)
1.将其他目录下的.android复制到C:\Documents and Settings\Administrator路径下(具体的用户名看自己的).然后进入.android\avd打开avd.ini ...
- enable assembly bind failure logging (Fusion) in .NET
今天遇到新建wcf项目编译成64位版本在64位windows上无法运气的,问题 先百度了一下如何查看程序集加载日志: Add the following values to HKEY_LOCAL_MA ...
- Kendo UI 单页面应用(四) Layout
Kendo UI 单页面应用(四) Layout Layout 继承自 View,可以用来包含其它的 View 或是 Layout.下面例子使用 Layout 来显示一个 View <div i ...
- Web Api2中使用Session
要在webApi里面使用Session必须在Global.asax插入 public override void Init() { this.PostAuthenticateRequest += (s ...
- GoAccess安装和使用介绍
使用文档参考地址:https://my.oschina.net/mrco/blog/181737https://www.fanhaobai.com/2017/06/go-access.html goa ...
- COGS 1619. [HEOI2012]采花
★★☆ 输入文件:1flower.in 输出文件:1flower.out 简单对比时间限制:5 s 内存限制:128 MB [题目描述] 萧薰儿是古国的公主,平时的一大爱好是采花. 今 ...
- phar打包项目压力对比测试
工具 http_load 测试url: http://api.test.chaoma.me/agent/ad/good_goods/query http://api.test.chaoma.me/ag ...
- HDU - 5493 Queue 2015 ACM/ICPC Asia Regional Hefei Online(线段树)
按身高排序,每个人前面最高的人数有上限,如果超出上限说明impossible, 每次考虑最小的人,把他放在在当前的从左往右第k+1个空位 因为要求字典序最小,所以每次k和(上限-k)取min值. 没有 ...