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 ...
随机推荐
- 【PAT甲级】1040 Longest Symmetric String (25 分)(cin.getline(s,1007))
题意: 输入一个包含空格的字符串,输出它的最长回文子串的长度. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/std ...
- Solidity高级用法
1.内存数组: 将数组与memory结合形成内存数组,在函数调用完后就解释 uint[ ] memory values = new uint[ ](3);//初始化了一个长度为3的内存数组: #内存数 ...
- hbase入门-相关概念
hbase入门-概念理解 参考文档: https://blog.csdn.net/luanpeng825485697/article/details/80319552 1. hbase概念 ...
- ZOJ4102 Array in the Pocket(2019浙江省赛)
贪心~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int b[maxn]; int vis[maxn]; se ...
- day5-1继承
继承: Constructor属性和prototype属性的关系: 创建了自定义的构造函数之后,其原型对象默认会取得constructor属性:当调用构造函数创建一个新实例后,该实例的内部将包含一个指 ...
- 【剑指Offer面试编程题】题目1360:乐透之猜数游戏--九度OJ
题目描述: 六一儿童节到了,YZ买了很多丰厚的礼品,准备奖励给JOBDU里辛劳的员工.为了增添一点趣味性,他还准备了一些不同类型的骰子,打算以掷骰子猜数字的方式发放奖品.例如,有的骰子有6个点数(点数 ...
- Django:cookie和session相关问题
http://www.cnblogs.com/fnng/p/3750596.html http://www.cnblogs.com/chenchao1990/p/5283725.html
- freemarker 作为 word 模板实现下载功能
1:新建一个.doc 文档写好自己要导出文字如下图 2:把word 文档另存为xml 格式的文件用编辑器打开如图下,(如果你打开文件里面的标签没换行格式,那么你在myeclipse 新建一个jsp 文 ...
- Linux centos7 linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍、 target介绍
一.linux任务计划cron crontab -u -e -l -r 格式;分 时 日 月 周 user command 文件/var/spool/corn/username 分范围0-59,时范 ...
- matplotlib 柱状图 Bar Chart 样例及参数
def bar_chart_generator(): l = [1,2,3,4,5] h = [20, 14, 38, 27, 9] w = [0.1, 0.2, 0.3, 0 ...