poj 2236

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 题意:有一堆坏电脑,每个电脑都有一个二维坐标。有两种操作,修一台电脑和在线询问两台电脑是否可以连通。若两台电脑相距小于等于d或者可以经过第三方电脑连通则连通。给出询问操作的答案。
题解:显然并查集。因为修和询问都是实时在线操作,所以修完一台就看看有没有能和它相连的。
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = ; int dx[maxn], dy[maxn], Rank[maxn], par[maxn], re[maxn];
double dis[maxn][maxn];
int N;
double D; void init()
{
for (int i = ; i <= N; i++) {
Rank[i] = ; par[i] = i;
}
} int find(int x)
{
if (par[x] == x) return x;
else return find(par[x]);
} void unite(int xx, int yy)
{
int x = find(xx); int y = find(yy);
if (x == y) return;
if (Rank[x] < Rank[y]) par[x] = y;
else {
par[y] = x;
if (Rank[x] == Rank[y]) Rank[x]++;
}
} bool same(int x, int y)
{
return find(x) == find(y);
} int main()
{
cin >> N;
cin >> D;
init();
for (int i = ; i <= N; i++) cin >> dx[i] >> dy[i];
for(int i=;i<=N;i++)
for (int j = i + ; j <= N; j++) {
dis[i][j] = dis[j][i] = sqrt((double)(dx[i] - dx[j])*(dx[i] - dx[j]) + (double)(dy[i] - dy[j])*(dy[i] - dy[j]));
}
char op; int p, q;
int cnt = ;
while (cin>>op)
{
if (op == 'O') {
cin >> p;
re[cnt++] = p;
for (int i = ; i < cnt - ; i++)
if (dis[re[i]][p] <= D)
unite(re[i], p);
}
else
{
cin >> p >> q;
if (same(p, q))
cout << "SUCCESS" << endl;
else cout << "FAIL" << endl;
}
}
return ;
}

poj 2236【并查集】的更多相关文章

  1. poj 2236 并查集

    并查集水题 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring& ...

  2. Wireless Network POJ - 2236 (并查集)

    #include<iostream> #include<vector> #include<string> #include<cmath> #includ ...

  3. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  4. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

  5. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  6. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  7. POJ 3228 [并查集]

    题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...

  8. poj 1733 并查集+hashmap

    题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...

  9. poj 3310(并查集判环,图的连通性,树上最长直径路径标记)

    题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...

  10. POJ 3657 并查集

    题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...

随机推荐

  1. Http post请求案例

    public RmiRespBase sendHttpRes(String jsonParamStr, String url, String apiName,String systemId,Strin ...

  2. day65-test

    目录 一.点击事件控制标签颜色 二.实现点击次数,变换页面标签的颜色 三.周期性实现颜色的旋转变色 练习题 一.点击事件控制标签颜色 1.有 红.黄.蓝 三个按钮,以及一个200x200矩形框box, ...

  3. 【php实现数据结构】单向链表

    什么是单向链表 链表是以链式存储数据的结构,其不需要连续的存储空间,链表中的数据以节点来表示,每个节点由元素(存储数据)和指针(指向后继节点)组成. 单向链表(也叫单链表)是链表中最简单的一种形式,每 ...

  4. npm install时出现error

    今天启动vue前端时,发现依赖没了.于是乎cmd->npm install->更新了部分依赖后,出现error信息,提示更新依赖失败.很奇怪,原来这个项目都是好的,为啥突然更新下来依赖了呢 ...

  5. springboot使用@Aspect实现AOP记录日志讲解

    AOPAOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.在日常开发当中经常用来记录日志,方法跟踪 ...

  6. 扫描线矩形周长的并 POJ1177

    //扫描线矩形周长的并 POJ1177 // 我是按x轴 #include <iostream> #include <cstdio> #include <cstdlib& ...

  7. 百度地图JavaScript API申请密钥注意要点

    1.应用类型:浏览器端 2.启用服务:Javascript API要勾选 3.IP白名单:*即可

  8. php获取数据转换成json格式

    <?php header("content-type:text/html;charset=utf-8"); $con=mysql_connect("localhos ...

  9. Java review-basic2

    1.Implement a thread-safe (blocking) queue: Class Producer implements Runable{ Private final Blockin ...

  10. 关于github 代码管理,协作开发

    公司要用github 进行项目管理,  了解了一下github相关权限管理. 并做笔记如下: 个人账户可以建立公有/私有  repository ,  公有的全天下的人都能看到,私有的全天下人都看不到 ...