当前位置:网站首页>[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;
}
边栏推荐
- Bypass AV with golang
- v8
- NC24325 [USACO 2012 Mar S]Flowerpot
- [golang syntax] map common errors golang panic: assignment to entry in nil map
- 【Pulsar文档】概念和架构/Concepts and Architecture
- Detailed explanation of pod life cycle
- Architecture: load balancing
- How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
- node_ Modules cannot be deleted
- MySQL 23道经典面试吊打面试官
猜你喜欢
![Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation](/img/4a/ab732c41ea8a939fa0983fec475622.png)
Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation

pageoffice-之bug修改之旅

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

Architecture: database architecture design
![[target detection] r-cnn, fast r-cnn, fast r-cnn learning](/img/f0/df285f01ffadff62eb3dcb92f2e04f.jpg)
[target detection] r-cnn, fast r-cnn, fast r-cnn learning

How do educators find foreign language references?

logback配置文件

【雅思阅读】王希伟阅读P1(阅读判断题)

布隆过滤器

Basic use of shell script
随机推荐
关于Unity屏幕相关Screen的练习题目,Unity内部环绕某点做运动
MySQL 23道经典面试吊打面试官
Chapter 4 of getting started with MySQL: data types stored in data tables
NC17059 队列Q
NC20806 区区区间间间
在线预览Word文档
Blue decides red - burst CS teamserver password
Hundreds of continuous innovation to create free low code office tools
How SQLSEVER removes data with duplicate IDS
Free we media essential tools sharing
setInterval定时器在ie不生效原因之一:回调的是箭头函数
Sysdig analysis container system call
有哪些比较推荐的论文翻译软件?
Helm basic learning
What is the standard format of a 2000-3000 word essay for college students' classroom homework?
NC50965 Largest Rectangle in a Histogram
AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
node_ Modules cannot be deleted
Logback configuration file
Missing number