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

61 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;
//割边
struct Node {
int x, y;
bool const operator<(const Node &t) const {
if (x == t.x) return y < t.y;
return x < t.x;
}
};
vector<Node> v;
//边双模板
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], stk[N], timestamp, top;
int n, m;
void tarjan(int u, int from) {
dfn[u] = low[u] = ++timestamp;
stk[++top] = u;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (!dfn[j]) {
tarjan(j, i); // j:点,i:哪条边
low[u] = min(low[u], low[j]);
if (dfn[u] < low[j]) v.push_back({u, j});
} else if (i != (from ^ 1))
low[u] = min(low[u], dfn[j]);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("P1656.in", "r", stdin);
#endif
scanf("%d %d", &n, &m);
memset(h, -1, sizeof h);
while (m--) {
int a, b;
scanf("%d %d", &a, &b);
if (a != b) add(a, b), add(b, a);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i, -1);
//要求先按a排序,然后按b排序
sort(v.begin(), v.end());
for (auto i : v) printf("%d %d\n", i.x, i.y);
return 0;
}