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.

61 lines
1.4 KiB

2 years ago
#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;
}