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. php数组函数-array_rand()

    array_rand()函数返回数组中的一个随机键名,或者如果指定函数返回键名不止一个,则返 回一个包含随机键名的数组. array_rand(array,number); array:必需.规定数组 ...

  2. Vue.js学习笔记 第八篇 组件

    全局注册组件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

  3. Spark基本概念快速入门

      Spark集群 一组计算机的集合,每个计算机节点作为独立的计算资源,又可以虚拟出多个具备计算能力的虚拟机,这些虚拟机是集群中的计算单元.Spark的核心模块专注于调度和管理虚拟机之上分布式计算任务 ...

  4. Android fill_parent和wrap_content分析

    fill_parent设置一个顶部布局或控件强制性让它布满整个屏幕.(这是不管内容大小,直接适应整个屏幕的大小,例长度设置了这,就只有长度适应屏幕的长度) wrap_content布局指根据视图内部内 ...

  5. 【转】Android PullToRefresh (ListView GridView 下拉刷新) 使用详解

    最近项目用到下拉刷新,上来加载更多,这里对PullToRefresh这控件进行了解和使用. 以下内容转载自:http://blog.csdn.net/lmj623565791/article/deta ...

  6. PHP 学习(一)——课程介绍

    一.课程路线介绍 教程的学习路线按照:初级——>中级——>高级——>项目实做 初级: 中级: 高级: 项目实做: 整体: Php体系了解:

  7. 中文乱码之myEclipse项目导入时中文乱码(待)

    方法1:检查默认的编码是否设置成utf-8. 步骤如图: window——>preferences... 若Text file encoding 中的编码为 Other == UTF-8 ,则已 ...

  8. 树的dfs序 && 系统栈 && c++ rope

    利用树的dfs序解决问题: 就是dfs的时候记录每个节点的进入时间和离开时间,这样一个完整的区间就是一颗完整的树,就转化成了区间维护的问题. 比如hdu3887 本质上是一个求子树和的问题 #incl ...

  9. js中对象的类型

    js中的类型分为三种,"内部对象"."宿主对象"."自定义对象" 1."内部对象"有Date.Function.Arra ...

  10. 代码题 — 剑指offer题目、新增题目

     1.剪绳子 给你一根长度为n的绳子,请把绳子剪成m段 (m和n都是整数,n>1并且m>1)每段绳子的长度记为k[0],k[1],...,k[m].请问k[0]*k[1]*...*k[m] ...