According to the Automobile Collision Monitor (ACM), most fatal traffic accidents occur on two-way streets. In order to reduce the number of fatalities caused by traffic accidents, the mayor wants to convert as many streets as possible into one-way streets. You have been hired to perform this conversion, so that from each intersection, it is possible for a motorist to drive to all the other intersections following some route.
You will be given a list of streets (all two-way) of the city. Each street connects two intersections, and does not go through an intersection. At most four streets meet at each intersection, and there is at most one street connecting any pair of intersections. It is possible for an intersection to be the end point of only one street. You may assume that it is possible for a motorist to drive from each destination to any other destination when every street is a two-way street.
Input
The input consists of a number of cases. The first line of each case contains two integers n and m. The number of intersections is n (2 <= n <= 1000), and the number of streets is m. The next m lines contain the intersections incident to each of the m streets. The intersections are numbered from 1 to n, and each street is listed once. If the pair i j is present, j i will not be present. End of input is indicated by n = m = 0.
Output
For each case, print the case number (starting from 1) followed by a blank line. Next, print on separate lines each street as the pair i j to indicate that the street has been assigned the direction going from intersection i to intersection j. For a street that cannot be converted into a one-way street, print both i j and j i on two different lines. The list of streets can be printed in any order. Terminate each case with a line containing a single `#' character.
Note: There may be many possible direction assignments satisfying the requirements. Any such assignment is acceptable.
Sample Input
7 10 1 2 1 3 2 4 3 4 4 5 4 6 5 7 6 7 2 5 3 6 7 9 1 2 1 3 1 4 2 4 3 4 4 5 5 6 5 7 7 6 0 0
Sample Output
1 1 2 2 4 3 1 3 6 4 3 5 2 5 4 6 4 6 7 7 5 # 2 1 2 2 4 3 1 4 1 4 3 4 5 5 4 5 6 6 7 7 5 #
题意:无向的连通图,让你改为有向连通图,单向边尽可能多(无向边用两个单向边表示)
思路:桥无法用有向边表示,因此为无向边,其余用单向边,顺序按tarjan的搜索顺序来确定
#include <iostream>
#include <stack>
#include <vector>
#include <cstring>
using namespace std;
const int maxn=100010;
int dfn[maxn],low[maxn],b[maxn*2],head[maxn],ver[maxn*2],next[maxn*2],n,m,cnt,num;
void add(int x,int y)
{
ver[++cnt]=y;
next[cnt]=head[x];
head[x]=cnt;
}
void init()
{
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(b,0,sizeof(b));
memset(head,0,sizeof(head));
memset(next,0,sizeof(next));
memset(ver,0,sizeof(ver));
cnt=1;
num=0;
}
void tarjan(int x,int in_edge)
{
low[x]=dfn[x]=++num;
for(int i=head[x];i;i=next[i])
{
int y=ver[i];
if(b[i])
continue;
b[i]=1;
b[i^1]=0;//单向边
if(!dfn[y])
{
tarjan(y,i);
low[x]=min(low[x],low[y]);
if(low[y]>dfn[x])
b[i]=b[i^1]=1;//双向边
}
else if(i!=(in_edge^1))
low[x]=min(low[x],dfn[y]);
}
}
int main()
{
ios::sync_with_stdio(false);
int w=1;
while(cin>>n>>m&&n&&m)
{
init();
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
add(x,y);
add(y,x);
}
for(int i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i,0);
}
cout<<w<<endl;
cout<<endl;
for(int i=1;i<=cnt;i++)
{
if(b[i])
cout<<ver[i^1]<<" "<<ver[i]<<endl;
}
cout<<"#"<<endl;
w++;
}
return 0;
}