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. [AI]探寻高等生命的多面驱动

    引子 意识从来是一个前沿课题,充满了学术大神,也充满了神棍.对于意识的讨论和研究需要保持开放的思想,也要遵守理性的严格的方法.我们不是着急去推翻什么或者声称发现了什么,我们大部分要做的事情是把实验多重 ...

  2. Codeforces Round 894 (Div. 3)

    Codeforces Round 894 (Div. 3) A. Gift Carpet 题意:判断一列一个字母有没有"vika" 思路:挨个枚举每一列 #include<b ...

  3. 手机成绩分析软件排行榜TOP10下载

    随着智能手机的普及和移动应用的快速发展,手机成绩分析软件越来越受到学生.家长和教育机构的关注.这些软件可以帮助用户方便地记录.分析和管理学生成绩,提供个性化的学习指导和反馈.在本文中,将详细介绍202 ...

  4. chatgpt接口开发笔记3: 语音识别接口

    chatgpt接口开发笔记3: 语音识别接口 1.文本转语音 1.了解接口参数 接口地址: POST https://api.openai.com/v1/audio/speech 下面是接口文档描述内 ...

  5. Pattern类和Matcher类的使用

    1.先看好数据源 先将一个String对象确定为程序要对其进行操作的数据源. String b="hello,good morning"; 2.建立Pattern类的对象 Stri ...

  6. Charles对Android手机Https请求的抓包

    Charles对Android手机Https请求的抓包 • 前情提要: 本文只是对android手机进行抓包的描述,由于android手机系统原因,android7.0系统及以上需要在app中配置证书 ...

  7. AVL树和红黑树的Python代码实现

    AVL树 AVL树是一种自平衡二叉搜索树.在这种树中,任何节点的两个子树的高度差被严格控制在1以内.这确保了树的平衡,从而保证了搜索.插入和删除操作的高效性.AVL树是由Georgy Adelson- ...

  8. 一文读懂遗传算法(附python)

    几天前,我着手解决一个实际问题--大型超市销售问题.在使用了几个简单模型做了一些特征工程之后,我在排行榜上名列第 219 名. 虽然结果不错,但是我还是想做得更好.于是,我开始研究可以提高分数的优化方 ...

  9. 安装华企盾DSC防泄密软件造成CAD2012卡住怎么办?

    将下图目录的.exe程序删除或者重命名

  10. #11独立开发周总结|核心OKR1000元/月已达标

    核心OKR:1000元/月达成情况 算上微信上收费了200多元,核心OKR已达标 12.25-12.29本周完成事项 产品方面 本周产品上主要是在进行重构的测试,顺利上线,线上问题也比较少 运营方面 ...