2016 小灶1 K (poj2236)

来源:互联网 发布:万方数据库免费入口 编辑:程序博客网 时间:2024/04/29 16:34

题目:

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

题意:n台电脑,O表示修复一台电脑,并且将其与在一定距离内的电脑连接起来。若A.B连接,B.C连接则视为A,C已经连接,S表示检查这两台电脑是否连接起来。


思路:并查集

源代码:

//By Sean Chen#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>using namespace std;struct p{    int x,y,con;};double dis[1005][1005];p computer[1005];int n,Maxdis;int root[1005],depth[1005];char operate[10];int A,B;int findroot(int x)    //寻找并查集的根{    if (x==root[x])        return x;    else        return findroot(root[x]);}void combine(int xx,int yy)   //合并{    int x=findroot(xx);    int y=findroot(yy);    if (x==y)        return;    if (depth[x]>depth[y])        root[y]=x;    else if (depth[x]<depth[y])        root[x]=y;    else if (depth[x]==depth[y])    {        root[y]=x;        depth[x]++;    }    return;}void unite(int a){    for (int i=1;i<=n;i++)    {        if (i!=a && computer[i].con && dis[a][i]<=double(Maxdis))   //寻找该点可以与哪个点所在的并查集合并        {            combine(a,i);        }    }    return;}int main(){    scanf("%d%d",&n,&Maxdis);    for (int i=1;i<=n;i++)    {        root[i]=i;        scanf("%d%d",&computer[i].x,&computer[i].y);        computer[i].con=0;        depth[i]=0;        for (int j=1;j<i;j++)        {            dis[i][j]=sqrt((computer[i].x-computer[j].x)*(computer[i].x-computer[j].x)+(computer[i].y-computer[j].y)*(computer[i].y-computer[j].y));            dis[j][i]=dis[i][j];        }        dis[i][i]=0;    }    while (scanf("%s",operate)!=EOF)    {        if (operate[0]=='O')        {            scanf("%d",&A);            computer[A].con=1;            unite(A);        }        if (operate[0]=='S')        {            scanf("%d%d",&A,&B);            int aa=findroot(A),bb=findroot(B);            if (aa==bb)                printf("SUCCESS\n");            else                printf("FAIL\n");        }    }    return 0;}


0 0
原创粉丝点击