ZOJ 2588 Burning Bridges(求桥的数量,邻接表)
题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2588
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 解题代码:
// File Name :zoj2588.cpp
// Author :Freetion
// Created Time :2013年09月12日 星期四 19时37分34秒 #define LOCAL //Please annotate this line when you submit
/********************************************************/
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <map>
#define CLE(name, n) memset(name, n, sizeof(name))
using namespace std; const int max_n = ;
const int max_m = ;
struct node
{
int ok;
int to;
int id;
int next;
}Edge[*max_m];
int ver[max_n], num;
int dfn[max_n], brg[max_n], brg_num;
int tmdfn, son; bool OK(int u, int v)
{
for (int q = ver[u]; ~q; q = Edge[q].next)
if (Edge[q].to == v)
{
Edge[q].ok = ;
return true;
}
return false;
} void add(int u, int v, int id)
{
if (OK(u, v))
return;
Edge[num].to = v;
Edge[num].ok = ;
Edge[num].id = id;
Edge[num].next = ver[u];
ver[u] = num ++;
} void init()
{
CLE(ver, -);
CLE(dfn, );
tmdfn = son = brg_num = ;
num = ;
} int dfs(int u, int fa)
{
int low_u;
low_u = dfn[u] = ++ tmdfn;
for (int i = ver[u]; ~i; i = Edge[i].next)
{
int v = Edge[i].to;
if ( !dfn[v])
{
int low_v = dfs(v, u);
low_u = min(low_u, low_v);
if (low_v > dfn[u] && !Edge[i].ok)
brg[brg_num ++] = Edge[i].id;
}
else if(v != fa)
low_u = min(low_u, dfn[v]);
}
return low_u;
} int main()
{
int T, n, m;
int u, v;
scanf ("%d", &T);
while (T --)
{
init();
scanf ("%d%d", &n, &m);
for (int i = ; i <= m; i ++)
{
scanf ("%d%d", &u, &v);
add(u, v, i);
add(v, u, i);
}
dfs(, );
printf ("%d\n", brg_num);
sort(brg, brg + brg_num);
for (int i = ; i < brg_num; i ++)
{
printf ("%d%c", brg[i], i == brg_num - ? '\n' : ' ');
}
if (T) puts("");
}
return ;
}
ZOJ 2588 Burning Bridges(求桥的数量,邻接表)的更多相关文章
- zoj 2588 Burning Bridges(割边/桥)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1588 题意:Ferry王国有n个岛,m座桥,每个岛都可以互达,现在要 ...
- 【求无向图的桥,有重边】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(求含重边的无向连通图的割边) - from lanshui_Yang
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little country ...
- ZOJ 2588 Burning Bridges(无向连通图求割边)
题目地址:ZOJ 2588 由于数组开小了而TLE了..这题就是一个求无向连通图最小割边.仅仅要推断dfn[u]是否<low[v],由于low指的当前所能回到的祖先的最小标号,增加low[v]大 ...
- 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
题目描述:Ferry王国是一个漂亮的岛国,一共有N个岛国.M座桥,通过这些桥可以从每个小岛都能到达任何一个小岛.很不幸的是,最近Ferry王国被Jordan征服了.Jordan决定烧毁所有的桥.这是个 ...
- ZOJ 2588 Burning Bridges 割边(处理重边)
<题目链接> 题目大意: 给定一个无向图,让你尽可能的删边,但是删边之后,仍然需要保证图的连通性,输出那些不能被删除的边. 解题分析: 就是无向图求桥的题目,主要是提高一下处理重边的姿势. ...
- ZOJ 2588 Burning Bridges (tarjan求割边)
题目链接 题意 : N个点M条边,允许有重边,让你求出割边的数目以及每条割边的编号(编号是输入顺序从1到M). 思路 :tarjan求割边,对于除重边以为中生成树的边(u,v),若满足dfn[u] & ...
随机推荐
- luogu 4844 LJJ爱数数 (莫比乌斯反演+数学推导)
题目大意:求满足gcd(a,b,c)==1,1/a+1/b=1/c,a,b,c<=n的{a,b,c}有序三元组个数 因为题目里有LJJ我才做的这道题 出题人官方题解https://www.cnb ...
- mysql 1067终极解决办法 亲测好使
进入mysql data 目录 删除 ib_logfile0 ib_logfile1 ibdata1 这三个文件 重启mysql
- 浅谈python 中正则的一些函数
主要的函数有 : match() search() findall() group() groups() split() match (): 含义 开头匹配,匹配成功返回一个对象失败则 ...
- tar 命令man说明
TAR(1) User Commands TAR(1) NAME tar - manual page for tar 1.26 SYNOPSIS tar [OPTION...] [FILE]... D ...
- Hadoop的datanode超时时间设置
datanode进程死亡或者网络故障造成datanode无法与namenode通信, namenode不会立即把该节点判定为死亡,要经过一段时间,这段时间暂称作超时时长. HDFS默认的超时时长为10 ...
- android将String转化为MD5的方法+一些String经常使用的方法
public class StringUtils { public static String MD5Encode(String origin) { String resultString = nul ...
- web集群中经常使用的session同步解决方式及对照
随着站点的功能越来越多,用户量越来越庞大,单节点模式已经严重不能支撑整个系统的正常运作,轻则用户页面訪问时间越来越慢.重则就会导致整个系统瘫痪.这时候 就须要优化或调整眼下的架构,大部分人就会採用各种 ...
- django 笔记6 Ajax
感谢alex~ .Django请求生命周期 输入url 进入 urls(路由系统) 指向 views(视图函数)->(获取模板) 里面的函数 再由函数返回字符串给用户 .路由系统 /index/ ...
- [JZOJ3382] [NOIP2013模拟] 七夕祭 解题报告
Description 七夕节因牛郎织女的传说而被扣上了「情人节」的帽子.于是TYVJ今年举办了一次线下七夕祭.Vani同学今年成功邀请到了cl同学陪他来共度七夕,于是他们决定去TYVJ七夕祭游玩. ...
- POJ 3204 网络流的必须边
思路: 求一遍网络流 在残余网络上DFS 从起点DFS 从终点把边反向DFS 一个边跟起点连通 跟终点反向的边连通 ans++ 注:此题不能用tarjan 因为有边权为0的边 //By SiriusR ...