POJ: 2236 Wireless Network 题解
加工并储存数据的数据结构
并查集
这是并查集的基本应用,两台修好的电脑若距离d内则加入合并。不过不小心的话会TLE,比如:
#include <iostream>
using namespace std;
#define MAX_N 1001 + 16
int parent[MAX_N];
int height[MAX_N];
bool status[MAX_N];
int distance[MAX_N][MAX_N];
void init(const int& n)
{
for (int i = 0; i < n; ++i)
{
parent[i] = i;
height[i] = 0;
}
}
int find(const int& x)
{
if (parent[x] == x)
{
return x;
}
else
{
return parent[x] = find(parent[x]);
}
}
void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x == y)
{
return;
}
if (height[x] < height[y])
{
parent[x] = y;
}
else
{
parent[y] = x;
if (height[x] == height[y])
{
++height[x];
}
}
}
bool same(const int& x, const int& y)
{
return find(x) == find(y);
}
pair<int, int> computer[MAX_N];
int square(const int& x)
{
return x * x;
}
int main(int argc, char *argv[])
{
int N, d;
cin >> N >> d;
for (int i = 0; i < N; ++i)
{
cin >> computer[i].first >> computer[i].second;
}
init(N);
char operation;
int x, y;
while (cin >> operation)
{
if (operation == 'O')
{
cin >> x;
--x;
status[x] = true;
for (int i = 0; i < N; ++i)
{
if (i == x)
{
continue;
}
if (status[i] && square(computer[x].first - computer[i].first) + square(computer[x].second - computer[i].second) <= square(d))
{
unite(x, i);
}
}
}
else
{
cin >> x >> y;
--x; --y;
if (same(x, y))
{
cout << "SUCCESS" << endl;
}
else
{
cout << "FAIL" << endl;
}
}
}
return 0;
}
平方计算太多了,初始化的时候算一次记录在一个二维数组中就够了。
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define ms(a,b) memset(a,b,sizeof(a));
const int maxn = 1010;
int f[maxn];
int h[maxn];
pair<int, int>dis[maxn];
bool status[maxn];//电脑是否维修好了
bool able[maxn][maxn];//distance
void init() {
for (int i = 0; i < maxn; ++i) f[i] = i, h[i] = 0;
}
int find(int x) {
return f[x] == x ? x : f[x] = find(f[x]);
}
void merge(int x, int y) {
x = find(x);
y = find(y);
if (x == y)return;
if (h[x] < h[y])f[x] = y;
else {
f[y] = x;
if (h[x] == h[y])++h[x];
}
}
bool same(int a, int b) {
return find(a) == find(b);
}
int square(int x) {
return x * x;
}
int main() {
int N, d;
cin >> N >> d;
for (int i = 0; i < N; ++i)cin >> dis[i].first >> dis[i].second;
init();
for (int i = 0; i < N; ++i)for (int x = i; x < N; ++x)
if (square(dis[x].first - dis[i].first) + square(dis[x].second - dis[i].second) <= square(d))able[i][x] = able[x][i] = true;
char operation;
int x, y;
while (cin >> operation) {
if (operation == 'O') {
cin >> x; --x;
status[x] = true;
for (int i = 0; i < N; ++i){
if (i == x) continue;
if (status[i] && able[x][i]) merge(x, i);
}
}
else {
cin >> x >> y;
--x, --y;
if(same(x,y))cout << "SUCCESS" << endl;
else cout << "FAIL" << endl;
}
}
return 0;
}
POJ: 2236 Wireless Network 题解的更多相关文章
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- [并查集] POJ 2236 Wireless Network
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 25022 Accepted: 103 ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
- POJ 2236 Wireless Network (并查集)
Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...
- POJ 2236 Wireless Network(并查集)
传送门 Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 24513 Accepted ...
- POJ 2236 Wireless Network (并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 18066 Accepted: 761 ...
- poj 2236 Wireless Network 【并查集】
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16832 Accepted: 706 ...
- POJ 2236 Wireless Network [并查集+几何坐标 ]
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...
- poj 2236 Wireless Network (并查集)
链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network
题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p 代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...
随机推荐
- Centos 7在线安装JDK1.8+Tomcat+MySQL8.0+Nginx
一.安装JDK 注: 以下命令环境在Xshell中进行. 温馨提示: 在线安装方式需要有网速的前提,有的软件下载需要大量时间. 1.查询出系统自带的OpenJDK及版本 rpm -qa | grep ...
- Mock基础知识
使用的框架:moco框架下载地址:https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/1.1.0/ 启动方式:java -j ...
- 数据库的连接用Java
第一步注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); 第二步创建用户密码,和具体的url static String name = ...
- [2020-2021 集训队作业] Tom & Jerry
题目背景 自选题 by ix35 题目描述 给定一张包含 \(n\) 个顶点和 \(m\) 条边的 无向连通图,Tom 和 Jerry 在图上进行了 \(q\) 次追逐游戏. 在第 \(i\) 次游戏 ...
- 怎么在Android项目中导入ffmpeg库?
1.前言 在这里我以导入静态库(.a)为例进行分析,动态库(.so)是类似的.在导入前,各位要先编译好ffmpeg库,需要注意的是在编译的时候要开启交叉编译,目标平台为Android,其他平台的库(w ...
- 关于C#接口的用法详细解答,附上案例说明!
接口 C#中的接口是一种定义了一组方法.属性和事件的类型.它只包含成员的声明,而不包含任何实现.接口可以被类通过实现的方式使用,从而使类能够具有接口定义的行为. 接口在C#中被定义为使用interfa ...
- springboot——yaml格式
- SpringBoot-Validate优雅的实现参数校验,详细示例~
1.是什么? 它简化了 Java Bean Validation 的集成.Java Bean Validation 通过 JSR 380,也称为 Bean Validation 2.0,是一种标准化的 ...
- 【.NET】控制台应用程序的各种交互玩法
老周是一个不喜欢做界面的码农,所以很多时候能用控制台交互就用控制台交互,既方便又占资源少.有大伙伴可能会说,控制台全靠打字,不好交互.那不一定的,像一些选项类的交互,可以用键盘按键(如方向键),可比用 ...
- libgdx摄像头的移动
要知道,做一个游戏,摄像头是必不可少的.接下来,我将讲解libgdx里面摄像头的移动 2d摄像头OrthographicCamera也叫做正交相机 结果展示: 按上下左右是可以移动的 Orthogra ...