option=com_onlinejudge&Itemid=8&category=153&page=show_problem&problem=551">题目链接

题意: 给出一张无向图,尽量多的使边成为单向边。改变之后的图仍然强连通。

思路:找出全部的桥。桥肯定是不能改变成为单向边。之后不是桥的边能组成n个连通块。依照dfs的顺序规定方向就可以。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <utility>
#include <algorithm> using namespace std; const int MAXN = 10005; struct Edge{
int to, next;
bool cut, vis;
}edge[MAXN * 10]; int head[MAXN], tot;
int Low[MAXN], DFN[MAXN];
int Index, top;
int bridge; vector<pair<int, int> > ans; void addedge(int u, int v) {
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].vis = false;
head[u] = tot++;
} void Tarjan(int u, int pre) {
int v;
Low[u] = DFN[u] = ++Index;
for (int i = head[u]; i != -1; i = edge[i].next) {
v = edge[i].to;
if (v == pre) continue;
if (edge[i].vis) continue;
edge[i].vis = edge[i^1].vis = true;
ans.push_back(make_pair(u, v));
if (!DFN[v]) {
Tarjan(v, u);
if (Low[u] > Low[v]) Low[u] = Low[v];
if (Low[v] > DFN[u]) {
ans.push_back(make_pair(v, u));
}
}
else if (Low[u] > DFN[v])
Low[u] = DFN[v];
}
} void init() {
memset(head, -1, sizeof(head));
memset(DFN, 0, sizeof(DFN));
tot = Index = 0;
} int main() {
int n, m, t = 1;
while (scanf("%d%d", &n, &m)) {
if (n == 0 && m == 0) break;
init();
int u, v;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
addedge(u, v);
addedge(v, u);
} ans.clear();
for (int i = 1; i <= n; i++) {
if (!DFN[i])
Tarjan(i, i);
} printf("%d\n\n", t++);
for (int i = 0; i < ans.size(); i++)
printf("%d %d\n", ans[i].first, ans[i].second);
printf("#\n");
}
return 0;
}

UVA610 - Street Directions(Tarjan)的更多相关文章

  1. UVALive 5412 Street Directions

    Street Directions Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. ...

  2. UVA 610 - Street Directions(割边)

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

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

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

  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. //相当于深拷贝一份dataArray。这样才不会改变dataArray本身的值

    //相当于深拷贝一份dataArray.这样才不会改变dataArray本身的值 NSMutableArray* commitDataArray = [NSKeyedUnarchiver unarch ...

  2. html 浮动元素

    在CSS布局中分为内联元素(display:inline)和块状元素(display:block),块状元素默认会占据一行,可设置高度宽度以及边距,而内联元素不会也不能设置.常见的内联元素有:a.sp ...

  3. IntelliJ IDEA 出现Unable to execute DX 问题

    最近从eclipse转移到IDEA上写代码的时候,无意间发现一个让我头疼不已的问题,message报错为 Error:Android Dex: [**] Unable to execute DX Er ...

  4. html5 存储篇(一)

    localStorage 和 sessionStorage      localStorage 与 sessionStorage的相同点:         (1).都是用于客户端存储的技术,相对于传统 ...

  5. Python之登陆接口设计

    刚刚开始学习Python,第一个编写的程序. import os user_file = open('use_file.txt', 'r') user_list = user_file.readlin ...

  6. 论left-pad的实现

    这两天微博上看到左耳朵耗子吐槽了一下node社区的left-pad的代码,原po链接 我也思考了一下 怎么用实现一个left-pad比较合适,上图代码确实比较搓 leftpad功能,就是字符串前面拼指 ...

  7. poj 2398 计算几何

    #include <iostream> #include<cstdio> #include<cstring> #include <algorithm> ...

  8. 为什么要用on()而不直接使用click

    为什么要用on()而不直接使用clickhttp://stackoverflow.com/questions/10082031/why-use-jquery-on-instead-of-clickht ...

  9. DB2 权限控制

    http://blog.csdn.net/liujinwei2005/article/details/8606983 http://www.ibm.com/developerworks/cn/data ...

  10. Protel99se教程六:创建原理图元件库

    在我们平时使用protel99se进行电路以及PCB设计的时候,系统自带的元件库和PCB封装库,只有一小部分,大部份元件的元件库以及封装库,我们都需要自己制作,使用protel99se,我们可以很容易 ...