POJ2236Wireless Network(并查集模板)

来源:互联网 发布:linux 搜索文件 编辑:程序博客网 时间:2024/06/05 04:50
Wireless Network
Time Limit: 10000MS Memory Limit: 65536KTotal Submissions: 27117 Accepted: 11218

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 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4

Sample Output

FAILSUCCESS

Source

POJ Monthly,HQM

题目大意:有n个电脑损坏,同时每个电脑都有一个坐标,当电脑修好之后,可以与跟他距离小于等于d的修好的电脑连接,然后有无数次操作1.修复

2.查询两台电脑是否可以连接

解题思路:这是一道并查集,每次修好一台电脑后,遍历一下所有电脑看看是否有跟他距离小于等于d的电脑,有的话就把他们放到一个集合之中意味着他们能够连接,然后查询的时候判断一下是否他们属于一个集合

#include<iostream>    #include<cstdio>  #include<stdio.h>  #include<cstring>    #include<cstdio>    #include<climits>    #include<cmath>   #include<vector>  #include <bitset>  #include<algorithm>    #include <queue>  #include<map>  using namespace std;struct point{long long int x, y;}p[2010];long long int a[2010];bool check[2010];int n, k;void init(){int i;for (i = 1; i <= n; i++){a[i] = i;}}int Tfind(int x){if (a[x] == x)return x;a[x] = Tfind(a[x]);return a[x];}void join(int x, int y){int t1, t2;t1 = Tfind(x);t2 = Tfind(y);if (t1 != t2){a[t2] = t1;}}int main(){char x;int i, test, test1, test2;//scanf("%d %d", &n, &k);cin >> n >> k;for (i = 1; i <= n; i++){//scanf("%d %d", &p[i].x, &p[i].y);cin >> p[i].x >> p[i].y;}init();memset(check, false, sizeof(check));while (cin>>x){if (x == 'O'){//scanf("%d", &test);cin >> test;check[test] = true;for (i = 1; i <= n; i++){if (i != test&&check[i] == true){if((p[i].x-p[test].x)*(p[i].x - p[test].x)+(p[i].y-p[test].y)*(p[i].y - p[test].y)<=k*k)join(i,test);}}}else{//scanf("%d %d", &test1, &test2);cin >> test1 >> test2;if (Tfind(test1) == Tfind(test2)){cout << "SUCCESS" << endl;}else{cout << "FAIL" << endl;}}}}


0 0
原创粉丝点击