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

The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats:
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

For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.

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的更多相关文章

  1. hdu 1829 基础并查集,查同性恋

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

  3. poj2236 基础并查集

    题目链接:http://poj.org/problem?id=2236 题目大意:城市网络由n台电脑组成,因地震全部瘫痪,现在进行修复,规定距离小于等于d的电脑修复之后是可以直接相连 进行若干操作,O ...

  4. HDU4496 D-City【基础并查集】

    Problem Description Luxer is a really bad guy. He destroys everything he met.  One day Luxer went to ...

  5. 并查集 poj2236

    网址:http://poj.org/problem?id=2236 题意:有n台坏的电脑,如果每两台电脑的距离不能超过d,那么这两台电脑有联系,用字符串O 表示标记第x台电脑维修了,用S判断从X到y是 ...

  6. AOJ 2170 Marked Ancestor (基础并查集)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...

  7. 并查集——poj2236(带权并查集)

    题目:Wireless Network 题意:给定n台已损坏计算机的位置和计算机最远通信距离d,然后分别根据命令执行以下两种操作: "O p" (1 <= p <= N ...

  8. CodeForces - 827A:String Reconstruction (基础并查集)

    Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...

  9. hdu1325 Is It A Tree? 基础并查集

    #include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...

随机推荐

  1. JSON对象转换成字符串【JSON2.JS】

    下载地址 https://github.com/douglascrockford/JSON-js JSON.JS和JSON2.JS的区别 JSON.JS使用的方法名称不同,用的是toJSONStrin ...

  2. 应不应该使用inline-block代替float

    CSS布局创建网站,浮动绝对占据了很大的比例.大块区域如主内容及侧边栏,以及在其中的小块区域,都可以看到浮动的影子.这里浮动是唯一的解决方案吗? 浮动通常表现正常,但有时候搞起来会很纠结.特别是处理内 ...

  3. linux 下的 mkfifo、exec 命令使用

    MKFIFOSection: User Commands (1)Updated: 1998年11月Index Return to Main Contents  NAME(名称)mkfifo - 创建F ...

  4. MAC下Xcode配置opencv(2017.3.29最新实践,亲测可行)

    本文原创,未经同意,谢绝转载!(转载请告知本人并且经过本人同意--By Pacific-hong) 本人小硕一枚,因为专业方向图像相关,所以用到opencv,然后网上MAC下Xcode配置opencv ...

  5. C#中ListView易错的方法

    现在有一个ListView(lv1),有2列. ListViewItem lvi = new ListViewItem(); lvi.Text = "语文"; lvi.SubIte ...

  6. js 操作 cookie

    js 操作 cookie 的方法如下: //设置cookie function setCookie(cname, cvalue, exdays) { var d = new Date(); d.set ...

  7. 20155323 2016-2017-2 《Java程序设计》第5周学习总结

    20155323 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 异常处理 提供两种异常处理机制:捕获异常和声明抛弃异常. 捕获异常:在Java程序运行过程中 ...

  8. Python之路-计算机基础

    一·计算机的组成 一套完整的计算机系统分为:计算机硬件,操作系统,软件.   硬件系统:运算器,控制器和存储器 ,输入设备,输出设备. 1.运算器:负责算数运算和逻辑运算,与控制器一起组成CPU. 2 ...

  9. 改变iOS app的icon(iOS10.3)

    改变iOS app的icon 官方 iOS10.3新增了可以让开发者去更改app的icon,接下来看看怎么更改. 官方API给的东西很少,只是介绍了一个实例方法: open func setAlter ...

  10. MongoDB基础教程系列--第七篇 MongoDB 聚合管道

    在讲解聚合管道(Aggregation Pipeline)之前,我们先介绍一下 MongoDB 的聚合功能,聚合操作主要用于对数据的批量处理,往往将记录按条件分组以后,然后再进行一系列操作,例如,求最 ...