当前位置:网站首页>[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;
}
边栏推荐
- 有哪些比较推荐的论文翻译软件?
- 经济学外文文献在哪查?
- Markdown tutorial
- TypeError: Cannot read properties of undefined (reading ***)
- Where can I find the English literature of the thesis (except HowNet)?
- JS interviewer wants to know how much you understand call, apply, bind no regrets series
- setInterval定时器在ie不生效原因之一:回调的是箭头函数
- Multi process programming (III): message queue
- ftrace工具的介绍及使用
- NC50965 Largest Rectangle in a Histogram
猜你喜欢

教育学大佬是怎么找外文参考文献的?

Cmake basic use

How do educators find foreign language references?

详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图

Redis21 classic interview questions, extreme pull interviewer

pageoffice-之bug修改之旅

One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function

CMake基本使用

使用jenkins之二Job

Rust字符串切片、结构体和枚举类
随机推荐
Sysdig analysis container system call
Nc20806 District interval
pod生命周期详解
What is the standard format of a 2000-3000 word essay for college students' classroom homework?
Introduction and use of ftrace tool
Preview word documents online
Implement the foreach method of array
Thinkadmin V6 arbitrary file read vulnerability (cve-2020-25540)
pageoffice-之bug修改之旅
Centos7 one click compilation to build MySQL script
Rust所有权(非常重要)
多进程编程(三):消息队列
【luogu P4320】道路相遇(圆方树)
Briefly talk about other uses of operation and maintenance monitoring
Bypass AV with golang
奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
经济学外文文献在哪查?
使用jenkins之二Job
多进程编程(五):信号量
可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场