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求 ...
随机推荐
- uCOS-II任务的挂起和恢复
函数描述 OSTaskSuspend() 功能描述:无条件挂起一个任务.调用此函数的任务也可以传递参数OS_PRIO_SELF,挂起调用任务本身.函数原型:INT8U OSTaskSuspend ( ...
- unity3d 使用背景贴图
使用贴图代替天空盒作为背景,参照:http://www.narkii.com/club/thread-261840-1.html 看看我做的:
- 【BZOJ1823】 [JSOI2010]满汉全席
Description 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而 ...
- 我的PHP之旅--数据库连接MySQL服务器,添加 删除 查询
PHP连接MySQL服务器 连接MySQL的方法:mysql_connect(); 语法:resource $link = mysql_connect($hostname, $username, $p ...
- nginx -t "nginx: [warn] only the last index in "index" directive should be absolute in 6 "的问题解决
修改完nginx的配置文件之后,执行nginx -t命令提示"nginx: [warn] only the last index in "index" directive ...
- [TSP+floyd]POJ3311 Hie with the Pie
题意: 给i到j花费的地图 1到n编号 一个人要从1遍历n个城市后回到1 求最小的花费(可以重复走) 分析 http://www.cnblogs.com/Empress/p/4039240.htm ...
- [状压dp]经典TSP
0出发 每个顶点经过一次 回到0 最小花费. O($n^2 \times 2^n$) 记忆化搜索: // s: 已经访问过的节点状态 v: 出发位置 int dfs(int s, int v) { ) ...
- [topcoder]SmartWordToy
广度搜索BFS,要用Queue.还不是很熟,这道题帮助理清一些思绪了.其实这道题是求最短路径,所以BFS遇到第一个就可以返回了,所以后面有些现有大小和历史大小的判断可以省却. 过程中拿数组存step还 ...
- J2EE的十三种技术(规范)
J2EE的十三种技术(规范) Java数据库连接(JDBC) JDBC API以一个统一的方式访问各种数据库.与ODBC类似,JDBC将开发者和私有数据库之间的问题隔离开来.由于它建立在Java上, ...
- php stdClass Object 问题
Array ( [0] => stdClass Object ( [term_id] => 3 [name] => apache [slug] => apache [term_ ...