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

60 lines
1.7 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 = 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;
}