标签:
题目大意:给出一张无向图,任意两点可相互到达,现在要求你将这张无向图变成有向图,且改变之后任意两点还是可相互到达的
解题思路:桥的话肯定是要保留双向的,所以可以在dfs的时候把桥标记出来,顺便在dfs的时候标记一下使用的是哪条边即可
#include <cstdio>
#include <cstring>
#define N 1010
#define M 1000010
#define min(a,b) ((a) < (b) ? (a) : (b))
struct Edge{
int from, to, next, flag;
}E[M];
int head[N], pre[N], lowlink[N];
int tot, n, m, dfs_clock;
void AddEdge(int u, int v) {
E[tot].from = u;
E[tot].to = v;
E[tot].flag = 0;
E[tot].next = head[u];
head[u] = tot++;
}
void init() {
memset(head, -1, sizeof(head));
tot = 0;
int u, v;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
AddEdge(u, v);
AddEdge(v, u);
}
}
void dfs(int u, int fa) {
pre[u] = lowlink[u] = ++dfs_clock;
int v;
for (int i = head[u]; i != -1; i = E[i].next) {
v = E[i].to;
if (E[i].flag) continue;
E[i].flag = 1;
E[i ^ 1].flag = -1;
if (!pre[v]) {
dfs(v, u);
lowlink[u] = min(lowlink[u], lowlink[v]);
if (lowlink[v] > pre[u])
E[i ^ 1].flag = 1;
}
else if (v != fa)
lowlink[u] = min(lowlink[u], pre[v]);
}
}
int cas = 1;
void solve() {
memset(pre, 0, sizeof(pre));
dfs_clock = 0;
for (int i = 1; i <= n; i++)
if (!pre[i])
dfs(i, -1);
printf("%d\n\n", cas++);
int u, v;
for (int i = 0; i < tot; i++) {
u = E[i].from;
v = E[i].to;
if (E[i].flag == 1)
printf("%d %d\n", u, v);
}
printf("#\n");
}
int main() {
while (scanf("%d%d", &n, &m) != EOF && n + m) {
init();
solve();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ - 1515 Street Directions(无向图变有向图)
标签:
原文地址:http://blog.csdn.net/l123012013048/article/details/47690373