My algorithm is
I find all the bridges ,store the orientation bridge with (x,y) as orient[x][y]=orient[y][x]=1
For every other edge in DFS if x is parent of y , then orient[x][y]=1.
All the other unmarked edges in arr[j] (adjacency matrix) is orient[y][x]=1;
Any inputs for which this fails.
Code: Select all
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAX 1010
int N, E;
int arr[MAX][MAX], orient[MAX][MAX];
int visit[MAX], low[MAX], num[MAX], tme;
/* tme is time
* orient is whether the edge runs from x -> y or y->x
* For a bridge it is both
*
* */
/*Find the bridges in the graph */
void
dfs (int x, int par, int isroot)
{
num[x] = ++tme;
visit[x] = 1;
low[x] = num[x];
int y;
for (y = 1; y <= N; y++)
{
if (y != par)
{
if (arr[x][y])
{
if (!visit[y])
{
/* y is successor of x in dfs , go from x->y */
orient[x][y] = 1;
dfs (y, x, false);
if (low[y] == num[y]) //&& low[i]>=num[i])
{
// printf ("x,y=%d,%d\n", x, y);
orient[x][y] = 1;
orient[y][x] = 1;
}
low[x] = min (low[x], low[y]);
}
else
{
low[x] = min (low[x], num[y]);
}
}
}
}
}
int
main ()
{
int tc = 0;
while (scanf (" %d %d", &N, &E) != EOF)
{
//if(f) printf("\n");
if (!N && !E)
break;
++tc;
memset (arr, 0, sizeof (arr));
memset (orient, 0, sizeof (orient));
memset (visit, 0, sizeof (visit));
memset (low, 0, sizeof (low));
memset (num, 0, sizeof (num));
tme = 0;
int x, i, y, j;
for (j = 0; j < E; j++)
{
scanf (" %d %d", &x, &y);
arr[x][y] = 1;
arr[y][x] = 1;
}
for (i = 1; i <= N; i++)
if (!visit[i])
{
dfs (i, i, true);
}
printf ("%d\n\n", tc);
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
if (arr[i][j])
{
if (!orient[i][j])
orient[j][i] = 1;
}
}
}
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
if (orient[i][j])
{
printf ("%d %d\n", i, j);
if ((i != j) && orient[j][i])
{
printf ("%d %d\n", j, i);
orient[j][i] = 0;
}
}
}
}
printf ("#\n");
}
return 0;
}