poj1515--Street Directions(边的双连通)
给一个无向图,要求变成强连通的有向图,需要保留哪些边。
边的双连通,对于桥保留两条边,其他的只保留一条边。求双连通的过程中记录保留边。
/*********************************************
Problem: 1515 User: G_lory
Memory: 232K Time: 32MS
Language: C++ Result: Accepted
**********************************************/
#include <cstdio>
#include <cstring>
#include <iostream>
#define pk printf("KKK!\n"); using namespace std; const int N = 1005;
const int M = N * N; struct Edge {
int from, to, next;
int cut;
} edge[M];
int cnt_edge;
int head[N];
void add_edge(int u, int v)
{
edge[cnt_edge].from = u;
edge[cnt_edge].to = v;
edge[cnt_edge].next = head[u];
edge[cnt_edge].cut = 0;
head[u] = cnt_edge++;
} int dfn[N]; int idx;
int low[N]; int n, m; void tarjan(int u, int pre)
{
dfn[u] = low[u] = ++idx;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].cut) continue;
edge[i].cut = 1;
edge[i ^ 1].cut = -1;
if (v == pre) continue; if (!dfn[v])
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (dfn[u] < low[v])
edge[i].cut = edge[i ^ 1].cut = 1;
}
else low[u] = min(low[u], dfn[v]);
}
} void init()
{
idx = cnt_edge = 0;
memset(dfn, 0, sizeof dfn);
memset(head, -1, sizeof head);
} void solve()
{
for (int i = 0; i < cnt_edge; ++i)
if (edge[i].cut == 1)
printf("%d %d\n", edge[i].from, edge[i].to);
} int main()
{
//freopen("in.txt", "r", stdin);
int cas = 0;
while (~scanf("%d%d", &n, &m))
{
if (n == 0 && m == 0) break;
printf("%d\n\n", ++cas);
int u, v;
init();
for (int i = 0; i < m; ++i)
{
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
tarjan(1, -1);
solve();
printf("#\n");
}
return 0;
}
poj1515--Street Directions(边的双连通)的更多相关文章
- POJ 1515 Street Directions --一道连通题的双连通和强连通两种解法
题意:将一个无向图中的双向边改成单向边使图强连通,问最多能改多少条边,输出改造后的图. 分析: 1.双连通做法: 双连通图转强连通图的算法:对双连通图进行dfs,在搜索的过程中就能按照搜索的方向给所有 ...
- (中等) CF 555E Case of Computer Network,双连通+树。
Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible a ...
- UVA 610 - Street Directions(割边)
UVA 610 - Street Directions option=com_onlinejudge&Itemid=8&page=show_problem&category=5 ...
- UVALive 5412 Street Directions
Street Directions Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. ...
- poj 3694 Network 边双连通+LCA
题目链接:http://poj.org/problem?id=3694 题意:n个点,m条边,给你一个连通图,然后有Q次操作,每次加入一条边(A,B),加入边后,问当前还有多少桥,输出桥的个数. 解题 ...
- poj3177--Redundant Paths(边的双连通)
有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路.两条独立的路是指:没有公共边的路,但可以 ...
- 图的强连通&双连通
http://www.cnblogs.com/wenruo/p/4989425.html 强连通 强连通是指一个有向图中任意两点v1.v2间存在v1到v2的路径及v2到v1的路径. dfs遍历一个图, ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- HDU4612(Warm up)2013多校2-图的边双连通问题(Tarjan算法+树形DP)
/** 题目大意: 给你一个无向连通图,问加上一条边后得到的图的最少的割边数; 算法思想: 图的边双连通Tarjan算法+树形DP; 即通过Tarjan算法对边双连通缩图,构成一棵树,然后用树形DP求 ...
随机推荐
- 【转】iOS 9自带苹果式省电模式 依然软硬兼施
非本人总结,转自:http://news.91.com/apple/1506/21837672.html 说好的改善和优化,iOS 9真的带来了.且不说那些经过改善的功能,iOS 9 推出的低功耗模式 ...
- Lucene基础(一)--入门
Lucene介绍 lucene的介绍,这里引用百度百科的介绍Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引 ...
- Vm image download resource
http://vmdepot.msopentech.com/List/Index http://www.hanselman.com/blog/Over400VirtualMachineImagesOf ...
- C#中如何按字节数截取字符串?
http://www.cnblogs.com/xuejie/archive/2012/12/14/2818452.html
- responsive web design
http://d.alistapart.com/responsive-web-design/ex/ex-site-flexible.html http://alistapart.com/article ...
- Unity3D游戏UI开发经验谈
原地址:http://news.9ria.com/2013/0629/27679.html 在Unity专场上,108km创始人梁伟国发表了<Unity3D游戏UI开发经验谈>主题演讲.他 ...
- 同步两台linux服务器时间同步方案
Linux自带了ntp服务 -- /etc/init.d/ntpd,这个服务不仅可以设置让本机和某台/某些机器做时间同步,他本身还可以扮演一个time server的角色,让其他机器和他同步时间. 配 ...
- cocos2d-x for js 继承的写法
cocos2d-x for js中集成了两套继承写法,一套是JR的(jquery的作者),一套是google. 目前来说,cocos2d-x for js demo使用JR的写法----未完待续... ...
- bzoj1965
很明显,求方程x*2^m=L (mod n+1)的解(1<=x<=n) var n,m,l,x,y,p:int64; function quick(x:int64):int64; var ...
- 操作系统杂谈 mac 和linux windows若干概念
Mac: vmware 安装:1.方式一通过FreeBSD方式用 darwin.iso引导加载dmg安装 2.通过制作cdr /iso vmware安装mac插件 mac有 macpe 使用open ...