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. Mysql提升大数据导入速度的绝妙方法

    一.对于Myisam类型的表,可以通过以下方式快速的导入大量的数据.      ALTER TABLE tblname DISABLE KEYS;     loading the data     A ...

  2. BZOJ 3674 可持久化并查集加强版(路径压缩版本)

    /* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...

  3. L - 还是畅通工程

    L - 还是畅通工程   思路:水! #include<cstdio> #include<cstring> #include<iostream> #include& ...

  4. WifiManager类具体解释

    public class WifiManager extends Object java.lang.Object    ↳ android.net.wifi.WifiManager 类概述 This ...

  5. 用PHP去实现静态化

    我们在PHP站点开发过程中为了站点的推广或者SEO的须要,须要对站点进行一定的静态化,这里设计到什么是静态页面,所谓的静态页面.并非页面中没有动画等元素,而是指网页的代码都在页面中,即不须要再去执行P ...

  6. CentOS6.5下安装远程桌面服务端软件VNC Server

    VNC 使您能够远程訪问和控制您的计算机从还有一计算机或移动设备上,不管你在世界的不论什么地方. 常见的使用情形,包含给同事和朋友提供桌面支持.远程管理您的服务器. 将 VNC Server部署到您想 ...

  7. ACM-SG函数之Fibonacci again and again——hdu1848

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  8. HDFS学习笔记(1)初探HDFS

    Hadoop分布式文件系统(Hadoop Distributed File System, HDFS) 分布式文件系统是一种同意文件通过网络在多台主机上分享的文件系统.可让多机器上的多用户分享文件和存 ...

  9. Chisel Tutorial(一)——Chisel介绍

    Chisel是由伯克利大学公布的一种开源硬件构建语言,建立在Scala语言之上,是Scala特定领域语言的一个应用,具有高度參数化的生成器(highly parameterized generator ...

  10. hdu5386 Cover

    Problem Description You have an n∗n matrix.Every grid has a color.Now there are two types of operati ...