基础并查集poj2236
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B.
In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.
Input
1. "O p" (1 <= p <= N), which means repairing computer p.
2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate.
The input will not exceed 300000 lines.
Output
Sample Input
4 1
0 1
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4
Sample Output
FAIL
SUCCESS
直接上代码
#include<iostream>
#include<cmath>
using namespace std;
int father[1010];
bool work[1010];
struct{
int x,y;
}e[1010];
int find(int x)
{
while(x!=father[x])x=father[x];
return x;
}
void merge(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)father[y]=x;
}
int main()
{
int n,d;
cin>>n>>d;//n是电脑数,d是最大距离
for(int i=1;i<=n;i++)
{
work[i]=0;
father[i]=i;
cin>>e[i].x>>e[i].y;
}
char q;
while(cin>>q){
if(q=='S')
{
int a,b;
cin>>a>>b;
if(find(a)==find(b))cout<<"SUCCESS"<<endl;
else cout<<"FAIL"<<endl;
}
else
{
int a;
cin>>a;
work[a]=1;
for(int i=1;i<=n;i++)
{
if(i==a)continue;
if(work[i]&&sqrt((e[i].x-e[a].x)*
(e[i].x-e[a].x)+(e[i].y-e[a].y)*(e[i].y-e[a].y))<=d)
merge(i,a);
}
}
}
return 0;
}
这是没有任何优化 的并查集
下面是优化后的(路径压缩+按秩合并)
#include<iostream>
#include<cmath>
using namespace std;
int father[1010],rank[1010];
bool work[1010];
struct{
int x,y;
}e[1010];
int find(int x)
{
int r=x;
while(r!=father[r])r=father[r];
int i=x,j;
while(i!=r){
j=father[i];
father[i]=r;
i=j;
}
return r;
}
void merge(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
{
if(rank[x]>rank[y])father[y]=x;
else
{
father[x]=y;
if(rank[x]==rank[y])rank[y]++;
}
}
}
int main()
{
int n,d;
cin>>n>>d;//n是电脑数,d是最大距离
for(int i=1;i<=n;i++)
{
work[i]=0;
rank[i]=0;
father[i]=i;
cin>>e[i].x>>e[i].y;
}
char q;
while(cin>>q){
if(q=='S')
{
int a,b;
cin>>a>>b;
if(find(a)==find(b))cout<<"SUCCESS"<<endl;
else cout<<"FAIL"<<endl;
}
else
{
int a;
cin>>a;
work[a]=1;
for(int i=1;i<=n;i++)
{
if(i==a)continue;
if(work[i]&&sqrt((e[i].x-e[a].x)*
(e[i].x-e[a].x)+(e[i].y-e[a].y)*(e[i].y-e[a].y))<=d)
merge(i,a);
}
}
}
return 0;
}
个人觉得优化后时间并没有差很远,优化后是8469ms,优化前8813ms。
但是万一题目要是卡时间,所以优化是有必要的。
基础并查集poj2236的更多相关文章
- hdu 1829 基础并查集,查同性恋
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- poj2236 基础并查集
题目链接:http://poj.org/problem?id=2236 题目大意:城市网络由n台电脑组成,因地震全部瘫痪,现在进行修复,规定距离小于等于d的电脑修复之后是可以直接相连 进行若干操作,O ...
- HDU4496 D-City【基础并查集】
Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ...
- 并查集 poj2236
网址:http://poj.org/problem?id=2236 题意:有n台坏的电脑,如果每两台电脑的距离不能超过d,那么这两台电脑有联系,用字符串O 表示标记第x台电脑维修了,用S判断从X到y是 ...
- AOJ 2170 Marked Ancestor (基础并查集)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...
- 并查集——poj2236(带权并查集)
题目:Wireless Network 题意:给定n台已损坏计算机的位置和计算机最远通信距离d,然后分别根据命令执行以下两种操作: "O p" (1 <= p <= N ...
- CodeForces - 827A:String Reconstruction (基础并查集)
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...
- hdu1325 Is It A Tree? 基础并查集
#include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...
随机推荐
- 【记录】解析具有合并单元格的Excel
最近公司让做各种数据表格的导入导出,就涉及到电子表格的解析,做了这么多天总结一下心得. 工具:NOPI 语言:C# 目的:因为涉及到导入到数据库,具有合并单元格的多行必然要拆分,而NPOI自动解析的时 ...
- React Native 之 数据持久化
前言 因为 实战项目系列 涉及到数据持久化,这边就来补充一下. 如本文有错或理解偏差欢迎联系我,会尽快改正更新! 如有什么问题,也可直接通过邮箱 277511806@qq.com 联系我. demo链 ...
- win8效果的横向布局
有一个月没写过博客了,自己的博客也没有看过,前段时间一直在忙着写代码,公司有一个制漆的产品,与传统纵向布局不一样,要求页面横向布局,类似win8的那种布局效果,最开始,我也没有什么头绪,然后硬着头皮做 ...
- React-Native 之 项目实战(三)
前言 本文有配套视频,可以酌情观看. 文中内容因各人理解不同,可能会有所偏差,欢迎朋友们联系我. 文中所有内容仅供学习交流之用,不可用于商业用途,如因此引起的相关法律法规责任,与我无关. 如文中内容对 ...
- Confluence安装&破解&汉化
p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...
- 老李分享:webservice是什么?2
web service 组件 基本的 web service 平台是 XML + HTTP.所有标准的 web service 使用以下组件: SOAP(简单对象访问协议) UDDI(通用描述.发现与 ...
- 老李推荐:第14章6节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-启动ViewServer
老李推荐:第14章6节<MonkeyRunner源码剖析> HierarchyViewer实现原理-装备ViewServer-启动ViewServer poptest是国内唯一一家培养 ...
- JSP的学习
JSP的学习 1. (1).服务器的名字:Tomcat (2).服务器浏览器访问的地址为: http://localhost:8080 http://127.0.0.1:8080 2.简单的知识 (1 ...
- JAVA加密算法系列-AES
package ***; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; ...
- Python第一天接触心得
最近想学Python,就开始看教程下载,官网是https://www.python.org/downloads/,最新版本是3.6.1, 注意:x86-64表示适用于windows 64位系统:x86 ...