并查集-poj2236

来源:互联网 发布:战国之道网络连接失败 编辑:程序博客网 时间:2024/05/29 02:11
Language:
Wireless Network
Time Limit: 10000MS Memory Limit: 65536KTotal Submissions: 14251 Accepted: 6028

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题意:由于某些原因所有电脑出现故障,现在进行修复,修复后在距离D范围内的电脑可以通信,现给出电脑的坐标,然后给出修复顺序询问两台电脑之间能否进行通讯。思路:并查集,根据修复,若距离在D内则进行合并操作。对于询问,只需要判断是否在一个集合里即可。下面是代码:
#include<iostream>#include<set>#include<map>#include<vector>#include<queue>#include<cmath>#include<climits>#include<cstdio>#include<string>#include<cstring>#include<algorithm>typedef long long LL;using namespace std;struct node{    int x,y;}comp[1010];int N,D;int repair[1010],num;int par[1010],rank[1010];void init(){    for(int i=0;i<=N;i++)    {        par[i]=i;        rank[i]=0;    }    memset(repair,-1,sizeof(repair));    num=0;}int find1(int x){    if(par[x]==x)        return x;    else return par[x]=find1(par[x]);}void unite(int a,int b){    int x=find1(a);    int y=find1(b);    if(x==y)        return;    if(rank[x]<rank[y])        par[x]=y;    else    {        par[y]=x;        if(rank[x]==rank[y])            rank[x]++;    }}bool dis(int i,int j){    double dist=(comp[i].x-comp[j].x)*(comp[i].x-comp[j].x)+(comp[i].y-comp[j].y)*(comp[i].y-comp[j].y);    dist=sqrt(dist);    return dist<=D;}void is_link(int x){    for(int i=0;i<num;i++)    {        if(dis(repair[i],x))        {            unite(repair[i],x);            //return;        }    }}int main(){    //freopen("in.txt","r",stdin);    char t;    int a,b;    scanf("%d%d",&N,&D);    for(int i=1;i<=N;i++)        scanf("%d%d",&comp[i].x,&comp[i].y);    init();    getchar();    while(scanf("%c",&t)!=EOF)    {        getchar();        if(t=='O')        {            scanf("%d",&a);            repair[num]=a;            is_link(a);            num++;        }        else        {            scanf("%d%d",&a,&b);            if(find1(a)==find1(b))                cout<<"SUCCESS"<<endl;            else cout<<"FAIL"<<endl;        }    }    return 0;}