poj2236——Wireless Network(并查集)
来源:互联网 发布:php x86 x64 编辑:程序博客网 时间:2024/05/16 17:32
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
考察并查集以及如何修改并查集
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;int f[1010],vaild[1010],x[1010],y[1010],d;int set_find(int i){ if(i!=f[i]) { i=set_find(f[i]); } return f[i];}bool dis(int i,int p){ int xx=x[i]-x[p],yy=y[i]-y[p]; if((xx*xx+yy*yy)<=d*d) return true; else return false;}int main(){ int n,i,p,q,k1,k2; char ch; scanf("%d%d",&n,&d); for(i=1;i<=n;++i) { f[i]=i; vaild[i]=false; } for(i=1;i<=n;++i) scanf("%d%d",&x[i],&y[i]); while(cin>>ch) { if(ch=='O') { scanf("%d",&p); vaild[p]=true; for(i=1;i<=n;++i) { if(vaild[i]&&dis(i,p)&&i!=p) { k1=set_find(i); k2=set_find(p); f[k1]=k2; } } } if(ch=='S') { scanf("%d%d",&p,&q); p=set_find(p); q=set_find(q); if(p==q) printf("SUCCESS\n"); else printf("FAIL\n"); } } return 0;}
- poj2236——Wireless Network(并查集)
- POJ2236 Wireless Network(并查集)
- POJ2236 Wireless Network(并查集)
- poj2236 Wireless Network(并查集)
- POJ2236 Wireless Network(并查集)
- POJ2236 Wireless Network(并查集)
- POJ2236——Wireless Network 并查集应用
- POJ2236 Wireless Network [并查集]
- poj2236 Wireless Network 并查集
- POJ2236 Wireless Network 【并查集】
- POJ2236 Wireless Network 并查集
- poj2236 Wireless Network(并查集)
- POJ2236 Wireless Network 并查集
- [并查集]poj2236 Wireless Network
- POJ2236,Wireless Network,并查集
- [并查集] poj2236 Wireless Network
- POJ2236-Wireless Network-并查集
- POJ2236 Wireless Network(并查集)
- java中类,对象,方法的理解
- Android利用AccessibilityService实现自动装总结(一)
- innotop监控mysql
- 关于iframe高度自适应的问题
- javaweb之----http协议详解
- poj2236——Wireless Network(并查集)
- iOS个人整理16-标签视图控制器--UITabBarController
- 第三章Spring的Hello World
- Stanford机器学习---第六周.学习曲线、机器学习系统的设计
- UIImagePickerController
- Jquery Mobile dialog的生命周期
- 递归-----Search Range in Binary Search Tree
- Python特殊语法:filter、map、reduce、lambda
- linux常用命令