当前位置:网站首页>【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;
}
边栏推荐
- NC50528 滑动窗口
- 洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表
- Which websites can I search for references when writing a thesis?
- Basic use of shell script
- Bypass AV with golang
- Pytorch里面多任务Loss是加起来还是分别backward?
- Slf4j + Logback日志框架
- 多进程编程(四):共享内存
- Blue decides red - burst CS teamserver password
- Free we media essential tools sharing
猜你喜欢
Markdown使用教程
mm中的GAN模型架构
What website can you find English literature on?
What are the projects of metauniverse and what are the companies of metauniverse
Where can I find the English literature of the thesis (except HowNet)?
Architecture: load balancing
Monitor container runtime tool Falco
请问大家在什么网站上能查到英文文献?
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
Basic 10 of C language: array and pointer
随机推荐
毕业总结
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
Pageoffice - bug modification journey
JVM foundation review
国外的论文在那找?
FAQ | FAQ for building applications for large screen devices
Markdown使用教程
NC50965 Largest Rectangle in a Histogram
[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
Feature Engineering: summary of common feature transformation methods
Missing number
Chapter 3 of getting started with MySQL: database creation and operation
Multi process programming (III): message queue
Nc20806 District interval
Is there a specific format for English papers?
Sysdig analysis container system call
Program analysis and Optimization - 9 appendix XLA buffer assignment
JS interviewer wants to know how much you understand call, apply, bind no regrets series
免费自媒体必备工具分享
Multiprocess programming (V): semaphores