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 题解的更多相关文章

  1. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  2. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  3. poj 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

  4. POJ 2236 Wireless Network (并查集)

    Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...

  5. POJ 2236 Wireless Network(并查集)

    传送门  Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 24513   Accepted ...

  6. POJ 2236 Wireless Network (并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 761 ...

  7. poj 2236 Wireless Network 【并查集】

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16832   Accepted: 706 ...

  8. POJ 2236 Wireless Network [并查集+几何坐标 ]

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

  9. poj 2236 Wireless Network (并查集)

    链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 ...

  10. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network

    题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p   代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...

随机推荐

  1. Windows 11 + Samsung 980 踩坑:在 LocalDB 15.0 实例启动期间出错: 无法启动 SQL Server 进程(附赠 查询指定日期范围内的前1000条SQL执行记录)

    Windows 11 + Samsung 980 踩坑:在 LocalDB 实例启动期间出错: 无法启动 SQL Server 进程 起因 用 Microsoft Visual Studio 2022 ...

  2. PageHelper插件注意事项

    PageHelper插件注意事项 使用PageHelper.startPage后要紧跟查询语句 下面的代码就有可能出问题: PageHelper.startPage(10, 10); if(param ...

  3. Salesforce LWC学习(四十六) 自定义Datatable实现cell onclick功能

    本篇参考:https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable 背景:我们有时会有这种类 ...

  4. [CF1601C] Optimal Insertion

    Optimal Insertion 题面翻译 题目大意 给定两个序列 \(a,b\),长度分别为 \(n,m(1\leq n,m\leq 10^6)\).接下来将 \(b\) 中的所有元素以任意方式插 ...

  5. 高斯朴素贝叶斯(Gaussian Naive Bayes)原理与实现——垃圾邮件识别实战

    朴素贝叶斯(Naive Bayes): 根据贝叶斯定理和朴素假设提出的朴素贝叶斯模型. 贝叶斯定理: 朴素假设(特征条件独立性假设): 代入可知朴素贝叶斯模型计算公式: 因为朴素贝叶斯是用来分类任务, ...

  6. hdu 5685

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5685 解题思路:前缀积+费马小定理求逆元. AC代码: 1 #include<iostream> ...

  7. WebView中的页面调试方法

    在 iOS 12 中,苹果正式弃用 UIWebView,改成 WKWebView,参考官方声明. 后者在性能.稳定性.功能方面有很大提升,并且与 Safari 具有相同的 JavaScript 引擎( ...

  8. 构建健康游戏环境:DFA算法在敏感词过滤的应用

    现在的游戏有敏感词检测这一点,相信大家也不陌生了,不管是聊天,起名,签名还是简介,只要是能让玩家手动输入的地方,一定少不了敏感词识别,至于识别之后是拒绝修改还是星号替换,这个就各有各的做法了,但是绕不 ...

  9. Redis 分片集群

    1.Redis分片集群 1.1.搭建分片集群 主从和哨兵可以解决高可用.高并发读的问题.但是依然有两个问题没有解决: 海量数据存储问题 高并发写的问题 使用分片集群可以解决上述问题,如图: 分片集群特 ...

  10. 【scikit-learn基础】--『监督学习』之 随机森林分类

    随机森林分类算法是一种基于集成学习(ensemble learning)的机器学习算法,它的基本原理是通过对多个决策树的预测结果进行平均或投票,以产生最终的分类结果. 随机森林算法可用于回归和分类问题 ...