ZOJ 2588 Burning Bridges(求含重边的无向连通图的割边) - from lanshui_Yang
Burning Bridges
Time Limit: 5 Seconds Memory Limit: 32768 KB
Ferry Kingdom is a nice little country located on N islands that are connected by M bridges. All bridges are very beautiful and are loved by everyone in the kingdom. Of course, the system of bridges is designed in such a way that one can get from any island to any other one.
But recently the great sorrow has come to the kingdom. Ferry Kingdom was conquered by the armies of the great warrior Jordan and he has decided to burn all the bridges that connected the islands. This was a very cruel decision, but the wizards of Jordan have advised him no to do so, because after that his own armies would not be able to get from one island to another. So Jordan decided to burn as many bridges as possible so that is was still possible for his armies to get from any island to any other one.
Now the poor people of Ferry Kingdom wonder what bridges will be burned. Of course, they cannot learn that, because the list of bridges to be burned is kept in great secret. However, one old man said that you can help them to find the set of bridges that certainly will not be burned.
So they came to you and asked for help. Can you do that?
Input
The input contains multiple test cases. The first line of the input is a single integer T (1 <= T <= 20) which is the number of test cases. T test cases follow, each preceded by a single blank line.
The first line of each case contains N and M - the number of islands and bridges in Ferry Kingdom respectively (2 <= N <= 10 000, 1 <= M <= 100 000). Next M lines contain two different integer numbers each and describe bridges. Note that there can be several bridges between a pair of islands.
Output
On the first line of each case print K - the number of bridges that will certainly not be burned. On the second line print K integers - the numbers of these bridges. Bridges are numbered starting from one, as they are given in the input.
Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.
Sample Input
2 6 7
1 2
2 3
2 4
5 4
1 3
4 5
3 6 10 16
2 6
3 7
6 5
5 9
5 4
1 2
9 8
6 4
2 10
3 8
7 9
1 4
2 4
10 5
1 6
6 10
Sample Output
2
3 7 1
4
题目大意:直接把题目抽象一下吧,给你一个无向连通图,每两个顶点之间可能有多条边(即含重边),让你求出图中割边(即桥)的个数,并输出割边的序号。
解题思路:这是一道典型的求连通图割边的问题,需要注意的是,此图可能有重边。
割边的求法:割边的求解过程与求关节点的过程类似,判断方法是:无向图中的一条边(u,v)是桥,当且仅当(u,v)为深度优先搜索生成树中的边。且满足dfn[n] < low[v] 。
Ps:ZOJ判题很严格,笔者PE了无数次,才发现一个小小的坑,具体请看程序。
代码如下:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<vector>
#include<set>
#include<algorithm>
using namespace std ;
const int MAXN = 10005 ;
struct Node
{
int adj ;
int e ;
Node *next ;
} ;
Node *vert[MAXN] ; // 顶点指针数组
bool vis[MAXN] ; // 标记数组,判断顶点是否被访问
bool vise[111111] ; // 标记数组,判断边是否被访问
set <int> bridges ; // 记录割边的集合
int low[MAXN] ;
int dfn[MAXN] ;
int tmpdfn ;
int tmpe ;
int n , m ;
int root ; // 根节点
bool first = true ;
void init()
{
scanf("%d%d" , &n , &m) ;
memset(vert , 0 , sizeof(vert)) ;
bridges.clear() ;
int i ;
tmpe = 0 ;
for(i = 1 ; i <= m ; i ++)
{
int a , b ;
scanf("%d%d" , &a , &b) ;
Node * p ;
p = new Node ;
p -> adj = b ;
p -> e = ++ tmpe ;
p -> next = vert[a] ;
vert[a] = p ; p = new Node ;
p -> adj = a ;
p -> e = tmpe ;
p -> next = vert[b] ;
vert[b] = p ;
root = a ;
}
}
void dfs(int u)
{
Node *p = vert[u] ;
while (p != NULL)
{
int v = p -> adj ;
int te = p -> e ;
if(!vise[te]) // 注意:因为此题中可能有重边,所以应先判重,即访问过的边就不再访问
{
vise[te] = 1 ;
if(!vis[v])
{
vis[v] = 1 ;
dfn[v] = low[v] = ++ tmpdfn ;
dfs(v) ;
low[u] = min(low[u] , low[v]) ;
if(low[v] > dfn[u]) // 注意此处是严格的 “ > ”
{
bridges.insert(te) ;
}
}
else
{
low[u] = min(low[u] , dfn[v]) ;
}
}
p = p -> next ;
}
}
void solve()
{
if(first) first = false ;
else puts("") ;
memset(dfn , 0 , sizeof(dfn)) ;
memset(vis , 0 , sizeof(vis)) ;
memset(low , 0 , sizeof(low)) ;
memset(vise , 0 ,sizeof(vise)) ;
tmpdfn = 1 ;
dfn[root] = low[root] = tmpdfn ;
vis[root] = 1 ;
dfs(root) ;
cout << bridges.size() << endl ;
set<int> :: iterator it ;
int cnt = 0 ;
for(it = bridges.begin() ; it != bridges.end() ; ++ it)
{
printf("%d" , *it) ;
if( cnt < bridges.size() - 1)
printf(" ") ;
cnt ++ ;
}
if(bridges.size() > 0) // 注意:此处极易PE,想想bridges.size() == 0 的情况
puts("") ;
}
void dele()
{
int i ;
for(i = 1 ; i <= n ; i ++)
{
Node *p = vert[i] ;
while (p != NULL)
{
vert[i] = p -> next ;
delete p ;
p = vert[i] ;
}
}
}
int main()
{
int T ;
scanf("%d" , &T) ;
first = true ;
while (T --)
{
init() ;
solve() ;
dele() ;
}
return 0 ;
}
ZOJ 2588 Burning Bridges(求含重边的无向连通图的割边) - from lanshui_Yang的更多相关文章
- ZOJ 2588 Burning Bridges(求桥的数量,邻接表)
题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2588 Burning Bridges Time Limit: 5 ...
- ZOJ 2588 Burning Bridges(无向连通图求割边)
题目地址:ZOJ 2588 由于数组开小了而TLE了..这题就是一个求无向连通图最小割边.仅仅要推断dfn[u]是否<low[v],由于low指的当前所能回到的祖先的最小标号,增加low[v]大 ...
- 【求无向图的桥,有重边】ZOJ - 2588 Burning Bridges
模板题——求割点与桥 题意,要使一个无向图不连通,输出必定要删掉的边的数量及其编号.求桥的裸题,可拿来练手. 套模板的时候注意本题两节点之间可能有多条边,而模板是不判重边的,所以直接套模板的话,会将重 ...
- zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...
- zoj——2588 Burning Bridges
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...
- zoj 2588 Burning Bridges(割边/桥)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1588 题意:Ferry王国有n个岛,m座桥,每个岛都可以互达,现在要 ...
- ZOJ 2588 Burning Bridges (tarjan求割边)
题目链接 题意 : N个点M条边,允许有重边,让你求出割边的数目以及每条割边的编号(编号是输入顺序从1到M). 思路 :tarjan求割边,对于除重边以为中生成树的边(u,v),若满足dfn[u] & ...
- ZOJ 2588 Burning Bridges 割边(处理重边)
<题目链接> 题目大意: 给定一个无向图,让你尽可能的删边,但是删边之后,仍然需要保证图的连通性,输出那些不能被删除的边. 解题分析: 就是无向图求桥的题目,主要是提高一下处理重边的姿势. ...
- zoj 2588 Burning Bridges
题目描述:Ferry王国是一个漂亮的岛国,一共有N个岛国.M座桥,通过这些桥可以从每个小岛都能到达任何一个小岛.很不幸的是,最近Ferry王国被Jordan征服了.Jordan决定烧毁所有的桥.这是个 ...
随机推荐
- sqlite使用小结
官方网站 http://www.sqlite.org/index.html 图形前端 http://www.sqlabs.net/sqlitemanager.php 个人觉得firefox的sqlit ...
- 《学习OpenCV》练习题第五章第一题ab
这道题是载入一幅带有有趣纹理的图像并用不同的模板(窗口,核)大小做高斯模糊(高斯平滑),然后比较用5*5大小的窗口平滑图像两次和用11*11大小的窗口平滑图像一次是否接近相同. 先说下我的做法,a部分 ...
- 第三百四十九、五十天 how can I 坚持
昨天是忘写博客了,今天下班才突然意思到,搞框架搞了好晚.今天重新下了个好了. 昨天,把存储过程交给同事写了,啥都不会,又一堆问题,折腾了一天. 今天相对轻松些,不过事情还没完..明天又周五了. 还有昨 ...
- 【多线程同步案例】Race Condition引起的性能问题
Race Condition(也叫做资源竞争),是多线程编程中比较头疼的问题.特别是Java多线程模型当中,经常会因为多个线程同时访问相同的共享数据,而造成数据的不一致性.为了解决这个问题,通常来说需 ...
- oracle 去掉空格
trim(value) 去掉左右空格 ltrim(value) 去掉左空格 rtrim(value) 去掉右空格
- jQuery - Chaining
jQuery - Chaining @(编程) 通过 jQuery,您可以把动作/方法链接起来. Chaining 允许我们在一条语句中允许多个 jQuery 方法(在相同的元素上). jQuery ...
- windows 7 获取SYSTEM权限
当Adobe Reader 9.0卸载之后,你会发现原来的C:\Program Files\Adobe\Reader 9.0\Resource\CMap文件夹下的一些文件无法删除,提示你需要SYSTE ...
- Python基础 基本运算符
什么是操作符 形如 1+1 = 2 这样的 1 就是操作数 + 就是操作符 python语言支持的操作符: 算数运算符 比较运算符(关系) 赋值运算符 逻辑运算符 位运算符 会员操作符 标志操作符 算 ...
- POJ 3174 Alignment of the Planets (暴力求解)
题意:给定 n 个坐标,问你三个共线的有多少组. 析:这个题真是坑啊,写着 n <= 770,那么一秒时间,三个循环肯定超时啊,我一直不敢写了,换了好几种方法都WA了,也不知道为什么,在比赛时坑 ...
- 更有效率的使用Visual Studio(一)
很多比较通用的快捷键的默认设置其实是有一些缩写在里面的,这个估计也是MS帮助我们记忆.比如说注释代码的快捷键是Ctrl + E + C,我们如果知道它是 Ctrl + Edit + Comment C ...