版本比较
P2860 [USACO06JAN] Redundant Paths G 题解
正文
增加 42 行删除 39 行
37 行未改动38394041424344454647484950515253545556575859606162636465662 行未改动697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341355 行未改动141142143144145146147148149150151152153154121 行未改动全部展开37 行未改动```cpp#include <bits/stdc++.h>using namespace std;const int MAXN = 1e5 + 5; // 节点数上限const int MAXM = 2e5 + 5; // 边数上限(无向图要开双倍)// -------------------------- 链式前向星存图 --------------------------struct Edge {int to, nxt;} e[MAXM]; // 边数组int head[MAXN], tot; // 头数组、边计数器int n,m;// -------------------------- Tarjan 必备数组 --------------------------int dfn[MAXN]; // 节点的时间戳(访问顺序)int low[MAXN]; // 节点能回溯到的最早时间戳int tim; // 时间戳全局变量bool vis[MAXM]; // 标记边是否被访问过(无向图成对边)int stk[MAXN], top; // 栈 + 栈顶指针int col[MAXN]; // 每个节点所属的边双连通分量编号int cnt_col; // 边双连通分量的总个数// -------------------------- 缩点后统计 --------------------------int u[MAXM], v[MAXM]; // 存储所有输入的边int deg[MAXN]; // 缩点后每个分量的度数// -------------------------- 链式前向星加边 --------------------------void add_edge(int x, int y) {e[++tot].to = y;2 行未改动}// -------------------------- Tarjan 求边双连通分量 --------------------------void tarjan(int x) {dfn[x] = low[x] = ++tim; // 初始化当前节点的时间戳stk[++top] = x; // 当前节点入栈// 遍历 x 的所有邻边for (int i = head[x]; i; i = e[i].nxt) {int y = e[i].to; // 邻节点 yif (vis[i]) continue; // 这条边已经访问过,跳过(避免走回头边)vis[i] = vis[i ^ 1] = true; // 标记正反两条边都访问过if (!dfn[y]) { // y 还没被访问过(树边)tarjan(y); // 递归访问 ylow[x] = min(low[x], low[y]); // 用子节点更新当前节点 low 值} else { // y 已经被访问过(回边)low[x] = min(low[x], dfn[y]); // 用回边更新能到达的最早时间戳}}// -------------------------- 找到一个边双连通分量 --------------------------if (low[x] == dfn[x]) {cnt_col++; // 分量编号 +1// 弹栈,把当前边双里的所有节点标记为同一个编号while (stk[top] != x) {col[stk[top--]] = cnt_col;}col[x] = cnt_col; // 最后把 x 自己标记top--; // 栈顶弹出}}// -------------------------- 主函数 --------------------------int main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin >> n >> m; // 输入节点数 n,边数 mtot = 1; // 链式前向星从 1 开始存,方便异或找反向边// 输入 m 条无向边for (int i = 1; i <= m; i++) {cin >> u[i] >> v[i];add_edge(u[i], v[i]);add_edge(v[i], u[i]);}// 遍历所有节点,防止图不连通for (int i = 1; i <= n; i++) {if (!dfn[i]) {tarjan(i);}}// -------------------------- 缩点:统计每个分量的度数 --------------------------for (int i = 1; i <= m; i++) {int cu = col[u[i]]; // 起点所在分量int cv = col[v[i]]; // 终点所在分量if (cu != cv) { // 两点不在同一个分量 → 这条边是割边deg[cu]++; // 两个分量度数 +1deg[cv]++;}}5 行未改动}}// -------------------------- 输出答案 --------------------------// 叶子数 / 2 向上取整 → (leaf + 1) / 2cout << (leaf + 1) / 2 << endl;return 0;//The end}```注:此代码大部分注释由【豆包】生成,保证作者贡献大于豆沙包的注释求个赞呗~---121 行未改动全部展开37 行未改动38394041424344454647484950515253545556575859606162636465662 行未改动697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341355 行未改动141142143144145146147148149150151152153154155156157121 行未改动全部展开37 行未改动```cpp#include <bits/stdc++.h>using namespace std;const int MAXN = 1e5 + 5; // 节点数上限const int MAXM = 2e5 + 5; // 边数上限(无向图要开双倍)// -------------------------- 链式前向星存图 --------------------------struct Edge {int to, nxt;} e[MAXM]; // 边数组int head[MAXN], tot; // 头数组、边计数器int n,m;// -------------------------- Tarjan 必备数组 --------------------------int dfn[MAXN]; // 节点的时间戳(访问顺序)int low[MAXN]; // 节点能回溯到的最早时间戳int tim; // 时间戳全局变量bool vis[MAXM]; // 标记边是否被访问过(无向图成对边)int stk[MAXN], top; // 栈 + 栈顶指针int col[MAXN]; // 每个节点所属的边双连通分量编号int cnt_col; // 边双连通分量的总个数// -------------------------- 缩点后统计 --------------------------int u[MAXM], v[MAXM]; // 存储所有输入的边int deg[MAXN]; // 缩点后每个分量的度数// -------------------------- 链式前向星加边 --------------------------void add_edge(int x, int y) {e[++tot].to = y;2 行未改动}// -------------------------- Tarjan 求边双连通分量 --------------------------void tarjan(int x) {dfn[x] = low[x] = ++tim; // 初始化当前节点的时间戳stk[++top] = x; // 当前节点入栈// 遍历 x 的所有邻边for (int i = head[x]; i; i = e[i].nxt) {int y = e[i].to; // 邻节点 yif (vis[i]) continue; // 这条边已经访问过,跳过(避免走回头边)vis[i] = vis[i ^ 1] = true; // 标记正反两条边都访问过//0^1=1 1^1=0 2^1=3 3^1=2 ......if (!dfn[y]) { // y 还没被访问过(树边)tarjan(y); // 递归访问 ylow[x] = min(low[x], low[y]); // 用子节点更新当前节点 low 值} else { // y 已经被访问过(回边)low[x] = min(low[x], dfn[y]); // 用回边更新能到达的最早时间戳}}// -------------------------- 找到一个边双连通分量 --------------------------if (low[x] == dfn[x]) {cnt_col++; // 分量编号 +1// 弹栈,把当前边双里的所有节点标记为同一个编号while (stk[top] != x) {col[stk[top--]] = cnt_col;}col[x] = cnt_col; // 最后把 x 自己标记top--; // 栈顶弹出}}// -------------------------- 主函数 --------------------------int main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin >> n >> m; // 输入节点数 n,边数 mtot = 1; // 链式前向星从 1 开始存,方便异或找反向边// 输入 m 条无向边for (int i = 1; i <= m; i++) {cin >> u[i] >> v[i];add_edge(u[i], v[i]);add_edge(v[i], u[i]);}// 遍历所有节点,防止图不连通for (int i = 1; i <= n; i++) {if (!dfn[i]) {tarjan(i);}}// -------------------------- 缩点:统计每个分量的度数 --------------------------for (int i = 1; i <= m; i++) {int cu = col[u[i]]; // 起点所在分量int cv = col[v[i]]; // 终点所在分量if (cu != cv) { // 两点不在同一个分量 → 这条边是割边deg[cu]++; // 两个分量度数 +1deg[cv]++;}}5 行未改动}}// -------------------------- 输出答案 --------------------------// 叶子数 / 2 向上取整 → (leaf + 1) / 2cout << (leaf + 1) / 2 ;return 0;//The end}```注:此代码大部分注释由【豆包】生成,保证作者贡献大于豆沙包的注释求个赞呗~---121 行未改动全部展开