POJ ---2236 Wireless Network[简单并查集]

Wireless Network
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 12427   Accepted: 5236

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 1

0 1

0 2

0 3

0 4

O 1

O 2

O 4

S 1 4

O 3

S 1 4

Sample Output

FAIL

SUCCESS

Source

 
 
 
 
 
 
 
 
题目要输出的是FAIL,我写成了FALL,好蠢啊!!!WA了N次,太粗心了!以后要养成好习惯,像输出的格式问题都直接去OUTPUT里拷贝,不要自作聪明去自己写!!!囧,浪费了好多时间!!!
code:
  1 #include <iostream>   

  2 #include <iomanip>   

  3 #include <fstream>   

  4 #include <sstream>   

  5 #include <algorithm>   

  6 #include <string>   

  7 #include <set>   

  8 #include <utility>   

  9 #include <queue>   

 10 #include <stack>   

 11 #include <list>   

 12 #include <vector>   

 13 #include <cstdio>   

 14 #include <cstdlib>   

 15 #include <cstring>   

 16 #include <cmath>   

 17 #include <ctime>   

 18 #include <ctype.h> 

 19 using namespace std;

 20 

 21 #define MAXN 1005

 22 

 23 int father[MAXN];

 24 bool vst[MAXN];

 25 int  n;

 26 double d;

 27 

 28 typedef struct point

 29 {

 30     int x,y;

 31 }Point;

 32 Point point[MAXN];

 33 

 34 bool fun(int x1,int y1,int x2,int y2)

 35 {

 36     if(sqrt(double((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)))<=d)

 37         return true;

 38     else

 39         return false;

 40 }

 41 

 42 void init()

 43 {

 44     for(int i=0;i<=n;i++)

 45     {

 46         father[i]=i;

 47         vst[i]=false;

 48     }

 49 }

 50 

 51 int findset(int v)

 52 {

 53     if(v==father[v])

 54         return v;

 55     else

 56         father[v]=findset(father[v]);

 57     return father[v];

 58 }

 59 

 60 void Union(int a,int b)

 61 {

 62     a=findset(a);

 63     b=findset(b);

 64     if(a!=b)

 65         father[a]=b;

 66 }

 67 

 68 int main()

 69 {

 70     int i,j;

 71     char ch;

 72     int p,q;

 73     scanf("%d%lf",&n,&d);

 74     init();

 75     for(i=1;i<=n;i++)

 76         scanf("%d%d",&point[i].x,&point[i].y);

 77     getchar();

 78     while(~scanf("%c",&ch))

 79     {

 80         if(ch=='O')

 81         {

 82             scanf("%d",&p);

 83             vst[p]=true;

 84             for(i=1;i<=n;i++)

 85             {

 86                 if(fun(point[p].x,point[p].y,point[i].x,point[i].y)&&vst[i]&&p!=i)

 87                     Union(p,i);

 88             }

 89         }

 90         else

 91         {

 92             scanf("%d%d",&p,&q);

 93             int temp1,temp2;

 94             temp1=findset(p);

 95             temp2=findset(q);

 96             if(temp1!=temp2)

 97                 printf("FAIL\n");

 98             else

 99                 printf("SUCCESS\n");

100         }

101         getchar();

102     }

103     return 0;

104 }

 

 

你可能感兴趣的:(NetWork)