【洛谷 P4320】 道路相遇 (圆方树,LCA)
题目链接
题意:给一张无向图和\(M\)个询问,问\(u,v\)之间的路径的必经之点的个数。
对图建出圆方树,然后必经之点就是两点路径经过的原点个数,用\((dep[u]+dep[v]-dep[LCA]*2)/2+1\)即可算出。
什么你不知道圆方树(说的跟我知道一样)
\(APIO2018\)出来的黑科技,详见\(APIO2018\)铁人两项。
就是对每个点双新建一个点,然后让点双里所有点都对这个点连边。
看图。

#include <cstdio>
const int MAXN = 500010;
const int MAXM = 1000010;
namespace IO{
int xjc; char ch;
inline int read(){
xjc = 0; ch = getchar();
while(ch < '0' || ch > '9') ch = getchar();
while(ch >= '0' && ch <= '9'){ xjc = xjc * 10 + ch - '0'; ch = getchar(); }
return xjc;
}
}using namespace IO;
inline int min(int a, int b){
return a > b ? b : a;
}
inline void swap(int &a, int &b){
xjc = a; a = b; b = xjc;
}
int n, m, k, a, b, cha;
struct Edge{
int next, to;
};
struct Graph{
int head[MAXN << 1], num;
Edge e[MAXM << 1];
inline void Add(int from, int to){
e[++num].to = to; e[num].next = head[from]; head[from] = num;
e[++num].to = from; e[num].next = head[to]; head[to] = num;
}
}G, T;
int dfn[MAXN], low[MAXN], vis[MAXN], stack[MAXN], f[MAXN << 1][20], dep[MAXN << 1], top, id, cnt;
void Tarjan(int u){
dfn[u] = low[u] = ++id; stack[++top] = u;
for(int i = G.head[u]; i; i = G.e[i].next)
if(!dfn[G.e[i].to]){
Tarjan(G.e[i].to);
low[u] = min(low[u], low[G.e[i].to]);
if(low[G.e[i].to] >= dfn[u]){
T.Add(u, ++cnt);
do T.Add(stack[top], cnt);
while(stack[top--] != G.e[i].to);
}
}
else low[u] = min(low[u], dfn[G.e[i].to]);
}
void getDF(int u, int fa){
f[u][0] = fa; dep[u] = dep[fa] + 1;
for(int i = T. head[u]; i; i = T.e[i].next)
if(T.e[i].to != fa)
getDF(T.e[i].to, u);
}
void make_ST(){
for(int j = 1; j <= 19; ++j)
for(int i = 1; i <= cnt; ++i)
f[i][j] = f[f[i][j - 1]][j - 1];
}
inline int LCA(int u, int v){
if(dep[u] < dep[v]) swap(u, v);
cha = dep[u] - dep[v];
if(cha)
for(int i = 0; i <= 19; ++i)
if(cha & (1 << i))
u = f[u][i];
if(u == v) return u;
for(int i = 19; ~i; --i)
if(f[u][i] != f[v][i])
u = f[u][i], v = f[v][i];
return f[u][0];
}
int main(){
cnt = n = read(); m = read();
for(int i = 1; i <= m; ++i)
G.Add(read(), read());
Tarjan(1);
getDF(1, 0);
make_ST();
k = read();
while(k--){
a = read(); b = read();
printf("%d\n", ((dep[a] + dep[b] - (dep[LCA(a, b)] << 1)) >> 1) + 1);
}
return 0;
}
【洛谷 P4320】 道路相遇 (圆方树,LCA)的更多相关文章
- 【luogu4320】道路相遇 (圆方树 + LCA)
Description 给你一张\(~n~\)个点\(~m~\)条边的无向图,保证无重边无自环, 共\(~q~\)组询问求\(~x~\)到\(~y~\)的路径上必经的点数. Solution ...
- 【刷题】洛谷 P4320 道路相遇
题目描述 在 H 国的小 w 决定到从城市 \(u\) 到城市 \(v\) 旅行,但是此时小 c 由于各种原因不在城市 \(u\),但是小 c 决定到在中途与小 w 相遇 由于 H 国道路的原因,小 ...
- luoguP4320 道路相遇 圆方树
标题已经告诉你怎么做了..... 两点间的圆点个数即为所求 建出圆方树后打个树剖求$lca$就行..... 复杂度$O(n + q \log n)$ #include <cstdio> # ...
- [洛谷P4320]道路相遇
题目大意:基本同上一题[bzoj5329][Sdoi2018]战略游戏,只是每个点集内只有两个点,且只有一组询问而已.(双倍经验?我反正就直接改了一下代码就交了) 题解:同上一题(链接见“题目大意”) ...
- P4320-道路相遇,P5058-[ZJOI2004]嗅探器【圆方树,LCA】
两题差不多就一起写了 P4320-道路相遇 题目链接:https://www.luogu.com.cn/problem/P4320 题目大意 \(n\)个点\(m\)条边的一张图,\(q\)次询问两个 ...
- Traffic Real Time Query System 圆方树+LCA
题目描述 City C is really a nightmare of all drivers for its traffic jams. To solve the traffic problem, ...
- 图论杂项细节梳理&模板(虚树,圆方树,仙人掌,欧拉路径,还有。。。)
orzYCB 虚树 %自为风月马前卒巨佬% 用于优化一类树形DP问题. 当状态转移只和树中的某些关键点有关的时候,我们把这些点和它们两两之间的LCA弄出来,以点的祖孙关系连成一棵新的树,这就是虚树. ...
- 洛谷P4606 [SDOI2018]战略游戏 【圆方树 + 虚树】
题目链接 洛谷P4606 双倍经验:弱化版 题解 两点之间必经的点就是圆方树上两点之间的圆点 所以只需建出圆方树 每次询问建出虚树,统计一下虚树边上有多少圆点即可 还要讨论一下经不经过根\(1\)的情 ...
- 洛谷P4630 [APIO2018] Duathlon 铁人两项 【圆方树】
题目链接 洛谷P4630 题解 看了一下部分分,觉得树的部分很可做,就相当于求一个点对路径长之和的东西,考虑一下能不能转化到一般图来? 一般图要转为树,就使用圆方树呗 思考一下发现,两点之间经过的点双 ...
随机推荐
- lintcode-141-x的平方根
141-x的平方根 实现 int sqrt(int x) 函数,计算并返回 x 的平方根. 样例 sqrt(3) = 1 sqrt(4) = 2 sqrt(5) = 2 sqrt(10) = 3 挑战 ...
- cacti监控多个mysql端口
1. 在Console -> Data Templates 找到mysql-cacti-templates的mysql模板,编辑: 在这里面把Port的Use Per-Data Source V ...
- oracle怎样查询索引的使用情况
查询用户的索引select index_name,table_name,tablespace_name, index_type,uniqueness , status from dba_indexes ...
- java 文本读取 写入指定长度的内容
- CF961D Pair Of Lines
题目描述 You are given n n n points on Cartesian plane. Every point is a lattice point (i. e. both of it ...
- 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService...匹配的导出
安装微软的windows补丁 KB2781514 ,补丁主要解决“在 .NET Framework 4.5 更新之后,Visual Studio 用户可能无法打开或创建 C++ 或 JavaScrip ...
- [LOJ#2542] [PKUWC2018] 随机游走
题目描述 给定一棵 n 个结点的树,你从点 x 出发,每次等概率随机选择一条与所在点相邻的边走过去. 有 Q 次询问,每次询问给定一个集合 S,求如果从 x 出发一直随机游走,直到点集 S 中所有点都 ...
- 最小角回归 LARS算法包的用法以及模型参数的选择(R语言 )
Lasso回归模型,是常用线性回归的模型,当模型维度较高时,Lasso算法通过求解稀疏解对模型进行变量选择.Lars算法则提供了一种快速求解该模型的方法.Lars算法的基本原理有许多其他文章可以参考, ...
- BZOJ5290 & 洛谷4438:[HNOI/AHOI2018]道路——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5290 https://www.luogu.org/problemnew/show/P4438 的确 ...
- Communications link failure
针对数据库Communications link failure的错误,可以理解为有两种策略解决: 策略1(推荐): 数据池配置 <property name="minEvic ...