POJ - 1515(割边)

为了降低交通死亡事故,市长计划将大部分双向街道改为单向。给定城市的街道网络,任务是确保从每个交叉口出发都能通过单向路线到达所有其他交叉口。输入包含交叉口和街道信息,输出应为每条街道的单向方向。题目强调,双向街道在输出中需表示两次,方向相反。解决方案是识别割边并进行深度优先搜索确定方向。
摘要由CSDN通过智能技术生成

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
#

题目大意:

一个城市中有很多双向路,要求汽车可以从一个交叉路口到所有的路口,双向路需要变成单向路,变尽可能多的单向路。输出路的方向,双向路就输出两遍,方向不同。

思路:

双向路只有可能时图中的割边。所以找出割边,dfs出其他方向。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
#define ll long long
const int maxn = 1e3 + 5;
const int maxm = 1e6 + 5;
int n, m;
int dfs_clock;
int pre[maxn], low[maxn];

struct poin
{
	int from, to, next;
	int w;
}G[maxm];

int head[maxn], ans = 0;

void add(int u, int v, int w)
{
	G[ans].from = u;
	G[ans].to = v;
	G[ans].w = w;
	G[ans].next = head[u];
	head[u] = ans++;
}

int dfs(int u, int fa)
{
	int lowu = pre[u] = ++dfs_clock;
	for (int i = head[u]; ~i; i = G[i].next)
	{
		int v = G[i].to;
		if (fa == v)continue;
		if (G[i].w != -1)continue;
		G[i].w = 1;
		G[i ^ 1].w = 0;
		if (!pre[v])
		{
			int lowv = dfs(v, u);
			lowu = min(lowu, lowv);
			if (lowv > pre[u])  
			{
				G[i ^ 1].w = 1;
			}

		}
		else if (pre[v]<pre[u] && v != fa)
		{
			lowu = min(lowu, pre[v]);
		}
	}

	return low[u] = lowu;
}


int main()
{
	int T = 1;
	while (scanf("%d%d", &n, &m) && (n + m))
	{
		memset(pre, 0, sizeof(pre));
		memset(low, 0, sizeof(low));
		memset(head, -1, sizeof(head));
		ans = 0;
		for (int i = 1;i <= m;i++)
		{
			int u, v;
			scanf("%d%d", &u, &v);
			add(u, v, -1);
			add(v, u, -1);
		}


		dfs_clock = 0;
		for (int i = 1;i <= n;i++)
			if (!pre[i])dfs(i, -1);

		printf("%d\n\n", T++);
		for (int i = 0;i < ans;i++)
		{
			if (G[i].w)
			{
				printf("%d %d\n", G[i].from, G[i].to);
			}
		}
		printf("#\n");

	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值