bzoj5049 [Lydsy1709月赛]导航系统 双向bfs
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=5049
题解
题面里面满眼的随机。既然数据完全随机,那就是在锻炼选手的乱搞能力啊。
根据一个常用的结论,一棵随机的树的深度不超过 \(\log n\),大概等价于一个点期望下有 \(2\) 个孩子。
那么这个图中,以某个点为根的最短路树也应该满足。不妨设 \(dis(u,v) = l\),根据之前的结论,\(l \leq \log n\)。那么如果直接暴力 bfs 建最短路的话,需要遍历 \(2^l = n\) 个点。这样的情况可以使用双向 bfs,这样需要遍历的点就只有 \(2\cdot 2^{\frac l2} = \sqrt n\) 个点。
代码如下,在期望情况下的时间复杂度为 \(O(k\sqrt n)\)。
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
const int N = 100000 + 7;
const int M = 300000 + 7;
int n, m, qq;
int fa[N], sz[N], q[N], vis[N], dis[N];
struct Edge { int to, ne; } g[M << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); }
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline void merge(int x, int y) {
x = find(x), y = find(y);
if (sz[x] < sz[y]) std::swap(x, y);
fa[y] = x, smax(sz[x], sz[y] + 1);
}
inline int bfs(int s, int t) {
int hd = 0, tl = 0;
q[++tl] = s, vis[s] = 1, dis[s] = 0;
q[++tl] = t, vis[t] = -1, dis[t] = 0;
while (hd < tl) {
int x = q[++hd];
for fec(i, x, y) if (!vis[y]) dis[y] = dis[x] + 1, vis[y] = vis[x], q[++tl] = y;
else if (vis[y] != vis[x]) {
for (int i = 1; i <= tl; ++i) vis[q[i]] = 0;
return dis[x] + dis[y] + 1;
}
}
return -1;
}
inline void work() {
while (qq--) {
int x, y;
read(x), read(y);
if (find(x) != find(y)) puts("-1");
else printf("%d\n", bfs(x, y));
}
}
inline void init() {
read(n), read(m), read(qq);
int x, y;
for (int i = 1; i <= n; ++i) fa[i] = i, sz[i] = 1;
for (int i = 1; i <= m; ++i) read(x), read(y), adde(x, y), merge(x, y);
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
bzoj5049 [Lydsy1709月赛]导航系统 双向bfs的更多相关文章
- 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路
题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...
- POJ1915Knight Moves(单向BFS + 双向BFS)
题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- POJ 3170 Knights of Ni (暴力,双向BFS)
题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...
- [转] 搜索之双向BFS
转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...
- 双向BFS
转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2) 快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- POJ 3126 Prime Path 解题报告(BFS & 双向BFS)
题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...
随机推荐
- python 3.6连接数据库(pymysql方式)
pymysql 模块可以通过 pip 安装.但如果你使用的是 pycharm IDE,则可以使用 project python 安装第三方模块. [File] >> [settings] ...
- 将原生JS和jquery里面的DOM操作进行了一下整理
创建元素节点 1.原生: document.createElement("div") 2.jquery: $("<div></div>" ...
- 004-URL编码转换函数:escape()、encodeURI()、encodeURIComponent()
一.概述 函数出现时间: escape() javascript 1.0 ...
- Vagrant 手册之网络 - 私有网络 private network
原文地址 Vagrantfile 配置文件中私有网络的标识符:private_network,例如: config.vm.network "private_network", ty ...
- vue分页练习
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【MM系列】SAP ABAP 在选择画面显示输出结果
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 在选择画面显示 ...
- MySQL replace into 与insert into
https://blog.csdn.net/helloxiaozhe/article/details/77427266 使用replace带来的问题 1.Replace into 操作在唯一键重复情况 ...
- Win7 64位注册32位DLL
记忆力越来越差,备忘. 参考地址 https://support.microsoft.com/en-us/help/249873/how-to-use-the-regsvr32-tool-and-tr ...
- 最长连续公共子序列(LCS)与最长递增公共子序列(LIS)
最长公共子序列(不连续) 实际问题中也有比较多的应用,比如,论文查重这种,就是很实际的一个使用方面. 这个应该是最常见的一种了,不再赘述,直接按照转移方程来进行: 按最普通的方式就是,直接构造二维矩阵 ...
- 解决Sql Server服务远程过程调用失败
方法一:修复Sql Server: 修复过程中若遇到:重新启动计算机失败, 1.按下组合键[Win]+[R],调出运行窗口 2.输入“regedit”,在注册表左侧目录栏中找到如下位置:“HKEY_L ...