poj2236(并查集)

来源:互联网 发布:jquery传json数据 编辑:程序博客网 时间:2024/06/08 09:54

Wireless Network
Time Limit: 10000MS Memory Limit: 65536KTotal Submissions: 29621 Accepted: 12298

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

用并查集可以轻松解决,要注意的是,每修好一台电脑,要和所有已知的电脑来进行比较,对在其范围内好所有的电脑进行合并。




//用并查集可以轻松解决,要注意的是,每修好一台电脑,要和所有已知的电脑来进行比较,对在其范围内好所有的电脑进行合并。#include<iostream>#include<stdio.h>#include<string.h>using namespace std;#define MAX_N 1010int n,d;int par[MAX_N];int rank[MAX_N];int point[MAX_N][2];bool okay[MAX_N];void init(int n)    //初始化 {for(int i=0;i<n;i++){par[i]=i;rank[i]=0;okay[i]=false;}}int find(int x)    //查询树根 {if(par[x]==x)return x;return par[x]=find(par[x]);}void unite(int x,int y)       //合并x,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)     //判断x,y是否在同一集合 {return find(x)==find(y);}int gougu(int x,int y)    //求出两点距离平方 {int a=point[x][0]-point[y][0];int b=point[x][1]-point[y][1];return a*a+b*b;}int main(){cin>>n>>d;init(n);for(int i=0;i<n;i++)cin>>point[i][0]>>point[i][1];char op;int x,y;while(cin>>op){if(op=='O'){cin>>x;x--;okay[x]=true;for(int i=0;i<n;i++){if(okay[i]&&gougu(i,x)<=d*d){unite(i,x);}}}else if(op=='S'){cin>>x>>y;x--;y--;if(same(x,y))cout<<"SUCCESS"<<endl;else cout<<"FAIL"<<endl;}}}



原创粉丝点击