Wireless Network
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 22977   Accepted: 9613

Description

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 <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int MAXN=;
struct Node{
int x,y;
}nodes[MAXN];
int n,d;
bool state[MAXN];
int par[MAXN];
void prep()
{
for(int i=;i<MAXN;i++)
{
par[i]=i;
}
}
int fnd(int x)
{
if(par[x]==x)
{
return x;
}
return par[x]=fnd(par[x]);
}
void unite(int x,int y)
{
int a=fnd(x);
int b=fnd(y);
par[b]=a;
}
bool same(int x,int y)
{
return fnd(x)==fnd(y);
}
double dist(int x1,int y1,int x2,int y2)
{
return sqrt(1.0*(x1-x2)*(x1-x2)+1.0*(y1-y2)*(y1-y2));
}
int main()
{
prep();
memset(state,false,sizeof(state));
scanf("%d%d",&n,&d);
for(int i=;i<=n;i++)
{
scanf("%d%d",&nodes[i].x,&nodes[i].y);
}
getchar();
char op;
while(scanf("%c",&op)!=EOF)
{
if(op=='O')
{
int id;
scanf("%d",&id);
state[id]=true;
for(int i=;i<=n;i++)
{
if(i==id) continue;
double len=dist(nodes[id].x,nodes[id].y,nodes[i].x,nodes[i].y);
if(state[i]&&!same(i,id)&&len<=d)
{
unite(id,i);
}
}
}
else
{
int x,y;
scanf("%d%d",&x,&y);
if(state[x]&&state[y]&&same(x,y))
{
printf("SUCCESS\n");
}
else
{
printf("FAIL\n");
}
}
getchar();
} return ;
}

POJ2236(并查集入门)的更多相关文章

  1. hdu1272并查集入门

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. 并查集入门--畅通工程(HDU1232)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 畅通工程 Time Limit: 4000/2000 MS (Java/Others)    M ...

  3. poj2236(并查集)

    题目链接: http://poj.org/problem?id=2236 题意: 有n台计算机, 已知每台计算机的坐标, 初始时所有计算机都是坏的, 然后修复其中一些计算机, 已修复的计算机距离不超过 ...

  4. 并查集——poj2492(带权并查集入门)

    一.题目回顾 题目链接:传送门 题意:给定n只虫子,不同性别的可以在一起,相同性别的不能在一起.给你m对虫子,判断中间有没有同性别在一起的. 二.解题思路 种类并查集 和poj1073的本质一样 详见 ...

  5. 并查集——poj1703(带权并查集入门)

    传送门:Find them, Catch them 题意:警察抓获N个罪犯,这些罪犯只可能属于两个团伙中的一个,现在给出M个条件(D a b表示a和b不在同一团伙),对于每一个询问(A a b)确定a ...

  6. HDU1213:How Many Tables(并查集入门)

    -----------刷点水题练习java------------- 题意:给定N点,M边的无向图,问有多少个连通块. 思路:可以搜索;  可以并查集.这里用并查集练习java的数组使用,ans=N, ...

  7. 并查集入门(hdu1232“畅通工程”)

    在学习并查集之前,首先需要明白基本的并查集可以完成的功能.并查集主要是用于处理不相交集合的合并问题.它是一种基础算法,在离散数学中,可以利用并查集求一个图的连通分支,利用其这个特性可以为我们解决一系列 ...

  8. POJ1611 && POJ2524 并查集入门

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 28293   Accepted: 13787 De ...

  9. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

随机推荐

  1. [RK3288][Android6.0] USB OTG模式及切换【转】

    本文转载自:https://blog.csdn.net/kris_fei/article/details/78620960 Platform: RK3288 OS: Android 6.0 Kerne ...

  2. Spring中为什么实体类不用注入

    要理解为什么不用注入,首先就清楚注入的目的是什么?如果不注入,在程序中要使用某个类对象的方法,则需要去new一个对象.然后我们调用其中的方法,众所周知"程序=算法+数据".不失一般 ...

  3. Nginx location指令匹配顺序规则

    location匹配命令 1. “= ”,字面精确匹配, 如果匹配,则跳出匹配过程.(不再进行正则匹配) 2. “^~ ”,最大前缀匹配,如果匹配,则跳出匹配过程.(不再进行正则匹配) 3. 不带任何 ...

  4. 【转】数据库获得当前时间getdate()

    CONVERT(nvarchar(10),count_time,121): CONVERT为日期转换函数,一般就是在时间类型 (datetime,smalldatetime)与字符串类型(nchar, ...

  5. JavaWeb -- Servlet+JSP+JavaBean(MVC)模式

    Servlet+JSP+JavaBean(MVC)模式适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp负责数据显示,javabean负责封装数据. Servlet+JSP ...

  6. QT 创建主窗口 MainWindow 实例

    1. 2. mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include & ...

  7. 一个最简单的JMeter测试流程

    环境:     JMeter   3.3 JDK 1.8 首先下载JMeter安装包  可以去官网下载. http://jmeter.apache.org/ 然后选择Download Windows系 ...

  8. eclipse web项目导入itellij idea并启动

    概述 主要分为项目配置和tomcat配置两大步骤. 一.项目配置 打开idea,选择导入项 选择将要打开的项目路径后,继续选择项目的原本类型(后续引导设置会根据原本的项目类型更新成idea的项目),此 ...

  9. spring: 在表达式中使用类型

    如果要在SpEL中访问类作用域的方法和常量的话,要依赖T()这个关键的运算符.例如,为了在SpEL中表达Java的Math类,需要按照如下的方式使用T()运算符: T{java.lang.Math} ...

  10. let和const命令新总结

    let声明变量 新特性 声明的变量仅仅在块级作用域有效 块级作用域:外层作用于无法读取内层作用于周中的变量,内层作用域可以定义外层作用于中的同名变量 不存在变量提升 暂时性死区 只要在块级作用域内存在 ...