poj2236 Wireless Network(并查集直接套模板
题目地址:http://poj.org/problem?id=2236
题目大意:n台电脑都坏了,只有距离小于d且被修好的电脑才可以互相联系,联系可传递。输入n和d,n个点的坐标x y。两个操作:O x表示把电脑x修好了,S x y表示询问x和y能否联系。
思路:并查集把能互相联系的电脑都放到同一个集合里(联系可以互相传递),查询的时候只要看x和y是否在一个集合就行了。
另设置两个数组visit[x]表示x是否被修好,dis[x][y]表示x和y之间的距离是否符合要求【先预处理任意两个点之间的距离<=d的标记为1】。
每修好一台电脑记得改变visit标记且把所有和他能联系的电脑合并(距离合适且修好了)。
#include <cstdio> using namespace std; const int maxn = ;
int fa[maxn], visit[maxn], dis[maxn][maxn]; struct node
{
int x, y;
}node[maxn]; //先预处理所有点间的距离
void pre(int n, int d)
{
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
{
int dx = node[i].x - node[j].x;
int dy = node[i].y - node[j].y;
if(dx*dx + dy*dy <= d*d)
dis[i][j] = ;
else
dis[i][j] = ;
}
} void init(int n)
{
for(int i = ; i <= n; i++)
{
fa[i] = i;
visit[i] = ;
}
} int found(int x)
{
if(fa[x] == x)
return x;
return fa[x] = found(fa[x]);
} void unite(int x, int y)
{
int px = found(x);
int py = found(y);
if(px == py)
return;
else
fa[px] = py;
} int main()
{
int n, d, x, y;
char opr;
scanf("%d%d", &n, &d);
init(n);
for(int i = ; i <= n; i++)
scanf("%d%d", &node[i].x, &node[i].y);
pre(n, d);//pre要写在输入坐标之后!!!!不然dis就全是1!!!
getchar();
while(scanf("%c", &opr) == )
{
if(opr == 'O')
{
scanf("%d", &x);
visit[x] = ;
for(int i = ; i <= n; i++)
if(visit[i] && dis[x][i])//距离合适且修好了的就可以互相联络
unite(x, i);//能互相联络的都放一个集合里
}
else
{
scanf("%d%d", &x, &y);
if(found(x) == found(y))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
getchar();
}
return ;
}
【pre(n, d)要放到输入坐标之后!!!!!!因为要用到坐标!!!!!!!!不然dis全是1!!!!!!!!!!!!!!!!!!!!!!!】
poj2236 Wireless Network(并查集直接套模板的更多相关文章
- POJ2236 Wireless Network 并查集简单应用
Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have ...
- POJ2236 Wireless Network 并查集
水题 #include<cstdio> #include<cstring> #include<queue> #include<set> #include ...
- POJ 2236 Wireless Network (并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 18066 Accepted: 761 ...
- Wireless Network 并查集
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...
- poj 2236 Wireless Network (并查集)
链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 ...
- POJ 2236 Wireless Network [并查集+几何坐标 ]
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- POJ-2236 Wireless Network 顺便讨论时间超限问题
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 26131 Accepted: 108 ...
- [LA] 3027 - Corporative Network [并查集]
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
随机推荐
- N3K异常重启(案例)
在实际的情况下,有时候会遇到设备无故重启的问题,这个时候,我们需要判断一下重启的根本原因是什么,是否有规避的方法等. 这里记录了几个N3K异常重启的问题. 案例1: 设备型号:N3K-C3048TP- ...
- JAVA web课堂测试1
1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分)2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分)3性别:要求用单选框或下拉框实现 ...
- echarts做飞线图
先上图,要不感觉没有说服力: 飞线图应该是大屏中很常见的一种了,通常你可以很轻易的用datav做一个飞线图,而且datav做的大屏逼格真的很高,本身也是开源免费的项目,开箱即用,上手简单……行了回归正 ...
- 使用 Sandcastle Help File Builder 制作文档
1.下载安装 Sandcastle 程序. http://download-codeplex.sec.s-msft.com/Download/Release?ProjectName=shfb& ...
- 2.1.1Remove Duplicates from Sorted Arr
/* 题目:2.1.1 Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place ...
- SpringBoot中普通类无法通过@Autowired自动注入Service、dao等bean解决方法
无法注入原因: 有的时候我们有一些类并不想注入Spring容器中,有Spring容器实例化,但是我们又想使用Spring容器中的一些对象,所以就只能借助工具类来获取了 工具类: package com ...
- Flex 学习笔记
Flex布局是什么 Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 任何一个容器都可以指定为Flex布局 .box{ displ ...
- FTP虚拟账户
部署一个内网FTP服务器 为了解决公司员工文件存储和下载的需求.要求部署内部FTP服务器,员工可以通过自己的账号的权限对FTP进行操作. 1)公司公共文件可以通过匿名下载 2)公司财务部.商务部.行政 ...
- 学习笔记(15)- 保险行业的问答语料 insuranceqa_data
数据概览 ''' pool data are translated Chinese data with Google API from original English data ''' POOL_T ...
- python3爬虫
1.爬虫的基本原理讲解 2.Urllib库的基本使用 3.Requests库的基本使用 4.正则的基本使用 5.BeautifulSoup库的使用 6.PyQuery库的使用 √ 7.Seleni ...