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. bootstrap-table的简要使用

    bttable功能强大!同时支持申明方式和编程方式来使用!是wenzhixin主导开发一款开源的表格控件! 文档比较详尽,但要求初学者已经比较熟悉js,jquey等基本内容,否则可能许多代码无法阅读! ...

  2. Luogu P2051 [AHOI2009]中国象棋(dp)

    P2051 [AHOI2009]中国象棋 题面 题目描述 这次小可可想解决的难题和中国象棋有关,在一个 \(N\) 行 \(M\) 列的棋盘上,让你放若干个炮(可以是 \(0\) 个),使得没有一个炮 ...

  3. shell 第一篇

    1. 查看当前linux 支持的shell 类型 [root@nfs01 ~]# cat /etc/shells /bin/sh /bin/bash /sbin/nologin /bin/dash / ...

  4. ubuntu16.04环境编译gSOAP

     一.gSOAP简介 SOAP 是基于 XML 的简易协议,可使应用程序在 HTTP 之上进行信息交换.或者更简单地说:SOAP 是用于访问网络服务的协议. SOAP 提供了一种标准的方法,使得运行在 ...

  5. GitBook的使用方法

    ---恢复内容开始--- 由于近期工作中使用gitbook编写讲义,现把出现的问题总结下: 1 . gitbook的安装 Gitbook与word等办公软件类似,能够编写文档,Gitbook中编写文档 ...

  6. H5C3--盒子模型

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. LTIME16小结(CodeChef)

    题目链接 最后一题是Splay...还没有学会..蒟蒻!!! A /****************************************************************** ...

  8. log4j2----JAVA日志打印

    注意:本篇文章是以log4j2.x 为例的,并不是log4j 1.x log4j 就是log for java  , log4j已经被移植到了C,C++,C#,Perl,Python和Ruby等语言中 ...

  9. oracle使用dblink跨库查询的例子

    本文介绍了oracle数据库使用dblink进行跨库查询的方法,oracle dblink跨库查询教程,需要的朋友参考下.   oracle dblink跨库查询 方法一:首先,创建数据库链接: 复制 ...

  10. Breakpoint 断点只生效一次