Description
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.
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.
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 #
题意:已知一堆双向边,要求把尽可能多的双向边改成单向边,但任意两点仍然可以互相到达。
解题思路:除割边需要双向,其他的边按dfs顺序即可。
代码:
/* *********************************************** Author :xianxingwuguan Created Time :2014/1/18 18:59:49 File Name :F.cpp ************************************************ */ #pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; const int maxn=2222; int head[maxn],tol,low[maxn],dfn[maxn],Stack[maxn],indexx,top,n; int mp[maxn][maxn]; int vis[maxn]; struct node { int next,to,cut,xiang; }edge[300000]; void add(int u,int v) { edge[tol].to=v; edge[tol].cut=-1; edge[tol].next=head[u]; head[u]=tol++; } void tarjin(int u,int pre) { low[u]=dfn[u]=++indexx; Stack[top++]=u; int v; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(v==pre)continue; if(edge[i].cut!=-1)continue; edge[i].cut=1; edge[i^1].cut=0; if(!dfn[v]) { tarjin(v,u); if(low[u]>low[v]) low[u]=low[v]; if(low[v]>dfn[u]) { edge[i].cut=1; edge[i^1].cut=1; } } else if(low[u]>dfn[v]) low[u]=dfn[v]; } } int main() { //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int i,j,k,m; int ncase=0; while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0)break; memset(head,-1,sizeof(head)); tol=0; while(m--) { scanf("%d%d",&i,&j); add(i,j); add(j,i); } printf("%d\n\n",++ncase); memset(dfn,0,sizeof(dfn)); indexx=top=0; for(i=1;i<=n;i++) if(!dfn[i])tarjin(i,i); for(i=0;i<tol;i++)if(edge[i].cut==1)printf("%d %d\n",edge[i^1].to,edge[i].to); puts("#"); } return 0; }