Files
python/TangDou/AcWing/DCC/P1656.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

53 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10, M = 4e6 + 10;
typedef pair<int, int> PII;
#define x first
#define y second
// 链式前向星
int e[M], h[N], idx, w[M], ne[M];
void add(int a, int b, int c = 0) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
int dfn[N], low[N], ts, root;
set<PII> _set;
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++ts;
// 对比有向图强连通分量的代码,这里没有入栈的操作,原因是本题只想求割边,并不是想缩点,
// 怀疑缩点时就需要加入stk,in_stk等操作了,待验证
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (v == fa) continue;
if (!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
// 记录割边,论证过的结论
if (low[v] > dfn[u]) _set.insert({u, v}); // SET天生会排序,按first由小到大
} else
low[u] = min(low[u], dfn[v]);
}
}
int n, m;
int main() {
#ifndef ONLINE_JUDGE
freopen("P1656.in", "r", stdin);
#endif
memset(h, -1, sizeof h);
scanf("%d %d", &n, &m);
while (m--) {
int a, b;
scanf("%d %d", &a, &b);
if (a != b) add(a, b), add(b, a);
}
for (root = 1; root <= n; root++)
if (!dfn[root]) tarjan(root, -1);
for (auto i : _set) printf("%d %d\n", i.x, i.y);
return 0;
}