POJ 2236 Wireless Network

Wireless Network

Time Limit: 10000MS Memory Limit: 65536K

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只有一千而且最大10000ms所以这样直接遍历就行了。

#include <iostream>
#include <cmath>
using namespace std;
struct NODE {
	int x;
	int y;
	bool flag;
};
NODE node[1010];
int father[1010];
int findfather(int x) {
	int a = x;
	while (x != father[x]) x = father[x];
	while (a != father[a]) {
		int z = a;
		a = father[a];
		father[z] = x;
	}
	return x;
}
void unionfather(int a, int b) {
	int fa = findfather(a);
	int fb = findfather(b);
	if (fa != fb) father[fa] = fb;
}
int main() {
	int n, d;
	scanf("%d %d", &n, &d);
	for (int i = 1; i <= n; i++) father[i] = i;
	for (int i = 1; i <= n; i++) scanf("%d %d", &node[i].x, &node[i].y);
	char c;
	getchar();
	while (scanf("%c", &c) != EOF) {
		if (c == 'O') {
			int x;
			scanf("%d", &x);
			node[x].flag = true;
			for (int i = 1; i <= n; i++) {
				if (i != x && node[i].flag == true) {
					int d1 = (node[i].x - node[x].x) * (node[i].x - node[x].x);
					int d2 = (node[i].y - node[x].y) * (node[i].y - node[x].y);
					if ((d1 + d2) <= d * d) unionfather(i, x);
				} 
			}
		} else {
			int a, b;
			scanf("%d %d", &a, &b);
			if (findfather(a) != findfather(b)) cout << "FAIL\n";
			else cout << "SUCCESS\n"; 
		}
		getchar();
	}
	return 0;
} 
全部评论

相关推荐

10-25 22:20
门头沟学院 Java
代码飞升_不回私信人...:同学院本,个人亮点去了,打招呼里面的废话也去了,学院本就是路边一条,明天拉满然后该学还是学,小厂也行尽量先有一段实习。另外你的项目描述写的不好,具体列一下可被提问的点,然后量化一下指标或者收益吧
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务