当前位置:网站首页>[Luogu p4320] road meets (round square tree)
[Luogu p4320] road meets (round square tree)
2022-07-03 00:33:00 【SSL_ TJH】
Road meets
Topic link :luogu P4320
The main idea of the topic
Here's an undirected connected graph , No double edge self ring , Then I'll give you two points each time , Ask you how many points are necessary for the path between two points .
Ideas
Round square tree pre The template questions ?
How to make a round square tree is not mentioned here , Watch the blog of triathlon .
Then we know that the nature of the round square tree is that the dots and square dots are fixed , And the dot is the dot of the original drawing .
That means that the number of dots is the number of dots that must go , That is to say ( The number of chains +1)/2.
be without .
Code
#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;
}
边栏推荐
- Install docker and use docker to install MySQL
- Multiprocess programming (I): basic concepts
- ftrace工具的介绍及使用
- 【雅思阅读】王希伟阅读P1(阅读判断题)
- Bypass AV with golang
- 英文论文有具体的格式吗?
- 为什么网站打开速度慢?
- Multi process programming (III): message queue
- Understanding and application of least square method
- FAQ | FAQ for building applications for large screen devices
猜你喜欢

Architecture: database architecture design

How SQLSEVER removes data with duplicate IDS

Bloom filter

Cmake basic use

Shell 实现文件基本操作(切割、排序、去重)

论文的英文文献在哪找(除了知网)?

DotNet圈里一个优秀的ORM——FreeSql
![Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration](/img/a3/55bb71d39801ceeee421a0c8ded333.png)
Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration

可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场

Redis21 classic interview questions, extreme pull interviewer
随机推荐
Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
FAQ | FAQ for building applications for large screen devices
TypeError: Cannot read properties of undefined (reading ***)
Maya fishing house modeling
[IELTS reading] Wang Xiwei reading P2 (reading fill in the blank)
简单聊聊运维监控的其他用途
Basic use of shell script
关于Unity屏幕相关Screen的练习题目,Unity内部环绕某点做运动
Nc20806 District interval
布隆过滤器
CMake基本使用
多进程编程(四):共享内存
[shutter] image component (image component introduction | image constructor | image.network constructor | image.asset constructor)
Should you study kubernetes?
【雅思阅读】王希伟阅读P2(阅读填空)
【luogu P4320】道路相遇(圆方树)
TypeError: Cannot read properties of undefined (reading ***)
Bypass AV with golang
pod生命周期详解
Angled detection frame | calibrated depth feature for target detection (with implementation source code)