You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}