poj 2236 Wireless Network(并查集模板题)

来源:互联网 发布:神界:原罪2 优化怎么样 编辑:程序博客网 时间:2024/05/04 18:09
Wireless Network
Time Limit: 10000MS Memory Limit: 65536KTotal Submissions: 17515 Accepted: 7382

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

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<algorithm>#include<cstdlib>#include<set>#include<queue>#include<stack>#include<vector>#include<map>#define N 1010#define Mod 10000007#define lson l,mid,idx<<1#define rson mid+1,r,idx<<1|1#define lc idx<<1#define rc idx<<1|1const double EPS = 1e-11;const double PI = acos ( -1.0 );const double E = 2.718281828;typedef long long ll;const int INF = 1000010;using namespace std;int n,d;int X[N],Y[N];int par[N];int rank[N];bool rep[N];//标记是否修复void init() {    for(int i=0; i<n; i++)        par[i]=i,rank[i]=0;}bool OK(int fir,int sec) {    return (X[fir]-X[sec])*(X[fir]-X[sec])+(Y[fir]-Y[sec])*(Y[fir]-Y[sec])<=d*d;}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);}void Int(int x) {    for(int i=1; i<=n; i++) {        if(i==x||!rep[i])continue;        if(!OK(i,x)) continue;        unite(i,x);    }    rep[x]=true;}int main() {    scanf("%d%d",&n,&d);    for(int i=1; i<=n; i++)        scanf("%d%d",&X[i],&Y[i]);    char s[2];    int id,id2;    init();    memset(rep,false,sizeof rep);    while(scanf("%s",s)!=EOF) {        if(s[0]=='O') {            scanf("%d",&id);            Int(id);        } else {            scanf("%d%d",&id,&id2);            if(same(id,id2))printf("SUCCESS\n");            else   printf("FAIL\n");        }    }    return 0;}


0 0
原创粉丝点击