POJ 2236Wireless Network

题目来源:http://poj.org/problem?id=2236

Wireless Network

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 16470   Accepted: 6942

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

Source

题解:简单并查集~~

AC代码:

#include<cstdio>
#include<cmath>
const int Max=1002;
struct Node{
 double x,y;int loc;
}father[Max];
int set[Max],pos=0,n,dx,dy;
char ch; double d;
bool dis(int x,int k){
	double di=sqrt(pow(father[x].x-father[k].x,2)+pow(father[x].y-father[k].y,2));
	if(di<=d)	return true;
	return false;
}
int find(int k){
	if(k==father[k].loc) return k;
	int temp=find(father[k].loc);
	father[k].loc=temp;
	return temp;
//	return find(father[k].loc);
}
int main(){
	scanf("%d%lf",&n,&d);
	for(int i=1;i<=n;i++){
		scanf("%lf%lf",&father[i].x,&father[i].y);
		father[i].loc=i;
	}
	while(getchar(),~scanf("%c",&ch)){
	if(ch=='O'){
	scanf("%d",&dx);
	for(int i=0;i<pos;i++){
		if(dis(dx,set[i])){
			int tempx=find(set[i]);
			father[tempx].loc=find(dx);
		}
	}
	set[pos++]=dx;
	}
	else {
	scanf("%d%d",&dx,&dy);
	int tempx=find(dx),tempy=find(dy);
	if(tempx==tempy) printf("SUCCESS\n");
	else printf("FAIL\n");
	}
	}
	return 0;
} 
时间: 2024-10-10 06:14:01

POJ 2236Wireless Network的相关文章

POJ 2236Wireless Network (并查集)

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

poj 2531 Network Saboteur 解题报告

题目链接:http://poj.org/problem?id=2531 题目意思:将 n 个点分成两个部分A和B(也就是两个子集啦), 使得子集和最大(一定很难理解吧,呵呵).举个例子吧,对于样例,最佳的分法就是把点2分为一个子集,另一个子集理所当然就是1.3了. 2-1 的权值是50,2-3的权值是40,那么最大就是50+40 = 90了. 首先dfs的话,我不太会做啦.看了队长的用了状态压缩来做,一下子觉得好神奇!!!! 可能第一次接触,理解得不是太深刻,先留着吧.就觉得好神奇,好神奇...

poj 1861 Network

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13260   Accepted: 5119   Special Judge Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the c

poj Command Network 最小树形图

规定根节点,求一颗生成树使得权值最小,但由于是有向图,所以最小生成树算法失效. 查资料后得知此类问题叫做最小树形图. 解决最小树形图问题的朱刘算法,算法核心基于找 [最小弧集->找环,消环缩点] 的思想,来慢慢构造树形图. 所有的灵魂都在这张图上.0.0 注意缩点后的弧权值的处理 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algo

ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

题目连接:ZOJ 1542 POJ 1861 Network 网络 Network Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, t

POJ 1236 Network of Schools(强连通分量)

POJ 1236 Network of Schools 链接:http://poj.org/problem?id=1236 题意:有一些学校连接到一个计算机网络.这些学校之间达成了一个协议:每个学校维护着一个学校列表,它向学校列表中的学校发布软件.注意,如果学校B 在学校A 的列表中,则A 不一定在B 的列表中. 任务A:计算为使得每个学校都能通过网络收到软件,你至少需要准备多少份软件拷贝. 任务B:考虑一个更长远的任务,想确保给任意一个学校发放一个新的软件拷贝,该软件拷贝能发布到网络中的每个学

poj 2531 -- Network Saboteur

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9183   Accepted: 4313 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully di

Poj 3694 Network (连通图缩点+LCA+并查集)

题目链接: Poj 3694 Network 题目描述: 给出一个无向连通图,加入一系列边指定的后,问还剩下多少个桥? 解题思路: 先求出图的双连通分支,然后缩点重新建图,加入一个指定的边后,求出这条边两个端点根节点的LCA,统计其中的桥,然后把这个环中的节点加到一个集合中,根节点标记为LCA. 题目不难,坑在了数组初始化和大小 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #inclu

poj 1861 Network (kruskal)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13633   Accepted: 5288   Special Judge Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the c