博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ -2236 Wireless Network
阅读量:5099 次
发布时间:2019-06-13

本文共 4071 字,大约阅读时间需要 13 分钟。

Wireless Network
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 12151   Accepted: 5129

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 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4

Sample Output

FAILSUCCESS代码如下:
/*解题思路:	并查集的简单应用,对每次修好的电脑对其它已经修好的电脑遍历,	如果距离小于等于最大通信距离就将他们合并。之后判断2台电脑是不是一个集合中就KO了*//*代码一:自己的代码#include
#include
#include
int point[1002][2],root[1002];bool dis[1002][1002],work[1002];int cal(int x1,int y1,int x2,int y2){ return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);}int find_root(int i){ while(root[i]>0) i=root[i]; return i; //return root[i]<0?i:find_root(root[i]);}void merge(int i,int j){ i=find_root(i); j=find_root(j); if(i!=j) { if(root[i]
>n>>d) { memset(work,false,sizeof(work)); memset(root,-1,sizeof(root)); for(i=1;i<=n;++i) cin>>point[i][0]>>point[i][1]; for(i=1;i<=n;++i) for(j=1;j<=i;++j) { int t=cal(point[i][0],point[i][1],point[j][0],point[j][1]); if(t<=d*d) dis[i][j]=dis[j][i]=true; else dis[i][j]=dis[j][i]=false; } while(cin>>s) { if(s=='O') { cin>>a; work[a]=true; for(i=1;i<=n;++i) { if(i!=a&&work[i]&&dis[i][a]) merge(i,a); } } else if(s=='S') { cin>>a>>b; if(find_root(a)==find_root(b)) cout<<"SUCCESS"<
#include
#include
#include
#include
using namespace std;#define N 1110int d;bool use[N];struct node{ int pre; int x, y;}p[N];int find(int x){ return x == p[x].pre ? x : find(p[x].pre);}void join(const node p1, const node p2){ int root1, root2; root1 = find(p1.pre); root2 = find(p2.pre); if(root1 != root2) if((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y) <= d * d) p[root2].pre = root1;}int main(){ //freopen("Input.txt", "r", stdin); int num; char ope; int ok; int from, to; scanf("%d%d", &num, &d); for(int i = 1; i <= num; ++i) p[i].pre = i; memset(use, false, sizeof(use)); for(int i = 1; i <= num; ++i) scanf("%d%d", &p[i].x, &p[i].y); while(scanf("\n%c", &ope) != EOF) { if(ope == 'O') { scanf("%d", &ok); use[ok] = true; for(int i = 1; i <= num; ++i) if(use[i] && i != ok) join(p[i], p[ok]); } else { scanf("%d%d", &from, &to); if(find(from) == find(to)) printf("SUCCESS\n"); else printf("FAIL\n"); } } return 0;}

 

转载于:https://www.cnblogs.com/dongsheng/archive/2012/07/21/2602065.html

你可能感兴趣的文章
Hdu 2069 Coin Change
查看>>
Python网络编程(socket模块、缓冲区、http协议)
查看>>
开博留念
查看>>
四重解法---P1047 校门外的树
查看>>
大马猴队-Alpha阶段项目复审
查看>>
集群时间同步
查看>>
Ubuntu16.04 + Win 10 双系统 时间同步,启动项顺序,NumLock指示灯常亮
查看>>
win10桌面显示我的电脑设置
查看>>
VxWorks各部分初始化流程 分类: vxWorks ...
查看>>
给你90天,成为不一样的自己!
查看>>
python版本下载时时,官方目录web-based与executable和embeddable 的区别
查看>>
Java程序单元测试工具对比——Parasoft Jtest与Junit
查看>>
js/jquery中什么时候用return,什么时候用return false
查看>>
Android开发 MVP模式的规范记录(个人总结)
查看>>
hadoop2.2使用手册2:如何运行自带wordcount
查看>>
ByteArrary(优化数据存储和数据流)
查看>>
Unity3D脚本中文系列教程(十五)
查看>>
从你的全世界路过-孤独的放逐
查看>>
java 实现多线程 3种方式
查看>>
<s:fielderror/>使用
查看>>