Street Directions

Time Limit: 3000ms
Memory Limit: 131072KB

This problem will be judged on UVALive. Original ID: 5412
64-bit integer IO format: %lld      Java class name: Main

 

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 ( ), and the number of streets ism. 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  is present,  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  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  and  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

 
解题:题目的意思是说将一个无向图改成有向图,使其成为强连通,输出所有的边。我们可以求无向图的边双连通分量,对于同一个双连通分量,只需保留单边即可构成强连通,而不同的双连通分量则需保留双向边
 
边双连通分量
 
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
const int maxn = ;
struct arc {
int to,next;
bool vis;
arc(int x = ,int y = -) {
to = x;
next = y;
vis = false;
}
} e[];
int dfn[maxn],low[maxn],belong[maxn],idx,bcc;
int head[maxn],tot,n,m;
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
stack<int>stk;
void tarjan(int u,int fa) {
dfn[u] = low[u] = ++idx;
stk.push(u);
bool flag = false;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to == fa && !flag) {
flag = true;
continue;
}
if(!dfn[e[i].to]) {
tarjan(e[i].to,u);
low[u] = min(low[u],low[e[i].to]);
} else low[u] = min(low[u],dfn[e[i].to]);
}
if(low[u] == dfn[u]) {
int v;
bcc++;
do {
belong[v = stk.top()] = bcc;
stk.pop();
} while(v != u);
}
}
bool vis[maxn];
void dfs(int u,int fa) {
vis[u] = true;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to == fa) continue;
if(belong[u] == belong[e[i].to] && !e[i].vis)
printf("%d %d\n",u,e[i].to);
else if(belong[u] != belong[e[i].to] && !e[i].vis) {
printf("%d %d\n",u,e[i].to);
printf("%d %d\n",e[i].to,u);
}
e[i].vis = e[i^].vis = true;
if(!vis[e[i].to]) dfs(e[i].to,u);
}
}
void init() {
for(int i = ; i < maxn; ++i) {
head[i] = -;
dfn[i] = belong[i] = ;
vis[i] = false;
}
tot = idx = bcc = ;
while(!stk.empty()) stk.pop();
}
int main() {
int u,v,kase = ;
while(scanf("%d%d",&n,&m),n||m){
init();
for(int i = ; i < m; ++i){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
tarjan(,-);
printf("%d\n\n",kase++);
dfs(,-);
puts("#");
}
return ;
}

UVALive 5412 Street Directions的更多相关文章

  1. UVA 610 - Street Directions(割边)

    UVA 610 - Street Directions option=com_onlinejudge&Itemid=8&page=show_problem&category=5 ...

  2. POJ 1515 Street Directions --一道连通题的双连通和强连通两种解法

    题意:将一个无向图中的双向边改成单向边使图强连通,问最多能改多少条边,输出改造后的图. 分析: 1.双连通做法: 双连通图转强连通图的算法:对双连通图进行dfs,在搜索的过程中就能按照搜索的方向给所有 ...

  3. UVA610 - Street Directions(Tarjan)

    option=com_onlinejudge&Itemid=8&category=153&page=show_problem&problem=551"> ...

  4. POJ 1515 Street Directions

    题意: 一幅无向图  将尽量多的无向边定向成有向边  使得图强连通  无向图保证是连通的且没有重边 思路: 桥必须是双向的  因此先求边双连通分量  并将桥保存在ans中 每一个双连通分量内的边一定都 ...

  5. POJ 1515 Street Directions (边双连通)

    <题目链接> 题目大意: 有m条无向边,现在把一些边改成有向边,使得所有的点还可以互相到达.输出改变后的图的所有边(无向边当成双向的有向边输出). 解题分析: 因为修改边后,所有点仍然需要 ...

  6. 【转】Tarjan&LCA题集

    转自:http://blog.csdn.net/shahdza/article/details/7779356 [HDU][强连通]:1269 迷宫城堡 判断是否是一个强连通★2767Proving ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. cf475B Strongly Connected City

    B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  9. HDU 2722 Here We Go(relians) Again (spfa)

    Here We Go(relians) Again Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/ ...

随机推荐

  1. 使用jekyll配置一个自己的blog

    使用coding.net上提供的pages服务来配置一个自己的站点 提示:下载这些软件,最好能FQ,有些链接是国外的,淘宝的ruby镜像已经不提供服务了 1. 安装Ruby 2. 安装Rubygems ...

  2. cogs 306. [SGOI] 糊涂的记者

    306. [SGOI] 糊涂的记者 ★★★   输入文件:sign.in   输出文件:sign.out   评测插件时间限制:1 s   内存限制:128 MB [问题描述] 在如今的信息社会中,时 ...

  3. [笔记][Java7并发编程实战手冊]3.4 等待多个并发事件的完毕CountDownLatch倒计数闭锁

    [笔记][Java7并发编程实战手冊]系列文件夹 简单介绍 本文学习CountDownLatch 倒计数闭锁. 本人英文不好.靠机器翻译,然后有一段非常形象的描写叙述,让我把它叫为倒计数 用给定的计数 ...

  4. 【CareerCup】Trees and Graphs—Q4.3

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/24744177     题目: Given a sorted (increasing ord ...

  5. 【简单的案例分享,停机10分钟】10204升级CRS&amp;DB的PSU至102044

    发现一个现象,AIX5.3+HACMP+10.2.0.4RAC+RAW的环境,执行五六年的数据库crsd.log都会报下面错误: ----------------------------------- ...

  6. Android之应用开发基础

    Android应用开发基础 英文地址:http://developer.android.com/guide/components/fundamentals.html 本人英语水平不高,如有翻译不当请指 ...

  7. unity3d Pathfinding插件使用

    Overview The central script of the A* Pathfinding Project is the script 'astarpath.cs', it acts as a ...

  8. 对python变量的理解

    #!/usr/bin/python class Person: '''some words content or descriptions!''' name='luomingchuan' _age = ...

  9. web前端简单布局

    jquery实现的计算器

  10. HIT Software Construction Lab 3

    ​ 2019年春季学期 计算机学院<软件构造>课程 Lab 3实验报告 姓名 刘帅 学号 班号 1703008 电子邮件 1609192321@qq.com 手机号码 目录 1 实验目标概 ...