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.

60 lines
1.7 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 = 1010, M = N << 2;
int ts;
int dfn[N], low[N];
// 链式前向星
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 out[M]; // 是不是成对变换输出过
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++ts;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (v == fa) continue;
if (out[i]) continue; // 对边已经输出过那么这条反边不能输出。因为如果是割边的话两条边在u->v时就都已经输出完了
out[i] = out[i ^ 1] = 1; // 标识成对变换输出过
printf("%d %d\n", u, v); // 输出u->v,同时,需要检查 v->u是桥的话还输出v->u
if (!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (dfn[u] < low[v]) // 割边
printf("%d %d\n", v, u); // 割边需要输出两条
} else
low[u] = min(low[u], dfn[v]);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("UVA610.in", "r", stdin);
#endif
int n, m, cas = 0;
while (scanf("%d%d", &n, &m), n + m) {
idx = ts = 0;
memset(h, -1, sizeof h);
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low)); // Tips:有些人的代码low也是可以不用清空的
memset(out, 0, sizeof out);
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
printf("%d\n\n", ++cas);
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i, -1);
puts("#");
}
return 0;
}