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交换一位可以得到素 ...
随机推荐
- Cenos7下nginx+mysql+php环境的搭建
首先更新系统软件 1 $ yum update 第一步:安装nginx 1.安装nginx源 1 $ yum localinstall http://nginx.org/packages/centos ...
- GEI步态能量图生成
步态能量图生成主要有两步,主要为: 在原始轮廓图上对人的轮廓进行裁剪,在下面制作步态能量图图片叠加以什么为中心位置也是一个问题.一般有两种方式,一种是中心位置为人体宽的一半.另一种是以头顶为中心位置. ...
- HY 的惩罚 (Trie 树,博弈论)
[问题描述] hy 抄题解又被老师抓住了,现在老师把他叫到了办公室. 老师要 hy 和他玩一个游 戏.如果 hy 输了,老师就要把他开除信息组; 游戏分为 k 轮.在游戏开始之前,老师会将 n 个由英 ...
- 20180821-Java封装
java 封装 在面向对象程式设计方法中,封装(英语:Encapsulation)是指,一种将抽象性函式接口的实作细节部份包装.隐藏起来的方法. 封装可以被认为是一个保护屏障,防止该类的代码和数据被外 ...
- div 上禁止复制的css实现方法
div { -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-se ...
- 使用MAC OS X进行PHP开发的一些建议和技巧
原创作品,允许转载,转载时请务必以超链接形式标明转载自:线筝 本文链接地址: 使用Mac OS X进行PHP开发的一些建议和技巧 用Mac OS X作为开发机已经有一年多的时间了,在这里写下自己的一些 ...
- js 通过浏览器直接打开应用程序(IOS,Android)
实现效果 如下图所示,在手机浏览器中访问京东的手机版网站(m.jd.com),顶部会有一个广告图,点击这个广告图,如果手机上已经安装了京东App,则直接打开,如果没有安装,则开始下载. 实现方式 1. ...
- Ehcache配置项及持久化到硬盘
EhCache 常见的配置项: cache元素的属性 name:缓存名称 maxElementsInMemory:内存中最大缓存对象数 maxElementsOnDisk:硬盘中最大缓存对象数,若是0 ...
- P2258子矩阵
传送 一道看起来就很暴力的题. 这道题不仅暴力,还要用正确的姿势打开暴力. 因为子矩阵的参数有两个,一个行一个列(废话) 我们一次枚举两个参数很容易乱对不对?所以我们先枚举行,再枚举列 枚举完行,列, ...
- Selenium WebDriver 常用API
public class Demo1 { WebDriver driver; @BeforeMethod public void visit(){ //webdriver对象的声明 System.se ...