POJ 2236 Wireless Network
人生中的第一个并查集!!
建树的时候虽然什么优化都没用。。。。 结果1S就过了
Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 13429 | Accepted: 5687 |
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 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4
Sample Output
FAILSUCCESS
Source
POJ Monthly,HQM
1 #include <cstdio> 2 #include <cstring> 3 4 using namespace std; 5 6 int N,D; 7 int djs[1010]; 8 9 struct Point 10 { 11 int x,y; 12 int status; 13 }P[1010]; 14 15 int dist(int a,int b,int d) 16 { 17 int s=(P[a].x-P[b].x)*(P[a].x-P[b].x)+(P[a].y-P[b].y)*(P[a].y-P[b].y); 18 if(s<=d*d) 19 return 1; 20 else 21 return 0; 22 } 23 24 void UF_init() 25 { 26 memset(djs,-1,sizeof(djs)); 27 } 28 29 int UF_find(int*tree,int x) 30 { 31 if(tree[x]<0) return x; 32 else 33 return UF_find(tree,tree[x]); 34 } 35 36 void UF_union(int* tree,int x,int y) 37 { 38 int rx,ry; 39 rx=UF_find(tree,x); 40 ry=UF_find(tree,y); 41 42 if(rx==ry) return ; 43 else 44 { 45 tree[ry]=rx; 46 } 47 } 48 49 50 51 int main() 52 { 53 scanf("%d%d",&N,&D); 54 for(int i=1;i<=N;i++) 55 { 56 scanf("%d%d",&P[i].x,&P[i].y); 57 P[i].status=0; 58 } 59 60 UF_init(); 61 62 char c; 63 while(scanf("%c",&c)!=EOF) 64 { 65 if(c=='O') 66 { 67 int a; 68 scanf("%d",&a); 69 P[a].status=1; 70 for(int i=1;i<=N;i++) 71 if(i!=a&&P[i].status&&dist(a,i,D)) 72 { 73 UF_union(djs,i,a); 74 } 75 76 } 77 else if(c=='S') 78 { 79 int a,b; 80 scanf("%d%d",&a,&b); 81 int ra=UF_find(djs,a); 82 int rb=UF_find(djs,b); 83 84 puts(ra==rb?"SUCCESS":"FAIL"); 85 } 86 } 87 88 return 0; 89 }