【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. 

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

题目大意:

   先给出一些点(的坐标x和y),接下来有两种操作  'O'代表修复一个点。'S'代表测试当前情况下是否两者已经连上。有一个计算机网络的所有线路都坏了,网络中有n台计算机,现在你可以做两种操作,修理(O)和检测两台计算机是否连通(S),只有修理好的计算机才能连通。连通有个规则,两台计算机的距离不能超过给定的最大距离D(一开始会给你n台计算机的坐标)。检测的时候输出两台计算机是否能连通。

解题报告:

   不算难啊这题,但是要注意,你能并起来的前提是这个电脑已经被修好了,所以用ok[ top ]记录一下已经被修好的电脑的下标。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const double eps = 1e-6;
int ok[1000 + 5];//代表已经修好的点 存标号即可 然后再结构体查询
int f[1000 + 5];
int n,d;
struct Node {double x,y;} node[1000 + 5];
int getf(int v) {return v==f[v]?v:f[v]=getf(f[v]);}
void merge(int u,int v) {int t1=getf(u);int t2=getf(v);if(t1!=t2) {f[t2]=t1;}
}double dis(Node a,Node b)
{return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void init() {for(int i = 1; i<=n; i++) {f[i]=i;}
}
int main()
{int u,v;int top=0;char op[5];cin>>n>>d;init();for(int i = 1; i<=n; i++) {scanf("%lf %lf",&node[i].x,&node[i].y);}while(~scanf("%s",op) ) {if(op[0]=='O') {scanf("%d",&u);for(int i = 1; i<=top; i++) {//因为只能与修复的电脑进行连接 if(dis(node[ok[i] ] ,node[u] ) - d < eps) {merge(ok[i],u);}}	ok[++top] = u;	}else {scanf("%d %d",&u,&v);getf(u) == getf(v) ? printf("SUCCESS\n") : printf("FAIL\n");}}return 0 ; 
}

总结:

   这题有一个技巧,ok数组存的是结构体下标,而不是另开一个ok结构体,这增强了代码的可读性与节省了空间。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442070.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何使用微信公众平台测试号进行系统开发

申请一个测试号&#xff1a;入口修改测试公众号自定义菜单&#xff08;使用微信公众平台接口调试工具&#xff09;网站开发&#xff0c;进行部署网站测试

【POJ - 1751】Highways (最小生成树)

题干&#xff1a; The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the …

jupyter怎么安装jieba_AI工具:Anaconda中Jupyter不能import已安装module问题解决

jupyter模式下写代码时,通过pip install package命令行安装package完成之后,无法在jupyter模式下import &#xff0c;这是个通用的问题&#xff0c;我这里遇到的是import jieba&#xff0c;可能import 别的package也会出现&#xff0c;记录下&#xff0c;也花了点时间排查。。。…

Sql Server数据库设置一个账户只能看到一个数据库

1 新建登录名&#xff0c;注意不要设置用户映射&#xff0c;服务器角色只选择public&#xff08;默认必选&#xff0c;无法去掉&#xff0c;可以添加其他服务器角色&#xff0c;但是不要添加查看所有数据库的权限&#xff0c;接下来会去掉public的查看所有数据库权限&#xff0…

boot lib分离 spring_spring boot + gradle打包bootJar分离lib

以前项目打包一直是用的maven&#xff0c;最近新开一个项目&#xff0c;使用的是spring boot 2.11 gradle 4.10.3&#xff0c;在打包的时候分离lib折腾了好几天&#xff0c;网上找了很多方法都不成功&#xff0c;老是卡在configurations.compile这里&#xff0c;总是获取不到正…

【POJ - 3494】Largest Submatrix of All 1’s(加一点思维后化成 单调栈)

题干&#xff1a; Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements. Input The input contains multiple test cases. Each test case begins with m and n (1 ≤ m…

如何在修改计算机设置时,不再弹出提示框?

1 打开控制面板&#xff0c;找到安全与维护&#xff1b; 2 进入安全与维护&#xff0c;在安全中找到“用户账户控制UAC”&#xff0c;点击打开“更改设置” 3 进行设置&#xff0c;“从不通知”设置之后&#xff0c;针对计算机的所有更改将不再提示

doe五步法_DOE方法介绍

DOE, Design of Experiment。就是试验设计。想知道对于某个过程的产生影响的诸多因素中对输出结果影响最关键的因素有哪些&#xff0c;就可以用DOE方法来设计一系列试验&#xff0c;获知关键影响因素。通常的确定对输出变量最关键影响因子的试验做法有三种。1. 经验猜测法。首先…

【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树)

题干&#xff1a; Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a…

matlab两张图片合成一张_11. 图像合成与图像融合

本文同步发表在我的微信公众号“计算摄影学”&#xff0c;欢迎扫码关注【转载请注明来源和作者】我们终于进入了新的篇章。这一次我来给大家介绍一下图像合成与融合。我们经常看到一些很奇妙的PS技术&#xff0c;例如下面这张&#xff0c;它把1928年的一位叫做Frankie Yale的黑…

C#学习,Web界面打开winform程序

1 Web打开winform程序&#xff0c;需要使用的标签为a标签 <a href"SelfName://">连接提示</a> //SelfName 是自己的自定义协议2 Winform程序一般不需要做任何操作&#xff0c;如果需要参数传递的情况下&#xff0c;可以将Winform的Program.cs程序修…

【POJ - 2349】【UVA - 10369】 Arctic Network(最小生成树求权值第k大的边)(内附两种算法)

题干&#xff1a; The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver a…

asp.net MVC结合Blazor开发学习

1建立MVC项目&#xff08;.net 6&#xff09;; 2 在项目启动文件Program.cs中添加Blazor框架&#xff1b; var builder WebApplication.CreateBuilder(args);// Add services to the container. builder.Services.AddControllersWithViews(); builder.Services.AddServerSid…

澄海口袋机器人_汕头市澄海区在2019年汕头市中小学智能机器人竞赛上取得优异成绩...

4月14日&#xff0c;2019年汕头市中小学智能机器人竞赛在汕头市蓬鸥中学举行。本次活动由汕头市教育局和汕头市科协联合主办&#xff0c;全市93所学校的249个队伍503名选手报名参赛&#xff0c;大赛设“高铁时代”机器人现场拼装赛、丛林任务挑战赛、超级轨迹赛、综合技能比赛、…

【POJ - 1703】Find them, Catch them(带权并查集之--种类并查集 权为与父节点关系)

题干&#xff1a; Find them, Catch them Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 36176 Accepted: 11090 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Ga…

h5前端如何实现F11全屏功能

网站全部实现小demo&#xff1a;兼容谷歌和Edge {Layout null; }<!DOCTYPE html><html> <head><meta name"viewport" content"widthdevice-width" /><title>Index</title><script src"~/Content/jquery/jque…

于小c三国语言_云顶之弈:三国成最强打工羁绊 校长教学顺滑转九五

虎牙校长是九五阵容的专业户&#xff0c;虽然版本在不断地更新&#xff0c;云顶的整体走向也在不断地发生变化&#xff0c;但九五至尊的强度一直都是TOP0级别&#xff0c;一旦成型第四保底&#xff0c;吃鸡都变得轻而易举。而整套九五的运营关键在于如何连胜、保证自身血量健康…

【HDU - 3172】Virtual Friends(带权并查集--权为集合元素个数)

题干&#xff1a; These days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends friends, their friends friends friends, and …

java null转换jason_常见java问题及解决办法汇总(干货可收藏)

Java Exception&#xff1a;1、Error2、Runtime Exception 运行时异常3、Exception4、throw 用户自定义异常异常类分两大类型&#xff1a;Error类代表了编译和系统的错误&#xff0c;不允许捕获&#xff1b;Exception类代表了标准Java库方法所激发的异常。Exception类还包含运行…

asp.net 网站开发,word导出

曾经有一个word导出案例&#xff1a; web保存word资源 后来发现可以使用MVC的File方法保存成相应的文件&#xff0c;特别做个记录。 1 前端 //网页 <form method"post" action"/Home/WordExport" id"wordTmp"><input type"text&…