题意
有一张道路交通图,每条边都是双向的,且连通。现在想尽量把一些多的道路改成单向的,且任意两点是可达的,输出改造后的道路图。
分析
同一个双连通分量里面的边都是可以通过重定向使得相互可达,可以改成单向边。桥边就只能是双向的了。
const int maxn = 1010;
int dfn[maxn], low[maxn], depth;
int belong[maxn], block;
stack<int> st;
bool in[maxn];
vector<ii> bridge;
vector<int> G[maxn];
bool vis[maxn][maxn];
void tarjan(int u,int fa) {
dfn[u] = low[u] = ++depth;
st.push(u);
in[u] = true;
bool had = false;
for (int i = 0;i < G[u].size();++i) {
int v = G[u][i];
if (v == fa && !had) {had = true;continue;}
if (dfn[v] == -1) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (dfn[u] < low[v]) {
bridge.push_back(ii(u, v));
bridge.push_back(ii(v, u));
vis[u][v] = vis[v][u] = true;
}else {
bridge.push_back(ii(u, v));
vis[u][v] = vis[v][u] = true;
}
}else if (in[v]) {
low[u] = min(low[u], dfn[v]);
if (!vis[u][v]) {
bridge.push_back(ii(u, v));
vis[u][v] = vis[v][u] = true;
}
}
}
}
int main(int argc, const char * argv[])
{
int n, m;
while(~scanf("%d%d", &n, &m) && n + m) {
if (n == 0 && m == 0) break;
for (int i = 0;i <= n + 1;++i)
G[i].clear();
bridge.clear();
int u, v;
while(m--) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
memset(in, false,sizeof in);
memset(dfn, -1,sizeof dfn);
memset(vis, false,sizeof vis);
depth = 0;
tarjan(1, -1);
printf("%d\n\n", ++nCase);
sort(bridge.begin(), bridge.end());
for (int i = 0;i < bridge.size();++i) {
printf("%d %d\n", bridge[i].first, bridge[i].second);
}
puts("#");
}
return 0;
}