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. html5 过程解决问题收集

    1.使用画布时报错误: Uncaught TypeError: Object [object Object] has no method 'getContext' . 解决方法: $(function ...

  2. BA-siemens-insight时间表设置

    时间表问题汇总: 如果遇到这种问题,显示"unable to locate databse object",就使用database transfer上传一边所有的模块信息,然后在操 ...

  3. spring是怎样管理mybatis的及注入mybatis mapper bean的

    1.spring启动mybatis的两个重要类:SqlSessionFactoryBean和MapperFactoryBean,这两个类都是org.mybatis.spring jar包的. 是用来启 ...

  4. POJ 2184

    简单的01背包,把S看体积,把F看价值,把它们变正数处理就可以了.在处理负数时,因为减一个负数相当于加一个,所以要从前往后. #include <iostream> #include &l ...

  5. POJ 1944

    明天补上... 这道题的思路确实很精致.考虑到连的边肯定不会是一个环,所以至少有一个断点.于是,可以枚举这个断点.断点一确定,那么连边的走向也就确定了.用D[i]表示由i开始可以到达的最远点即可.对于 ...

  6. Android 经常使用设计模式(一)

    由于项目变更的频繁性,作为一名程序猿,我们须要掌握设计模式的必要性.就不言而喻~~.以下就是一些我自己学习的设计模式总结. 接下来,主要是针对几个比較经常使用模式进行解说,主要是以下几种: 观察者模式 ...

  7. centos下yum安装lamp和lnmp轻松搞定

    centos下yum安装lamp和lnmp轻松搞定.究竟多轻松你看就知道了.妈妈再也不操心不会装lamp了. 非常辛苦整理的安装方法,会持续更新下去.凡无法安装的在评论里贴出问题来,会尽快解决.共同维 ...

  8. UVA 10539 - Almost Prime Numbers 素数打表

    Almost prime numbers are the non-prime numbers which are divisible by only a single prime number.In ...

  9. Java-MyBatis:MyBatis XML 文件

    ylbtech-Java-MyBatis:MyBatis XML 文件 1.返回顶部 1. Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大, ...

  10. 19.QT对话框(文件对话框,颜色对话框,字体框,自定义对话框)

    文件对话框 #include<QFileDialog> //文件对话框 void Dialog::on_pushButton_clicked() { //定义显示文件的类型 窗口标题 可供 ...