63 lines
1.4 KiB
C++
63 lines
1.4 KiB
C++
#include <bits/stdc++.h>
|
||
|
||
using namespace std;
|
||
|
||
const int N = 5e5 + 10, M = 4e6 + 10;
|
||
|
||
//链式前向星
|
||
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++;
|
||
}
|
||
|
||
//割边
|
||
struct Node {
|
||
int x, y;
|
||
bool const operator<(const Node &t) const {
|
||
if (x == t.x) return y < t.y;
|
||
return x < t.x;
|
||
}
|
||
};
|
||
|
||
int dfn[N], low[N], timestamp, root;
|
||
vector<Node> v;
|
||
|
||
void tarjan(int u, int fa) {
|
||
dfn[u] = low[u] = ++timestamp;
|
||
int son = 0;
|
||
for (int i = h[u]; ~i; i = ne[i]) {
|
||
int j = e[i];
|
||
if (j == fa) continue;
|
||
if (!dfn[j]) {
|
||
son++;
|
||
tarjan(j, u);
|
||
low[u] = min(low[u], low[j]);
|
||
//记录割边
|
||
if (low[j] > dfn[u]) v.push_back({u, j});
|
||
}
|
||
low[u] = min(low[u], dfn[j]);
|
||
}
|
||
}
|
||
|
||
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, root);
|
||
|
||
//要求先按a排序,然后按b排序
|
||
sort(v.begin(), v.end());
|
||
for (auto i : v) printf("%d %d\n", i.x, i.y);
|
||
return 0;
|
||
} |