Wireless Network
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 36363   Accepted: 15086

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,则这两个电脑间可以相互通信。第一行输入n和d分别代表电脑的个数和能够通信的距离,接下来n行表示n台电脑的坐标。接下来如果开头输入O(注意是字母O不是数字0),继续输入一个数字p表示维修第p台电脑。如果输入S,继续输入两个数p,q,表示询问这两台电脑间是否可以通信,如果能,输出SUCCESS,如果不能,输出FAIL

就是一道并查集模板题,但是要多加一个对两点间距离的判断,最后输入S后,查询p和q的根节点是否相同,如果相同,则可以通信

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define ll long long
#define INF 0x3f3f3f3f
const int maxn=1e6+10;
int a[maxn];
int flag[maxn];
int d;
struct wzy
{
int x,y;
}p[maxn];
int find(int x)
{
if(x!=a[x])
return find(a[x]);
return a[x];
}
void join(int x,int y)
{
int dx=find(x);
int dy=find(y);
if(dy!=dx)
{
a[dx]=dy;
}
}
int vis(int a,int b)
{
if ((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y)<=d*d)//a,b两点距离如果小于d
return 1;
else
return 0;
}
int main(int argc, char const *argv[])
{
int n;
std::cin>>n>>d;
for(int i=1;i<=n;i++)
{
flag[i]=0;
a[i]=i;
}
for(int i=1;i<=n;i++)
{
std::cin>>p[i].x>>p[i].y;
}
char ch;
int x,y;
getchar();
while(std::cin>>ch)
{
if(ch=='O')
{
std::cin>>x;
flag[x]=1;
for(int i=1;i<=n;i++)
{
if(flag[i]&&vis(i,x)&&i!=x)//判断电脑是否修好,两点距离是否小于d
join(i,a[x]);//合并节点
}
}
else if(ch=='S')
{
std::cin>>x>>y;
//判断x和y的根节点
a[x]=find(x);
a[y]=find(y);
if(a[x]!=a[y])
std::cout<<"FAIL"<<std::endl;
else
std::cout<<"SUCCESS"<<std::endl;
}
}
return 0;
}

POJ 2236:Wireless Network(并查集)的更多相关文章

  1. POJ 2236 Wireless Network (并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 761 ...

  2. poj 2236 Wireless Network (并查集)

    链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 ...

  3. POJ 2236 Wireless Network [并查集+几何坐标 ]

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

  4. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  5. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  6. poj 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

  7. POJ 2236 Wireless Network(并查集)

    传送门  Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 24513   Accepted ...

  8. POJ 2236 Wireless Network (并查集)

    Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...

  9. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network

    题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p   代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...

  10. poj 2236 Wireless Network 【并查集】

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16832   Accepted: 706 ...

随机推荐

  1. java基础知识 构造方法

    在java里面,构造方法也就是构造函数 构造函数=构造方法;构造方法是一种特殊的方法,具有以下特点.(1)构造方法的方法名必须与类名相同.(2)构造方法没有返回类型,也不能定义为void,在方法名前面 ...

  2. 4.8 Routing -- Specifying The URL Type

    1. 默认的路由器使用浏览器的hash来加载应用程序的开始状态并且当你移动时同步保持.目前,这依赖于浏览器中存在的hashchange事件. 2. 假设下面的路由器,输入/#/posts/new将会把 ...

  3. Flask权限管理

    权限管理功能的实现可以分为以下几个小块: 1,新建数据库表Role,里面包括id(Integer,主键)name(String),permission(Integer),default(boolean ...

  4. JavaScript高级内容笔记:原型链、继承、执行上下文、作用域链、闭包

    最近在系统的学习JS深层次内容,并稍微整理了一下,作为备忘和后期复习,这里分享给大家,希望对大家有所帮助.如有错误请留言指正,tks. 了解这些问题,我先一步步来看,先从稍微浅显内容说起,然后引出这些 ...

  5. jmeter导入csv压测

    压测csv数据源(设置为utf-8格式),jmeter不需要第一行参数名 新建一个线程组,根据压测数据调整设置需要的循环次数(测试数据有9行,设置循环次数为9) 添加http头信息 Content-T ...

  6. Python3.6(windows系统)解决编码问题

    Python3.6(windows系统)解决编码问题 1,py文件代码: import urllib.request url = "http://www.douban.com/" ...

  7. python中模块导入问题(已解决)

    想在python中导入request包: 无此模块,于是先安装requests包: 但是提示"Requirement already satisfied".在提示的相应目录里,找到 ...

  8. ZLYZD团队第四周项目总结

    ZLYD团队第四周项目总结 项目进展 将Wall.java.Gold.java.Player.java.Fruit.java.Enemy.java.Ticker.java和Packman.java七个 ...

  9. Migrating from Spring 3 to Spring 4 - org.springframework.scheduling.quartz.CronTriggerBean

    I'm trying to migrate from spring 3.0.5 to spring 4.1.X . Spring 3 has Class named as "org.spri ...

  10. callee 与 caller

    arguments.callee 在函数内部指向函数本身 1.函数调用 function sum (num){ if(num <= 1){ return 1; }else{ return num ...