基础并查集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[ ...
随机推荐
- (6)简单说说java中的线程
先甩出来两种创建线程的方法: private static int count = 100; public static void main(String[] args) { // 用继承Thread ...
- java根据HashMap中的值将其元素排序
思路:HashMap或Map本身没有排序功能,若要进行较轻松的排序,可利用ArrayList中的sort方法 例子: import java.util.ArrayList; import java.u ...
- 交叉编译Python-2.7.13到ARM(aarch32)—— 支持sqlite3
作者:彭东林 邮箱:pengdonglin137@163.com QQ: 405728433 环境 主机: ubuntu14.04 64bit 开发板: qemu + vexpress-a9 (参考: ...
- 浅谈js中的浅拷贝和深拷贝
在js中如何把一个对象里的属性和方法复制给另一个对象呢? 下面举一个例子来说明: var person={name:'chen',age:18}; var son={sex:'男'}; functio ...
- iOS开发之控制器创建与加载(生命周期)
1.如何创建一个控制器 控制器常见的创建方式有以下几种: (1)通过storyboard创建 (2)直接创建 MJViewController *mj = [[MJViewController all ...
- python生成二维码
1.python-qrcode是个用来生成二维码图片的第三方模块,依赖于 PIL 模块和 qrcode 库. 首先,我们要安装三个模块,qrcode,image,PIL. pip install q ...
- ArcEngine保存栅格数据至rastercatalog
将栅格数据(IRasterDataset)直接保存到数据库中很常见,但是保存到栅格目录下就比较少见,好不容易才找到,在这里记录一下. public void saveRasterDs2Catalog( ...
- 老李分享:loadrunner用javavuser进行接口测试
老李分享:loadrunner用javavuser进行接口测试 在这里分享一个poptest培训过程中案例,在日常工作中会遇到被测试系统通讯都是通过加密的数据包,加密算法是公司自己开发的,并且发送的数 ...
- 老李知识普及:web安全性的两大权威组织
老李知识普及:web安全性的两大权威组织 两个重要的WEB应用安全组织-WASC/OWASPWeb Application Security Consortium (WASC)a.WEB应用安全标准的 ...
- 手机自动化测试:Appium源码之API(2)
手机自动化测试:Appium源码之API(2) TouchAction AppiumDriver的辅助类,主要针对手势操作,比如滑动.长按.拖动等.TouchAction的原理是讲一系列的动作放在 ...