Street Directions ZOJ - 1186

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.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


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

1

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算法

AC的C++程序如下:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<cstdio>
using namespace std;
const int maxn = 1010;
vector<int> g[maxn];
stack<int> s;
int dfs_clock, sccno[maxn], scc_no, pre[maxn], low[maxn];
int single[maxn][maxn];//城市改造之后的单向通道 
void dfs(int u,int w) //找强连通分量
{
    pre[u] = low[u] = ++dfs_clock;
    s.push(u);
    for (int i = 0; i < (int)g[u].size(); i++)
    {
        int v = g[u][i];
        if(!single[u][v]&&!single[v][u])
        {
        	single[u][v]=1;
		}
        if (!pre[v])
        {
            dfs(v,u);
            low[u] = min(low[u], low[v]);
            if(low[v]==pre[v])
            {
            	single[u][v]=1;
            	single[v][u]=1;
			}
        }
        else if (w!=v)
        {
            low[u] = min(low[u], pre[v]);
        }
    }
}
int main()
{
	int n,m,N;
	cin>>N;
	while(N--)
	{
		int cas=1;
	while(scanf("%d %d",&n,&m)&&(n||m))
	{
		printf("%d\n\n",cas++); 
	   int src, dest;
	   for(int i=0;i<=n;i++) g[i].clear();
    for (int i = 1; i <= m; i++)
    {
        cin >> src >> dest;
        g[src].push_back(dest);
        g[dest].push_back(src);
    }
    memset(sccno, 0, sizeof(sccno));
    memset(pre, 0, sizeof(pre));
    memset(low, 0, sizeof(low));
    memset(single,0,sizeof(single));
    dfs_clock = 0, scc_no = 0;
    for (int i = 1; i <= n; i++)
    {
        if (!pre[i]) dfs(i,i);
    }
     for(int i=1;i<=n;i++)
     {
     	for(int j=1;j<=n;j++)
     	{
     		if(single[i][j]) cout<<i<<" "<<j<<endl;
		 }
	 }
	 cout<<"#"<<endl;
	}
	if(N) cout<<endl;
}
 } 

猜你喜欢

转载自blog.csdn.net/jinduo16/article/details/87193641
ZOJ