ZOJ2588 Burning Bridges(割边模板)
题目要输出一个无向图的所有割边。用Tarjan算法:
一遍DFS,构造出一颗深度优先生成树,在原无向图中边分成了两种:树边(生成树上的边)和反祖边(非生成树上的边)。
顺便求出每个结点的DFS序dfn[u] 和 每个结点能沿着它和它的儿子的返祖边达到的结点最小的DFS序low[u]。
一条边(u,v)是割边当且仅当——
- low[v]>dfn[u]
注意具体实现时,无向图的边在邻接表中有正反两条边,那么如果一条边是树边了,另一条边不应该是反祖边,要忽略。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXM 222222
#define MAXN 11111
struct Edge{
int v,next;
bool tag;
}edge[MAXM];
int NE,head[MAXN];
void addEdge(int u,int v){
edge[NE].v=v; edge[NE].next=head[u]; edge[NE].tag=;
head[u]=NE++;
}
int dn,dfn[MAXN],low[MAXN],res[MAXM],resn;
void dfs(int u){
dfn[u]=low[u]=++dn;
for(int i=head[u]; i!=-; i=edge[i].next){
if(edge[i].tag) continue;
int v=edge[i].v;
if(dfn[v]){
low[u]=min(low[u],dfn[v]);
continue;
}
edge[i].tag=edge[i^].tag=;
dfs(v);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u]) res[resn++]=(i>>)+;
}
}
int main(){
int t,n,m,a,b;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
NE=;
memset(head,-,sizeof(head));
while(m--){
scanf("%d%d",&a,&b);
addEdge(a,b); addEdge(b,a);
}
resn=dn=;
memset(dfn,,sizeof(dfn));
dfs();
printf("%d\n",resn);
sort(res,res+resn);
for(int i=; i<resn; ++i){
if(i) putchar(' ');
printf("%d",res[i]);
}
if(resn) putchar('\n');
if(t) putchar('\n');
}
return ;
}
ZOJ2588 Burning Bridges(割边模板)的更多相关文章
- ZOJ2588 Burning Bridges 无向图的割边
题目大意:求无向图的割边编号. 割边定义:在一个连通图中,如果删去一个边e,图便变成不连通的两个部分,则e为该图的割边. 求法:边(u,v) 不是割边,当且仅当边(u,v)在一个环内.因此所有不在环内 ...
- zoj2588 Burning Bridges(无向图的桥)
题目请戳这里 题目大意:给一张无向图,现在要去掉一些边,使图仍然连通,求不能去掉的边. 题目分析:就是求无向图的桥. tarjan算法跑一遍,和无向图割点十分类似,这里要找low[v] > df ...
- zoj2588 Burning Bridges --- 寻求尖端
#include <iostream> #include <cstring> #include <string> #include <cstdio> # ...
- ZOJ 2588 Burning Bridges 割边(处理重边)
<题目链接> 题目大意: 给定一个无向图,让你尽可能的删边,但是删边之后,仍然需要保证图的连通性,输出那些不能被删除的边. 解题分析: 就是无向图求桥的题目,主要是提高一下处理重边的姿势. ...
- zoj 2588 Burning Bridges(割边/桥)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1588 题意:Ferry王国有n个岛,m座桥,每个岛都可以互达,现在要 ...
- ACdream 1236 Burning Bridges 割边 + 去重边
题目就是求一副图的割边,然后对于那些有重复的边的,不能算做割边. 思路就是每次加入一条边的时候,判断这条边是否存在过,存在过的话,就把那条边设为inf,表示不能作为割边.于是有了这样的代码 #incl ...
- ZOJ 2588 Burning Bridges(求含重边的无向连通图的割边) - from lanshui_Yang
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little country ...
- Burning Bridges 求tarjan求割边
Burning Bridges 给出含有n个顶点和m条边的连通无向图,求出所有割边的序号. 1 #include <cstdio> 2 #include <cstring> 3 ...
- zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...
随机推荐
- [Effective JavaScript 笔记]第6章:库和API设计--个人总结
前言 又到了一章的总结,这章里的内容.是把我从一个代码的使用者,如何换位成一个代码的编写者.如何让别人用自己的代码更容易,不用去注意太多的无用细节,不用记住冗长的函数名.在使用API时怎样避免使用者会 ...
- 异常:The absolute uri: http://www.springframework.org/security/tags cannot be resolved in either web.xml or the jar files deployed with this application
The absolute uri: http://www.springframework.org/security/tags cannot be resolved in either web.xml ...
- Java字符串split函数的注意事项
Java字符串的split方法可以分割字符串,但和其他语言不太一样,split方法的参数不是单个字符,而是正则表达式,如果输入了竖线(|)这样的字符作为分割字符串,会出现意想不到的结果, 如, Str ...
- Linux生产服务器Shell脚本分享
Linux生产服务器Shell脚本分享 2012-6-6 86市场网 linux 作为一名Linux/unix系统管理员,我经常遇到人问这个问题:shell能做什么?PHP这么强大,为什么不用PHP来 ...
- No bootable device-insert boot disk and press any key
macbook air 2012 mid. 长按关机键关机,按开机键,然后长按option键,会出现可以选择启动的磁盘块,选择要启动的磁盘进入即可.
- sharepoint bcs (bussiness connectivity services)
sharepoint bcs 在2010 版本中是提供2010 与外部数据连接的. BCS全名Business Connectivity Services,可以把它看成SharePoint 2007 ...
- hdu3555
基本的数位dp #include <cstdio> #include <cstring> using namespace std; #define D(x) x ; long ...
- c++0x新特性实例(比较常用的)
//array #include <array> void Foo() { array<> a; generate(a.begin(),a.end(),rand); sort( ...
- 在SQLServer处理中的一些问题及解决方法 NEWSEQUENTIALID()
一.DBLINK性能问题select * from dbsource.dbname.dbo.table where guid in (select guid from tablechangelog w ...
- Greedy:Allowance(POJ 3040)
零用钱大作战 题目大意:农夫和牛又搞新花样了,现在农夫想给Bessie每个星期都给一点零用钱,农夫有一堆面值的钱币,并且这个钱币都能被上一个钱币整除(1,5,10,50),并且钱币有一定数量,要你求最 ...