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

77 lines
2.4 KiB
Markdown
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.

## [$NC106112$ $Street$ $Directions$](https://www.luogu.com.cn/problem/UVA610)
### 一、题目描述
现在有一个联通的无向图,我们要把整个图改造为有向图,在保证强连通的情况下使得双向边尽可能少。
### 二、解题思路
- ① 桥不能被改成单向边,因为会使得某一个方向断开
- ② 除了桥之外的边会构成若干个双连通图,那么我们按照$dfs$ 的顺序把这里面的边改成单向边,那么这个双连通图就会变成一个强连通图。
**总结**
- 桥,输出两条边
- 不是桥,输出一条边,成对变换的另一个不能输出需要标识
### 三、$Code$
```cpp {.line-numbers}
#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;
}
```