当前位置:网站首页>【luogu P4320】道路相遇(圆方树)
【luogu P4320】道路相遇(圆方树)
2022-07-02 23:31:00 【SSL_TJH】
道路相遇
题目链接:luogu P4320
题目大意
给你一个无向连通图,无重边自环,然后每次给你两点,问你有多少个点是两点间路径必有的。
思路
圆方树pre模板题?
圆方树怎么做这里不说,看铁人两项的博客。
那我们知道圆方树的性质就是圆点方点是固定的,而且圆点是原图的点。
那代表着圆点的数量就是必须走的点的数量,也就是(链的数量+1)/2。
没了。
代码
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 5e5 + 100;
int n, m, q;
vector <int> G[N];
struct YF_tree {
int dfn[N], low[N], deg[N << 1], fa[N << 1][21], sta[N], tot;
vector <int> GG[N << 1];
void link(int x, int y) {
GG[x].push_back(y); GG[y].push_back(x);}
void tarjan(int now) {
dfn[now] = low[now] = ++dfn[0]; sta[++sta[0]] = now;
for (int i = 0; i < G[now].size(); i++) {
int x = G[now][i];
if (!dfn[x]) {
tarjan(x); low[now] = min(low[now], low[x]);
if (dfn[now] == low[x]) {
tot++;
while (sta[sta[0]] != x) {
link(tot, sta[sta[0]]);
sta[0]--;
}
link(tot, sta[sta[0]]); sta[0]--;
link(tot, now);
}
}
else low[now] = min(low[now], dfn[x]);
}
}
void dfs(int now, int father) {
deg[now] = deg[father] + 1; fa[now][0] = father;
for (int i = 1; i <= 20; i++) fa[now][i] = fa[fa[now][i - 1]][i - 1];
for (int i = 0; i < GG[now].size(); i++) {
int x = GG[now][i];
if (x != father) {
dfs(x, now);
}
}
}
void Init() {
tot = n;
for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i), dfs(i, 0);
}
int LCA(int x, int y) {
if (deg[x] < deg[y]) swap(x, y);
for (int i = 20; i >= 0; i--)
if (deg[fa[x][i]] >= deg[y]) x = fa[x][i];
if (x == y) return x;
for (int i = 20; i >= 0; i--) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
int quest(int x, int y) {
int lca = LCA(x, y), dis = deg[x] + deg[y] - deg[fa[lca][0]] * 2;
return (dis + 1) / 2;
}
}T;
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
int x, y; scanf("%d %d", &x, &y);
G[x].push_back(y); G[y].push_back(x);
}
T.Init();
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
int x, y; scanf("%d %d", &x, &y);
printf("%d\n", T.quest(x, y));
}
return 0;
}
边栏推荐
- 百数不断创新,打造自由的低代码办公工具
- 洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
- [MCU project training] eight way answering machine
- [IELTS reading] Wang Xiwei reading P1 (reading judgment question)
- [pulsar document] concepts and architecture
- Andorid gets the system title bar height
- Using tensorflow to realize voiceprint recognition
- Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration
- How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
- Chapter 3 of getting started with MySQL: database creation and operation
猜你喜欢
随机推荐
NC24325 [USACO 2012 Mar S]Flowerpot
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
毕业总结
NC50965 Largest Rectangle in a Histogram
数组常用操作方法整理(包含es6)及详细使用
[reading notes] phased summary of writing reading notes
Bypass AV with golang
pod生命周期详解
v8
sysdig分析容器系统调用
多进程编程(二):管道
pageoffice-之bug修改之旅
Feature Engineering: summary of common feature transformation methods
MySQL 23 classic interview hanging interviewer
[golang syntax] map common errors golang panic: assignment to entry in nil map
Basic 10 of C language: array and pointer
Sysdig analysis container system call
英文论文有具体的格式吗?
JS interviewer wants to know how much you understand call, apply, bind no regrets series
Chapter 3 of getting started with MySQL: database creation and operation