洛谷P10953 逃不掉的路 题解 边双连通分量(缩点)+ LCA

news/2025/12/11 1:03:01/文章来源:https://www.cnblogs.com/quanjun/p/19333727

题目链接:https://www.luogu.com.cn/problem/P10953

解题思路:

缩点之后是棵树,答案是两点对应的的点在树上的距离。

因为缩点之后的树上的每一条边都对应一座桥。

示例程序:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;int n, m, q, ts, dcc, dfn[maxn], low[maxn], bl[maxn], fa[maxn][17], dep[maxn];
vector<int> g[maxn], G[maxn];
stack<int> stk;void tarjan(int u, int p) {dfn[u] = low[u] = ++ts;stk.push(u);for (auto v : g[u]) {if (v == p)continue;if (!dfn[v])tarjan(v, u), low[u] = min(low[u], low[v]);elselow[u] = min(low[u], dfn[v]);}if (low[u] == dfn[u]) {dcc++;int v;do {v = stk.top();stk.pop();bl[v] = dcc;} while (u != v);}
}void dfs(int u, int p, int d) {fa[u][0] = p;dep[u] = d;for (auto v : G[u])if (v != p)dfs(v, u, d+1);
}int lca(int x, int y) {if (dep[x] < dep[y])swap(x, y);for (int i = 16; i >= 0; i--) {if (dep[x] - dep[y] >= (1<<i))x = fa[x][i];}if (x == y) return x;for (int i = 16; i>= 0; i--) {if (fa[x][i] != fa[y][i])x = fa[x][i], y = fa[y][i];}return fa[x][0];
}int dis(int x, int y) {int z = lca(x, y);return dep[x] + dep[y] - 2 * dep[z];
}int main() {scanf("%d%d", &n, &m);for (int i = 0, u, v; i < m; i++) {scanf("%d%d", &u, &v);g[u].push_back(v);g[v].push_back(u);}tarjan(1, -1);for (int u = 1; u <= n; u++) {for (auto v : g[u]) {int x = bl[u], y = bl[v];if (x != y) {G[x].push_back(y);}}}dfs(1, 0, 0);for (int j = 1; j <= 16; j++)for (int i = 1; i <= dcc; i++)fa[i][j] = fa[ fa[i][j-1] ][j-1];scanf("%d", &q);for (int i = 0, a, b; i < q; i++) {scanf("%d%d", &a, &b);printf("%d\n", dis(bl[a], bl[b]));}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/997580.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STM32-bootloader引导程序跳转机制笔记 - 教程

STM32-bootloader引导程序跳转机制笔记 - 教程pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", &q…

深入解析:心率变异性计算算法(HRV)

深入解析:心率变异性计算算法(HRV)pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Mona…

Unlock Super Remote Diagnosis on Launch Smartlink C with 1-Time Activation Card

Problem: Remote Diagnostics Barriers in European/American Auto Repair In the dynamic world of automotive repair, European and American mechanics and car owners alike face a common challenge: complex ve…

.gitignore 的匹配规则

.gitignore 是使用 Git 的日常开发时一个非常常见的文件。它负责告诉 Git 哪些文件或目录应当被忽略,不被加入版本控制。必须掌握。 基本规则.gitignore 文件对其所在目录以及所有子目录生效。每行一条规则:空行:无…

无法在Debian13 VSCode中使用fcitx5输入中文

根据 https://fcitx-im.org/wiki/Setup_Fcitx_5/zh-cn 编辑下面两个文件,然后重启电脑 vim /etc/environmentXMODIFIERS=@im=fcitx GTK_IM_MODULE=fcitx QT_IM_MODULE=fcitxvim /etc/profileexport XMODIFIERS=@im=fc…

Debian 13安装rime中文输入法

删除无关输入法,并安装fcitx5-rime sudo apt purge --autoremove fcitx* fcitx5* ibus* sudo apt install fcitx5-rime图形设置,启用输入法 安装白霜拼音输入方案和词库。重启 systemctl reboot本文来自博客园,作者…

[LabVIEW随笔-10] -基础知识3

1、列表框1.1 创建列表框列表框同简单控件一样,有“新式”、“系统”和“古典”三种不同风格。列表框控件位于“列表与表格”控件选板中列表框的外观与一般的字符串控件比较相似,列表框控件内含的数据类型是 132,它…

0_C++的基础语法(上)

今天这个C++基础语法上面部分我们就假设你有c语言基础,不会去重复讲C语言一样的内容,然后只讲算法比赛中会用到的内容,不讲类,对象之类的内容。本次不讲STL,STL部分我会放在下一篇部分讲的。恋爱可以不谈,算法不能…

linux进线程

进线程 1. 什么是进程,什么是线程,区别在那?进程是系统资源分配的基本单位,线程是程序执行的最小单位,一个进程可以包含多个线程 进程有自己独立的地址空间,每新建一个进程,系统都会给进程分配独立的地址空间,…

Codeforces Round 1069 (Div. 2)

因为各种原因,最近几天的做题量与强度有所下降,时间不多了希望未来可以更加努力,以此为戒 B 这一套的难度感觉划分的不太对,这个B题反而比C题难得多 这个B题的思维还是挺巧妙的,显然是个构造,我竟然没有想出来 异…

Old-Java类集框架随笔

结构 Map ├── HashMap │ └── LinkedHashMap ├── SortedMap (接口) │ └── TreeMap └── Hashtable (遗留类,线程安全) Collection ├── List(ListIterator可用) │ ├── ArrayList │ ├…

第51天(中等题 数据结构)

打卡第五十一天 2道中等题题目:思路:前缀和+哈希表,同余定理 代码: int subarraysDivByK(vector<int>& nums, int k) {unordered_map<int,int> cnt; // 哈希表int ans = 0, s = 0; // 答…

对《AI元人文构想:从“伦理规范”向“技术合标”的范式扩展》的评价与展望(深化版)

迈向“全域可靠性”的工程宣言:一篇深度评述 对《AI元人文构想:从“伦理规范”向“技术合标”的范式扩展》的评价与展望 引言:一次理论-工程范式的关键跃迁 《AI元人文构想:从“伦理规范”向“技术合标”的范式扩展…

Claude使用体验

1.使用cli版本的Claude 安装 首先安装好node和npm,直接下载即可 https://nodejs.org/en 然后安装Claude clinpm install -g @anthropic-ai/claude-code --registry=https://registry.npmjs.org/安装完成就是这样查看C…

适合 Linux 强者的方式 freertos学习

适合 Linux 强者的方式 freertos学习✅ 适合 Linux 强者的方式 Step 1:对标理解LinuxFreeRTOSpthread xTaskmutex xSemaphoresignal task notifyIRQ ISRscheduling priority👉 把 FreeRTOS 当 “没有 MMU 的 Linux”…

网络故障具体排错

还是从物理层进行排查,查看crc循环冗余校验码是否持续增加,那么可能是物理线路异常,光衰过大,光模块插入异常,查看内存,风扇,cpu等设备问题可以通过命令进行自检,如dis cpu ,dis mamary ,dis fan 然后查看二…

Git 中文文件名显示为转义码(乱码)的解决方案

问题描述 在使用 Git 时,如果文件名包含中文或其他非 ASCII 字符,执行 git status 等命令时会看到类似这样的输出: $ git status On branch main Untracked files:(use "git add <file>..." to inc…

真相:大量硬件场景 根本跑不了 Linux

真相:大量硬件场景 根本跑不了 Linux2️⃣ 真相:大量硬件场景 根本跑不了 Linux 你以后真做设备,会遇到这些情况:场景LinuxFreeRTOS成本 < 20 MCU ❌ ✅启动 < 50ms ❌ ✅512KB Flash ❌ ✅精确定时 ⚠️ ✅…

Windows-GameBar-ErrorLog

Windows-GameBar-ErrorLog导航 (返回顶部)1. ms-gamingoverlay1.1 安装相关应用及组件 1.2 注册表关闭 1.3 注册表关闭-方式2 1.4 组策略禁用2. Xbox Game Bar2.1 xbox地区问题3. Reg-help1. ms-gamingoverlay1.1 安装…

S7-PLCSIM Advanced V6.0同网段不同电脑之间网络配置

场景: 电脑A:安装了TIA Portal V19和S7-PLCSIM Advanced V6.0。 电脑B:使用wpf上位机通过ModbusTcp/S7与S7-PLCSIM Advanced V6.0通信。 两台电脑IP为同网段,需要将S7-PLCSIM Advanced V6.0仿真的PLC的IP设置为同一…