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