POJ[1515] Street Directions 桥

本文介绍了一种算法,用于将城市交通图中的双向街道尽可能多地转换为单向街道,同时确保从任意交叉路口出发都能到达其他所有交叉路口。该算法特别关注如何处理桥梁等关键路径,以维持整个交通网络的连通性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Street Directions
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1041 Accepted: 539 Special Judge

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. 

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
#

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

题意:  把尽量多的无向边定向,使得最终图保持强连通的特性.

桥必须得是双向边, 要不然相邻就不能互达双连通分量就不能互达. 其他边按dfs序(搞成一个环)就行了.

如果某点的dfn==low的话, 说明当前这点之下是双连通分量. 父亲边便一定是桥.

#include<stdio.h>
#include<cstring>
#include<algorithm>
#define clear(a) memset(a, 0, sizeof(a))
using namespace std;
const int maxn = 100005;
int n, m, num(1), idx, cas;
int h[maxn], fa[maxn], dfn[maxn], low[maxn];
struct edge{int nxt, v, u, mk;}e[maxn * 10];
inline void add(int u, int v){
	e[++num].v = v, e[num].nxt = h[u], e[num].u = u, e[num].mk = -1, h[u] = num;
	e[++num].v = u, e[num].nxt = h[v], e[num].u = v, e[num].mk = -1, h[v] = num;
}
void dfs(int u){
	dfn[u]  = low[u] = ++idx;
	for(int i = h[u]; i; i = e[i].nxt){
		if(i == (fa[u] ^ 1) || e[i].mk != -1) continue;
		int v = e[i].v;
		e[i].mk = 1, e[i ^ 1].mk = 0;
		if(!dfn[v]){
			fa[v] = i, dfs(v);
			low[u] = min(low[u], low[v]);
		}
		else low[u] = min(low[u], dfn[v]);
	}
	if(dfn[u] == low[u]) e[fa[u]].mk = e[fa[u] ^ 1].mk = 1;
}
int main(){
	while(true){
		num = 1, idx = 0;
		clear(h), clear(fa), clear(dfn), clear(low);
		scanf("%d%d", &n, &m);
		if(!n) break;
		for(int i = 1; i <= m; ++i){
			int u, v;
			scanf("%d%d", &u, &v);
			add(u, v);
		}
		for(int i = 1; i <= n; ++i)
			if(!dfn[i]) dfs(i);
		printf("%d\n\n", ++cas);
		for(int i = 2; i <= num; ++i)
			if(e[i].mk) printf("%d %d\n", e[i].u, e[i].v);
		puts("#");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值