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求 ...
随机推荐
- 【toplink】 位居第一的Java对象关系可持续性体系结构
TopLink,是位居第一的Java对象关系可持续性体系结构,原署WebGain公司的产品,后被Oracle收购,并重新包装为Oracle AS TopLink.TOPLink为在关系数据库表中存储 ...
- windows store app promise
Promise.any ---- 参数是一个promise的数组.any的作用就是 promise 数组中任意一个 promise 执行完毕,就会执行 done内的函数 (function () { ...
- npm常用命令解析
npm是什么 NPM的全称是Node Package Manager,是随同NodeJS一起安装的包管理和分发工具,它很方便让JavaScript开发者下载.安装.上传以及管理已经安装的包. npm ...
- [HDOJ - 5282] Senior's String 【DP】
题目链接:BZOJ - 5282 题目分析 LCS 就是用经典的 O(n^2) DP 解决,f[i][j] 表示 x 串前 i 个字符与 y 串前 j 个字符的 LCS 长度. f[i][j] = m ...
- 引擎渲染速度测试--我js代码写得少你不要骗我
上一张图,很多人都看过的 地址:http://aui.github.io/artTemplate/test/test-speed.html 这个地址是在看artTemplate的时候看到的,很早都看过 ...
- asp.net推送
http://tech.it168.com/a2012/0210/1310/000001310252_all.shtml http://www.infoq.com/cn/news/2012/09/rc ...
- android 自定义按钮的外边框
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...
- ANDROID_MARS学习笔记_S02_006_APPWIDGET2_PendingIntent及RemoteViews实现widget绑定点击事件
一.代码流程 1.ExampleAppWidgetProvider的onUpdate(Context context, AppWidgetManager appWidgetManager, int[] ...
- Spring AOP实现方式三【附源码】
注解AOP实现 源码结构: 1.首先我们新建一个接口,love 谈恋爱接口. package com.spring.aop; /** * 谈恋爱接口 * * @author Administrator ...
- VC C运行时库(CRTL)的几个版本及选用
分类: Windows 2008-12-23 10:01 987人阅读 评论(0) 收藏 举报ciostreammfclibrary多线程import最近做项目碰到了一个关于在动态库中使用MFC以及在 ...