算法

POJ2236总结

Wireless 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 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

我的代码

并查集。维修一个电脑之后要遍历所有已经被修好的电脑,如果能连接上就合并。需要路径压缩。

#include<stdio.h>
#include<math.h>

struct position
{
	int x,y;
}computer[1010];

int pre[1010];
int repaired[1010]={0};
int n,d,p,q;
char op;
	
int unionsearch(int root)
{
	int son, tmp;
	son = root;
	while(root != pre[root])
		root = pre[root];
	while(son != root)
	{
		tmp = pre[son];
		pre[son] = root;
		son = tmp;
	}
	return root;
}
 
void join(int root1, int root2)
{
	int x, y;
	x = unionsearch(root1);
	y = unionsearch(root2);
	if(x != y)
		pre[x] = y;
}

bool adjoin(int a,int b)
{
	double x=computer[a].x-computer[b].x;
	double y=computer[a].y-computer[b].y;
	return sqrt(x*x+y*y)<=1.0*d;
}
int main()
{
	
	scanf("%d%d",&n,&d);
	for (int i=1;i<=n;i++)
	{
		pre[i]=i;
		scanf("%d%d",&computer[i].x,&computer[i].y);
	}
	getchar();
	while(scanf("%c",&op)==1)
	{
		if (op=='O')
		{
			scanf("%d",&p);
			for (int i=1;i<=n;i++)
			{
				if (repaired[i]==1&&adjoin(i,p))
				{
					join(i,p);
				}
			}
			repaired[p]=1;
		}
		else
		{
			scanf("%d%d",&p,&q);
			int x, y;
			x = unionsearch(p);
			y = unionsearch(q);
			if(x != y) printf("FAIL\n");
			else printf("SUCCESS\n");
		}
		getchar();
	}
	return 0;
}

反思

第一次提交编译不通过,因为POJ要求sqrt的参数必须是浮点数。

Main.cpp
F:\temp\21738449.165437\Main.cpp(42) : error C2668: 'sqrt' : ambiguous call to overloaded function
        math.h(581): could be 'long double sqrt(long double)'
        math.h(533): or       'float sqrt(float)'
        math.h(128): or       'double sqrt(double)'
        while trying to match the argument list '(int)'

读入字符时在格式符前加空格printf(” %c”,&op);会读掉一个ctrl+Z,不知道会不会造成读取错误,我换成getchar了没试。

发表评论