poj 2236(并查集的应用)
Posted flyer_duck
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2236(并查集的应用)相关的知识,希望对你有一定的参考价值。
Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 35832 | Accepted: 14863 |
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<cmath> 3 #include<cstring> 4 using namespace std; 5 const int maxn=1100; 6 bool flag[maxn]; 7 int fa[maxn],x[maxn],y[maxn]; 8 int n,d; 9 10 bool dis(int numa,int numb) 11 { 12 int opa,opb; 13 opa=x[numa]-x[numb]; 14 opb=y[numa]-y[numb]; 15 return opa*opa+opb*opb<=d*d; 16 } 17 18 int find_fa(int op) 19 { 20 if(op!=fa[op]) fa[op]=find_fa(fa[op]); 21 return fa[op]; ///此处有压缩 22 } 23 24 int main() 25 { 26 scanf("%d%d",&n,&d); 27 for(int i=1;i<=n;i++){ 28 fa[i]=i; 29 flag[i]=false; 30 } 31 for(int i=1;i<=n;i++) 32 scanf("%d%d",&x[i],&y[i]); 33 34 char order[3]; 35 int a,b; 36 while( ~scanf("%s%d",order,&a)){ 37 if(order[0]==‘O‘){ 38 flag[a]=true; 39 for(int i=1;i<=n;i++){ 40 if(i!=a&&flag[i]&&dis(i,a)){ 41 int iop,aop; 42 iop=find_fa(i); 43 aop=find_fa(a); 44 fa[aop]=iop; 45 } 46 } 47 } 48 else{ 49 scanf("%d",&b); 50 int aop,bop; 51 aop=find_fa(a); 52 bop=find_fa(b); 53 if(aop==bop) printf("SUCCESS\n"); 54 else printf("FAIL\n"); 55 } 56 } 57 return 0; 58 }
以上是关于poj 2236(并查集的应用)的主要内容,如果未能解决你的问题,请参考以下文章