传送门

 Wireless Network

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 24513   Accepted: 10227

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

思路

并查集模板题,奇怪的是用G++跑了3000MS多,C++则是1000MS多
 
#include<stdio.h>
#include<string.h>
const int maxn = 1005;
struct Node{
	int x,y;
}node[maxn];
int N,d,fa[maxn];
bool repair[maxn];

int find(int x)
{
	int r = x;
	while (r != fa[r])	r = fa[r];
	int i = x,j;
	while (i != r)
	{
		j = fa[i];
		fa[i] = r;
		i = j;
	}
	return r;
}

void unite(int x,int y)
{
	x = find(x),y = find(y);
	if (x != y)	fa[x] = y;
}

bool dist(struct Node xx,struct Node yy)
{
	return	((xx.x-yy.x)*(xx.x-yy.x)+(xx.y-yy.y)*(xx.y-yy.y)) <= d*d;
}

int main()
{
	int index,index1,index2;
	char op[5];
	memset(repair,false,sizeof(repair));
	scanf("%d%d",&N,&d);
	for (int i = 1;i <= N;i++)
	{
		fa[i] = i;
		scanf("%d%d",&node[i].x,&node[i].y);
	}
	while (~scanf("%s",op))
	{
		if (op[0] == 'O')
		{
			scanf("%d",&index);
			repair[index] = true;
			for (int i = 1;i <= N;i++)
			{
				if (i != index && repair[i] && dist(node[i],node[index]))	unite(i,index);
			}
		}
		else if (op[0] == 'S')
		{
			scanf("%d%d",&index1,&index2);
			index1 = find(index1),index2 = find(index2);
			if (index1 == index2)	printf("SUCCESS\n");
			else	printf("FAIL\n");
		}
	}
	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 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...

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

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

  9. poj 2236 Wireless Network 【并查集】

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

随机推荐

  1. 利用PowerShell+Jenkins,实现项目的自动化部署

    当项目越来越庞大,部署环境越来越多以后,就会越来越依赖于自动化.比如本人公司的项目,目前有6个web和4个windows service,同时本地有两套环境:开发自测试环境和QA测试环境.每次版本发布 ...

  2. 你误解 .net 了吗?

    我现在发现很多人对C#还存在很大的误解,例如C#是完全封闭的,C#不能跨平台,C#性能很差,C#不支持指针等等,持以上观点的人非常多,甚至最近看到的国内某机构对开发语言的统计中还写着C#不跨平台,不开 ...

  3. unity3d 扩展NGUI Tweener —— TweenTime

    这是今天做的一个小功能 策划想要一个时间滚动效果 那就搞呗!思路和之前写的tweenFillAmount一样 传送门:http://www.cnblogs.com/shenggege/p/479892 ...

  4. 30行代码实现Javascript中的MVC

    从09年左右开始,MVC逐渐在前端领域大放异彩,并终于在刚刚过去的2015年随着React Native的推出而迎来大爆发:AngularJS.EmberJS.Backbone.ReactJS.Rio ...

  5. nodejs实现Websocket的数据接收发送

    在去年的时候,写过一篇关于websocket的博文:http://www.cnblogs.com/axes/p/3586132.html ,里面主要是借助了nodejs-websocket这个插件,后 ...

  6. sql server死锁神器

    参考文章: http://blogs.msdn.com/b/sqlserverfaq/archive/2013/04/27/an-in-depth-look-at-sql-server-memory- ...

  7. 基于DDS的任意波形发生器

    实验原理 DDS的原理 DDS(Direct Digital Frequency Synthesizer)直接数字频率合成器,也可叫DDFS. DDS是从相位的概念直接合成所需波形的一种频率合成技术. ...

  8. 半平面交模板(BZOJ1007)

    #include<cstdio> #include<algorithm> #define LDB long double using namespace std; ]; str ...

  9. 弱占优策略--Weakly Dominant Strategy

    Weakly Dominant Strategy Equilibrium(均衡). 先说弱占优.一个策略弱占优就是说,无论其他人采取什么样的策略,这个策略的回报都大于等于其他策略的回报.如果所有人都使 ...

  10. git创建仓库

    创建仓库 git init: Git 使用 git init 命令来初始化一个 Git 仓库,Git 的很多命令都需要在 Git 的仓库中运行,所以 git init 是使用 Git 的第一个命令. ...