POJ 2236 Wireless Network (并查集)

Wireless Network
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 17584   Accepted: 7410

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

我发现好多题 都需要各种预处理 - -  那么本题也是的 先预处理出 可能会相连的边(就是距离够的) 

每次repair的时候 就检查与它相连的所有的已经repaired的点~   合并就好了

然后查询的时候 判断是不是在同一集合~ 

AC代码如下:

//
//  POJ 2236 Wireless Network
//
//  Created by TaoSama on 2015-03-16
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e4 + 10;

int n, d, repaired[N], par[N], rank[N];
pair<int, int> pnt[N];
vector<int> edge[N];

void init(int n) {
	for(int i = 1; i <= n; ++i) {
		par[i] = i;
		rank[i] = 0;
	}
}
int find(int x) {
	if(par[x] == x) return x;
	return par[x] = find(par[x]);
}
void unite(int x, int y) {
	x = find(x); y = find(y);
	if(x == y) return;
	if(rank[x] < rank[y])	par[x] = y;
	else {
		par[y] = x;
		if(rank[x] == rank[y]) ++rank[x];
	}
}
bool same(int x, int y) {
	return find(x) == find(y);
}

int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	//ios_base::sync_with_stdio(0);

	scanf("%d%d", &n, &d);	d *= d;
	init(n);
	for(int i = 1; i <= n; ++i)
		scanf("%d%d", &pnt[i].first, &pnt[i].second);
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= n; ++j) {
			if(i == j) continue;
			int dis = (pnt[i].first - pnt[j].first) *
			          (pnt[i].first - pnt[j].first) +
			          (pnt[i].second - pnt[j].second) *
			          (pnt[i].second - pnt[j].second);
			if(dis <= d) edge[i].push_back(j);
		}
	}
	char op[2]; int x, y;
	while(scanf("%s", op) != EOF) {
		if(op[0] == 'O') {
			scanf("%d", &x);
			if(!repaired[x]) {
				repaired[x] = true;
				for(int i = 0; i < edge[x].size(); ++i) {
					if(repaired[edge[x][i]]) {
						unite(x, edge[x][i]);
					}

				}
				/*for(int i = 1; i <= n; ++i)
					cout << i << "->" << find(i) << endl;
				cout << endl; */
			}
		} else {
			scanf("%d%d", &x, &y);
			if(same(x, y)) printf("SUCCESS\n");
			else	printf("FAIL\n");
		}
	}
	return 0;
}



你可能感兴趣的:(set,poj,并查集,disjoint)