POJ-2236 Wireless Network
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
------AC------
用pos[][]记录坐标,book[]表示修复的计算机,fa[]建立树
1 #include<iostream> 2 #include<cstring> 3 4 using namespace std; 5 6 int book[1050],pos[1050][5],fa[1050],d; 7 8 int Find(int x) 9 { 10 if(fa[x]==x) 11 return x; 12 return fa[x]=Find(fa[x]); 13 } 14 15 void Mix(int x,int y) 16 { 17 int fx=Find(x),fy=Find(y); 18 if(fx>fy) 19 swap(fx,fy); 20 if(fx!=fy) 21 fa[fy]=fx; 22 } 23 24 bool dis(int x1,int y1,int x2,int y2) 25 { 26 int len=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); 27 if(len<=d*d) 28 return true; 29 return false; 30 } 31 32 int main() 33 { 34 int n,i,j; 35 memset(book,0,sizeof(book)); 36 memset(pos,0,sizeof(pos)); 37 for(i=0;i<1005;++i) 38 fa[i]=i; 39 40 cin>>n>>d; 41 //从第一台计算机开始记录比较方便,不然下面修复计算机时会混乱修复哪一台计算机 42 for(i=1;i<=n;++i) 43 cin>>pos[i][0]>>pos[i][1]; 44 45 char ch; 46 int x1,x2,num=0; 47 //用C要注意回车键 48 while(cin>>ch>>x1) 49 { 50 if(ch=='O') 51 { 52 for(i=0;i<num;++i) 53 { 54 // printf("%d %d %d \n",x1,book[i],dis(pos[book[i]][0],pos[book[i]][1],pos[x1][0],pos[x1][1])); 55 if(dis(pos[book[i]][0],pos[book[i]][1],pos[x1][0],pos[x1][1])) 56 Mix(x1,book[i]); 57 } 58 book[num++]=x1; 59 60 // for(i=0;i<num;++i) 61 // printf("%d ",book[i]); 62 // printf("\n"); 63 } 64 if(ch=='S') 65 { 66 cin>>x2; 67 //这里是重点,用上面的方法将两棵树相连接,只有根节点的父亲改变,子节点的祖宗不变,所以要找真正的祖宗 68 while(fa[x1]!=x1) 69 x1=fa[x1]; 70 while(fa[x2]!=x2) 71 x2=fa[x2]; 72 73 if(x1==x2) 74 cout<<"SUCCESS"<<endl; 75 else 76 cout<<"FAIL"<<endl; 77 } 78 79 // for(i=0;i<10;++i) 80 // cout<<fa[i]<<" "; 81 // cout<<endl; 82 } 83 return 0; 84 }
现在所有的不幸都是以前不努力造成的。。。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!