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求 ...
随机推荐
- Boost IPC Persistence Of Interprocess Mechanisms 例子
下面这一段摘抄自 Boost 1_55_0 的文档,显然标注了 每一个的生命期. One of the biggest issues with interprocess communication m ...
- check whether the crystal report runtime is exists 检查crystalreport运行时是否存在
1. Try Dim rptDoc As New CrystalDecisions.CrystalReports.Engine.ReportClass() Dim rptView As New Cry ...
- 视图--bai
/*视图的必要性 create view population_all_view as select xxxx 详细信息 from qgck where rownum<500 -- sql语句不 ...
- 关于xcode6打包以及上线前企业部署测试的说明 --转自张诚教授微博
xcode6如何打包 首先clean然后点击归档 点击打包之后保存 点选第一个以后检查相关证书签名 那么我们开发完以后,在上线前如何给别人测试 有2种方法 1.使用299美金的企业开发者账号搭建企业部 ...
- 上传项目到Github
1.使用根工具(均是图形化的界面) TortoiseGit-1.8.12.0-32bit GitExtensions-2.48.05-SetupComplete 2.大致步骤 首先,你需要一个Gith ...
- HTML中的转义字符
HTML常用符号: 显示一个空格 < 小于 < <> 大于 > >& &符号 & &" 双引号 " &qu ...
- ASP.NET 弹出对话框和页面之间传递值的经验总结
今天碰到一个弹出对话框(PopUp dialog)的问题, 因该是个傻瓜问题, 但是还是让我研究了半天, 总结了一些前人经验, 拿出来跟大家分享一下! 在ASP.Net中页面之间的传值方法有很多,但是 ...
- java1.8的几大新特性(二)
七.Date APIJava 8 在包java.time下包含了一组全新的时间日期API.新的日期API和开源的Joda-Time库差不多,但又不完全一样,下面的例子展示了这组新API里最重要的一些部 ...
- [Android] 修改设备访问权限
在硬件抽象层模块中,我们是调用open函数来打开对应的设备文件的.例如,在2.3.2小节中开发的硬件抽象层模块freg中,函数freg_device_open调用open函数来打开设备文件/dev/f ...
- 【HDOJ】3275 Light
这就是个简单线段树+延迟标记.因为对bool使用了~而不是!,wa了一下午找不到原因. /* 3275 */ #include <iostream> #include <sstrea ...