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. 【转】CoreData以及MagicalRecord (二)

    3. 运行时类与对象 NSManagedObject Managed Object 表示数据文件中的一条记录,每一个Managed Object在内存中对应的实体(Entity)的一个数据表示.Man ...

  2. Exponentiation

    Description Problems involving the computation of exact values of very large magnitude and precision ...

  3. 实时消息传输协议 RTMP(Real Time Messaging Protocol)

    实时消息传输协议(RTMP)最初是由 Macromedia 为互联网上 Flash player 和服务器之间传输音频.视频以及数据流而开发的一个私有协议.Adobe 收购 Macromedia 购以 ...

  4. android手机关于google play商店闪退的解决办法

    部分android手机没有安装google play商店,这个可以通过类似“机锋”.“360手机助手”等应用市场下载. 安装google play商店后,点击打开却一闪而过:这个问题是因为手机没有安装 ...

  5. appium 学习各种小功能总结--功能有《滑动图片、保存截图、验证元素是否存在、》---新手总结(大牛勿喷,新手互相交流)

    1.首页滑动图片点击 /** * This Method for swipe Left * 大距离滑动 width/6 除数越大向左滑动距离也越大. * width:720 *height:1280 ...

  6. Select specified items from Tuple List

    #Select specified items from Tuple List ##Select one item to form list `tupleList.Select(element =&g ...

  7. WORD 无格式粘贴 2003 2007 MacOS2011

    2003 打开Word窗口,依次点击“工具----宏----Visual Basic编辑器”,打开“Microsoft visual Basic”窗口,在左侧“工程”栏选中“Normal”工程,点击“ ...

  8. ruby2.0(rails)以后版本的debug

    很喜欢RUBY(RAILS),认识也好久好久了,但是说实话,从来没用ROR写过什么东西,都是小打小闹,做些自娱自乐的东西,碰到什么问题,基本仔细看看,加上几个LOG就找到原因了,从来没想过要DEBUG ...

  9. TMT行业分析师

    诚聘英才 - 传媒梦工场 TMT行业分析师 工作经验:  2年以上 发布日期:  2013-01-04 最低学历:  本科 管理经验:  否 工作性质:  全职 招聘人数:  1人 职位类别:  金融 ...

  10. jQuery EasyUI 数字框(NumberBox)用法

    这里的options是选项,可以参考下表: 选项名 类型 描述 默认值 min 数字 文本框中可允许的最小值 null max 数字 文本框中可允许的最大值 null precision 数字 最高可 ...