Fix the computers(解题报告)

来源:互联网 发布:阿里云账号登录 编辑:程序博客网 时间:2024/05/22 07:55

                                                                                      Fix the computers

                                  Time Limit:10000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u


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解题思想:本题主要考察并查集,大意为n台电脑,接下来输入n行,每行代表电脑的坐标,n+1行开始O打头代表修复某台电脑,S打头代表问两台电脑在d距离内是否能修好。          数据输入时的处理颇为奇葩,用了一次循环,每次输入二维数组的两列;然后进入while循环,判断开头第一个字符是O还是S,若是O,则输入其中一台电脑,把          它标记下,接下来循环遍历,如果满足被标记过(被修好了)并且距离小于d,建立起联系;若是S,则输入两个数,判断这两个数是否建立起联系。解题代码:
#include <stdio.h>#include <string.h>int s[1111][2];int v[1111];int f[1111];void init(int n){    int i,j;    for(i=1;i<=n;i++)    {        f[i] = i;    }}int find(int x){    if(f[x]==x)        return x;    return f[x]=find(f[x]);}int is_friend(int a,int b){    int x=find(a);    int y=find(b);    if(x==y)        return 1;    else        return 0;}void set_friend(int a,int b){    int x=find(a);    int y=find(b);    if(x!=y)        f[y]=x;}int dis(int a,int b){    return ((s[a][0]-s[b][0])*(s[a][0]-s[b][0]) + (s[a][1]-s[b][1])*(s[a][1]-s[b][1]));}int main(){    int n,d,i,j,t1,t2,t3;    char ch[5];    while(scanf("%d%d",&n,&d)!=EOF)    {        init(n);        memset(v,0,sizeof(v));        d *= d;        for(i=1;i<=n;i++)            scanf("%d%d",&s[i][0],&s[i][1]);        while(scanf("%s",ch)!=EOF)        {            if(ch[0]=='O')            {                scanf("%d",&t1);                v[t1] = 1;                for ( i=1;i<=n ;i++ )                {                    if(v[i]==1 && dis(i,t1)<=d)                    {                        set_friend(t1,i);                    }                }            }            if(ch[0]=='S')            {                scanf("%d%d",&t2,&t3);                if(is_friend(t2,t3)==1)                    printf("SUCCESS\n");                else if(is_friend(t2,t3)==0)                    printf("FAIL\n");            }        }    }}

0 0