Time Limit: 10000MS Memory Limit: 65536K
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.
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.
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
并查集的应用。
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 struct type{ 5 int x,y;//电脑的坐标 6 bool state;//电脑是否已修复,已修复为1,未修复为0 7 }c[1005]; 8 int par[1005],rank[1005],n,d; 9 void init(int n) 10 { 11 for(int i=1;i<=n;i++){ 12 par[i]=i,rank[i]=0; 13 c[i].state=0; 14 } 15 } 16 int find(int x) 17 { 18 if(par[x] == x) return x; 19 else return( par[x] = find(par[x]) ); 20 } 21 void unite(int x,int y) 22 { 23 x=find(x),y=find(y); 24 if(x == y) return; 25 if(rank[x] < rank[y]) par[x]=y; 26 else 27 { 28 par[y]=x; 29 if(rank[x] == rank[y]) rank[x]++; 30 } 31 } 32 double dist2(type a,type b)//返回两台电脑的距离的平方 33 { 34 return( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ); 35 } 36 bool same(int x,int y){return( find(x) == find(y) );} 37 int main() 38 { 39 char op; 40 scanf("%d%d",&n,&d); 41 init(n);//初始化n台电脑 42 for(int i=1;i<=n;i++) scanf("%d%d",&c[i].x,&c[i].y); 43 while(cin>>op) 44 { 45 if(op == 'O') 46 { 47 int Num; 48 scanf("%d",&Num); 49 c[Num].state=1; 50 for(int i=1;i<=n;i++)//遍历每台电脑,对于某台电脑c[i]…… 51 { 52 if(i == Num) continue; 53 if(c[i].state && dist2(c[i],c[Num]) <= d*d) unite(i,Num);//如果c[i]是已修复的,并且与当前修复这台电脑c[Num]距离小于d,就归入一组 54 } 55 } 56 else if(op == 'S') 57 { 58 int test1,test2; 59 scanf("%d%d",&test1,&test2); 60 if(same(test1,test2)) printf("SUCCESS ");//在同一组内,就可以成功通信 61 else printf("FAIL ");//否则就无法通信 62 } 63 } 64 }