思路
其实可以发现除桥以外的边都是可以换成有向边的。那么只需求出桥即可。
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e4 + 5, M = 1e5 + 5;
struct Edge {
int u, v, y, f;
} adj[M * 2];
int cnt1 = 1, h[N], dfn[N], low[N], tot, vis[M * 2];
void UP(int u, int v) {
cnt1++;
adj[cnt1].u = u;
adj[cnt1].v = v;
adj[cnt1].y = h[u];
h[u] = cnt1;
}
void tarjan(int x, int fa) {
dfn[x] = low[x] = ++tot;
for (int i = h[x]; i; i = adj[i].y) {
int y = adj[i].v;
if (!dfn[y]) {
tarjan(y, x);
low[x] = min(low[x], low[y]);
if (low[y] > dfn[x]) {
adj[i].f = adj[i ^ 1].f = 1;
}
}
else if (y != fa) {
low[x] = min(low[x], dfn[y]);
}
}
}
void Cout(int x) {
for (int i = h[x]; i; i = adj[i].y) {
if (vis[i]) continue;
vis[i] = vis[i ^ 1] = 1;
if (adj[i].f) {
cout << adj[i].u << " " << adj[i].v << '\n';
cout << adj[i].v << " " << adj[i].u << '\n';
}
else {
cout << adj[i].u << " " << adj[i].v << '\n';
}
Cout(adj[i].v);
}
}
int main() {
int n, m, op = 0;
while (cin >> n >> m && n && m) {
tot = 0;
cnt1 = 1;
memset(h, 0, sizeof(h));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(vis, 0, sizeof(vis));
memset(adj, 0, sizeof(adj));
op++;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
UP(x, y);
UP(y, x);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) {
tarjan(i, i);
}
}
cout << op << "\n\n";
Cout(1);
cout << "#\n";
}
return 0;
}