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. C/C++ 图像二进制存储与读取

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50782792 在深度学习时,制作样本数 ...

  2. Hadoop集群(第11期)_常用MySQL数据库命令

    1.系统管理 1.1 连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 举例: 例1:连接到本机上的MySQL. 首先在打开DOS窗口,然后进入目录 mysqlbin,再键入 ...

  3. [Beginning SharePoint Designer 2010]Chapter4 发布页面

    本章概要: 1.SharePoint中的Web内容管理 2.SharePoint发布系统的特性 3.SharePoint发布页面的组成 4.母板页 5.如何构建页面布局和他们潜在的内容类型

  4. xml解析之----DOM解析

    DOM模型(documentobject model) •DOM解析器在解析XML文档时,会把文档中的全部元素.依照其出现的层次关系.解析成一个个Node对象(节点). •在dom中.节点之间关系例如 ...

  5. Unity3D中C#和JS的方法互相調用

    因为Unity3D中一些腳本的方法仅仅能用在JS中.在C#中是無效的,而C#能够與服務器端通訊,JS本身卻不行.所以勢必會遇到這兩種語言腳本中方法的互相調用,下面是演示样例. 兩個文件 test1.j ...

  6. 单机 &amp; 弱联网手游 防破解、金币改动 简单措施

    手游经常使用破解方法 对于一个弱联网或者单机游戏,能够从下面方面去破解: 1.找得到存档文件的,直接破解改动存档文件. 2.找不到存档文件,就在游戏执行时借助一些软件来改动数值,比方用各种改动器手游助 ...

  7. 0x01 位运算

    都比较基础吧. 知识点 1.快速幂和快速乘(这里有一个用long double舍弃精度的做法,但是感觉既不稳又没用) 2.懒人写边目录的时候的k^1 3.lowbit,得到的是低到高第一个1的位.求一 ...

  8. Linux学习之基本介绍

    技术不分年龄高低,只分水平高低. 搞技术25k以下是不看天赋的,25k以上是要看天赋的. 1U服务器,2U服务器,刀片服务器.程序都是运行在服务器上的. 榜样的力量是无穷的.--MK. 汇编语言跟硬件 ...

  9. phpStorm中ftp的配置与使用,支持配置多个

    小结:很方便,支持ftp功能和比较. 扩展,可以查看远程文件和日期 配置多个ftp

  10. java多线程 interrupt(), interrupted(), isInterrupted()方法区别

    interrupt()方法: 作用是中断线程. 本线程中断自身是被允许的,且"中断标记"设置为true 其它线程调用本线程的interrupt()方法时,会通过checkAcces ...