当前位置:网站首页>【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;
}
边栏推荐
- Preview word documents online
- 请问大家在什么网站上能查到英文文献?
- MySQL advanced learning notes (4)
- NC24325 [USACO 2012 Mar S]Flowerpot
- Thinkadmin V6 arbitrary file read vulnerability (cve-2020-25540)
- Basic 10 of C language: array and pointer
- MySQL 23道经典面试吊打面试官
- node_modules删不掉
- Where can I check the foreign literature of economics?
- pod生命周期详解
猜你喜欢

Which websites can I search for references when writing a thesis?

Monitor container runtime tool Falco

Pageoffice - bug modification journey

Markdown使用教程

Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track

What are the recommended thesis translation software?

Multiprocess programming (II): Pipeline

Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)

Introduction of UART, RS232, RS485, I2C and SPI
![Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation](/img/4a/ab732c41ea8a939fa0983fec475622.png)
Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation
随机推荐
AcWing_188. 武士风度的牛_bfs
Xcode real machine debugging
Monitor container runtime tool Falco
Angled detection frame | calibrated depth feature for target detection (with implementation source code)
Andorid gets the system title bar height
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
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
Seckill system design
FRP reverse proxy +msf get shell
NC50965 Largest Rectangle in a Histogram
NC50965 Largest Rectangle in a Histogram
Pytorch 20 realizes corrosion expansion based on pytorch
DotNet圈里一个优秀的ORM——FreeSql
Slf4j + Logback日志框架
ftrace工具的介绍及使用
pageoffice-之bug修改之旅
MySQL 23 classic interview hanging interviewer
TypeError: Cannot read properties of undefined (reading ***)
在线预览Word文档
【Pulsar文档】概念和架构/Concepts and Architecture