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之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...
随机推荐
- 决策树C4.5算法的技术深度剖析、实战解读
在本篇深入探讨的文章中,我们全面分析了C4.5决策树算法,包括其核心原理.实现流程.实战案例,以及与其他流行决策树算法(如ID3.CART和Random Forests)的比较.文章不仅涵盖了丰富的理 ...
- StackGres 1.6,可私有部署的云原生数据库中间件平台工程
StackGres 数据库平台工程简介 Enterprise Postgres made easy. On Kubernetes StackGres 是 Kubernetes 的全栈 PostgreS ...
- Eclipse 安装 ABAP 插件报错 Microsoft Visual C++ 2013 (x64) 快速解决
去官网下载Microsoft Visual C++ 2013 (x64) 安装 Download Visual C++ Redistributable Packages for Visual St ...
- Kylin Linux Advanced Server V10 上安装 Nacos详细步骤
要在 Kylin Linux Advanced Server V10 上安装 Nacos,可以按照以下进行操作:1.安装 Java JDK:首先确保已在 Kylin Linux Advanced Se ...
- springBoot——整合junit
spring整合junit复习 springBoot整合junit package com.example.springboot_04; import com.example.springboot_0 ...
- 容器网络Cilium:DualStack双栈特性分析
本文分享自华为云社区<容器网络Cilium入门系列之DualStack双栈特性分析>,作者: 可以交个朋友. 一 . 关于IPV6/IPV4 双栈 目前很多公司开始将自己的业务由ipv4切 ...
- 【C++】关于全局变量和局部变量问题
1 #include <iostream> 2 using namespace std; 3 4 void func(void); 5 6 static int count = 10; 7 ...
- 太赞了!墙裂推荐这款网页版 Nginx 配置生成器,好用到爆!
之前民工哥也给大家介绍过一款Nginx配置生成器:强大!Nginx 配置在线一键生成"神器",不太了解的人可以去看一看. 最近民工哥又发现一款好用的网页版开源工具,同样它的功能也是 ...
- DNSlog注入(利用DNSlog平台将SQL盲注变成回显注入)
前言 什么是UNC 什么是DNSlog注入 DNSlog注入的条件 防止DNSlog注入的几个措施 sqli-labs试验 前言 前几天面试的时候,面试官问我知不知道OOB(带外数据). 当时我蒙了, ...
- 完蛋,我被挖矿木马包围了|使用 TLS 连接 Docker
事故还原 近日,白泽在使用 docker 的时候,开放了防火墙的端口,以 SSH 方式访问远程服务器的 docker 守护进程(无需使用密钥即可建立连接),随后竟遭到了挖矿木马的攻击,好一顿折腾之后, ...