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之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...
随机推荐
- Java——面向对象(static关键字开始)
一.static 可以修饰成员变量和成员方法 关键字特点: 随着类的加载而加载: 优先于对象存在: 被类的所有对象共享: 可以通过类名直接调用: 注意事项: 在静态方法中是没有this关键字的 静态的 ...
- 浏览器跨 Tab 窗口通信原理及应用实践
最近,相信大家一定被这么个动效给刷屏了: 以至于,基于这个效果的二次创作层出不穷,眼花缭乱. 基于跨窗口通信的弹弹球: 基于跨窗口通信的 Flippy Bird: 我也尝试制作了一个跨 Tab 窗口的 ...
- vscode设置将英文界面设置为中文?
如果您希望将 Visual Studio Code(以下简称VSCode)的界面从英文设置为中文,可以按照以下步骤进行: 打开 VSCode 编辑器,并进入"扩展"菜单. 在搜索框 ...
- .NET8 依赖注入
依赖注入(Dependency Injection,简称DI)是一种设计模式,用于解耦组件(服务)之间的依赖关系.它通过将依赖关系的创建和管理交给外部容器来实现,而不是在组件(服务)内部直接创建依赖对 ...
- VM离线安装Centos 8以及配置
一.安装 1.预装准备 1.1. 硬件准备 物理内存:2G以上(这里指系统搭建所需占用空间) 物理外存:20G(这里指系统搭建所需占用空间) 1.2. 环境搭建准备 Window10系统电脑一台.Vm ...
- IDEA提示Cannot resolve symbol 'String'
一.解决方案: 1.问题原因: 系统提示Cannot resolve symbol 'String',是由于没有正确导入JDK : 2.解决方案: 在project SDK中正确配置即可. 二.完成. ...
- 初始OpenGL
OpenGL到底是什么? 一般它被认为是一个API,包含一系列操作图形,图像的函数.然而,它并不是一个API,而是Khronos组织制定并维护的规范. OpenGL规定了每个函数如何执行,以及它们的输 ...
- C++ Qt开发:自定义Dialog对话框组件
Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍自定义Dial ...
- Codeforces Round #426 (Div. 2) A. The Useless Toy
A. The Useless Toy time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- ElasticSearch之Open index API
打开指定的索引. 命令样例如下: curl -X POST "https://localhost:9200/testindex_003/_open?pretty" --cacert ...