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[u]

  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <set>
  6. #include <queue>
  7. #include <stack>
  8. #include <vector>
  9. #include <algorithm>
  10. #define LL long long
  11. using namespace std;
  12. const int INF = 0x3f3f3f3f;
  13. const int Max = 101000;
  14. //前向星存边
  15. typedef struct Node
  16. {
  17. int v;
  18. int num;
  19. int sum;
  20. int next;
  21. }Line;
  22. Line Li[Max*2];
  23. int top;
  24. int Head[Max];
  25. // 标记数组 0 表示没有遍历 1表示已遍历 2表示遍历完其相连的节点
  26. int vis[Max];
  27. // 表示所能连接的最先遍历的顺序
  28. int low[Max];
  29. // 标记边是不是割边
  30. bool flag[Max];
  31. // 记录遍历的顺序
  32. int dfn[Max];
  33. int Num;
  34. // 割边的数目
  35. int Total;
  36. //初始化
  37. void init()
  38. {
  39. memset(Head,-1,sizeof(Head));
  40. top = 0; Num = 0; Total = 0;
  41. memset(flag,false,sizeof(flag));
  42. memset(vis,0,sizeof(vis));
  43. }
  44. void AddEdge(int u,int v,int num)
  45. {
  46. for(int i=Head[u];i!=-1;i=Li[i].next)
  47. {
  48. if(Li[i].v==v)//判断是不是重边
  49. {
  50. Li[i].sum++;
  51. return ;
  52. }
  53. }
  54. Li[top].v=v; Li[top].num = num;
  55. Li[top].sum = 1;
  56. Li[top].next = Head[u];
  57. Head[u]=top++;
  58. }
  59. void dfs(int u,int father)
  60. {
  61. dfn[u]=low[u]=Num++;
  62. vis[u]=1;
  63. for(int i=Head[u];i!=-1;i=Li[i].next)
  64. {
  65. if(Li[i].v!=father&&vis[Li[i].v]==1)//不能是父节点
  66. {
  67. low[u]=min(low[Li[i].v],low[u]);
  68. }
  69. if(vis[Li[i].v]==0)
  70. {
  71. dfs(Li[i].v,u);
  72. low[u]=min(low[u],low[Li[i].v]);
  73. if(low[Li[i].v]>dfn[u]&&Li[i].sum==1)//重边肯定不是割点
  74. {
  75. flag[Li[i].num]=true;
  76. Total ++;
  77. }
  78. }
  79. }
  80. vis[u]=2;
  81. }
  82. int n,m;
  83. int main()
  84. {
  85. int T;
  86. int z=1;
  87. scanf("%d",&T);
  88. while(T--)
  89. {
  90. scanf("%d %d",&n,&m);
  91. init();
  92. int u,v;
  93. for(int i=1;i<=m;i++)
  94. {
  95. scanf("%d %d",&u,&v);
  96. AddEdge(u,v,i);
  97. AddEdge(v,u,i);
  98. }
  99. dfs(1,0);
  100. int ans = 0;
  101. printf("%d\n",Total);
  102. for(int i=1;i<=m;i++)
  103. {
  104. if(flag[i])
  105. {
  106. if(ans)
  107. {
  108. printf(" ");
  109. }
  110. else
  111. {
  112. ans = 1;
  113. }
  114. printf("%d",i);
  115. }
  116. }
  117. if(Total)
  118. {
  119. printf("\n");
  120. }
  121. if(T)
  122. {
  123. printf("\n");
  124. }
  125. }
  126. return 0;
  127. }

Burning Bridges-ZOJ1588(割边求解)的更多相关文章

  1. ZOJ2588 Burning Bridges(割边模板)

    题目要输出一个无向图的所有割边.用Tarjan算法: 一遍DFS,构造出一颗深度优先生成树,在原无向图中边分成了两种:树边(生成树上的边)和反祖边(非生成树上的边). 顺便求出每个结点的DFS序dfn ...

  2. ZOJ 2588 Burning Bridges(求含重边的无向连通图的割边) - from lanshui_Yang

    Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little country ...

  3. Burning Bridges 求tarjan求割边

    Burning Bridges 给出含有n个顶点和m条边的连通无向图,求出所有割边的序号. 1 #include <cstdio> 2 #include <cstring> 3 ...

  4. zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】

    Burning Bridges Time Limit: 5 Seconds      Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...

  5. xtu summer individual 5 E - Burning Bridges

    Burning Bridges Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...

  6. zoj——2588 Burning Bridges

    Burning Bridges Time Limit: 5 Seconds      Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...

  7. ZOJ 2588 Burning Bridges(求桥的数量,邻接表)

    题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2588 Burning Bridges Time Limit: 5 ...

  8. ZOJ 2588 Burning Bridges (tarjan求割边)

    题目链接 题意 : N个点M条边,允许有重边,让你求出割边的数目以及每条割边的编号(编号是输入顺序从1到M). 思路 :tarjan求割边,对于除重边以为中生成树的边(u,v),若满足dfn[u] & ...

  9. ZOJ Problem - 2588 Burning Bridges tarjan算法求割边

    题意:求无向图的割边. 思路:tarjan算法求割边,访问到一个点,如果这个点的low值比它的dfn值大,它就是割边,直接ans++(之所以可以直接ans++,是因为他与割点不同,每条边只访问了一遍) ...

随机推荐

  1. JS移动客户端--触屏滑动事件 banner图效果

    JS移动客户端--触屏滑动事件 移动端触屏滑动的效果其实就是图片轮播,在PC的页面上很好实现,绑定click和mouseover等事件来完成.但是在移动设备上,要实现这种轮播的效果,就需要用到核心的t ...

  2. document.body.scrollTop or document.documentElement.scrollTop

      用Javascript获取DOM节点相对于页面的绝对坐标时,需要计算当前页面的滚动距离,而这个值的获取又取决于浏览器. 在Firefox或Chrome浏览器的控制台可以查看document.bod ...

  3. IIS 8 下使用 WCF

    按照以下步骤添加后,IIS8即支持WCF服务. 首先添加MIME类型 扩展名“.svc”,MIME类型 “application/octet-stream” 2. 然后在“Handler Mappin ...

  4. Android课程---布局管理器中的线性布局

    线性布局实例: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:andro ...

  5. 【iCore3 双核心板】例程二十一:LAN_TCPS实验——以太网数据传输

    实验指导书及代码包下载: http://pan.baidu.com/s/1ntTjWpV iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  6. WordPress学习

    1,WordPress安装 2,WordPress前台与后台 3,WordPress Post&Page. 4,WordPress多媒体 5,WordPress插件管理 上面5条已经掌握,明天 ...

  7. webstorm svn 报错

    webstorm    svn 报错Cannot run program "svn": CreateProcess error=2, The system cannot find ...

  8. What’s the difference between data mining and data warehousing?

    Data mining is the process of finding patterns in a given data set. These patterns can often provide ...

  9. Swift 懒加载(lazy) 和 Objective-C 懒加载的区别

    在程序设计中,我们经常会使用 懒加载 ,顾名思义,就是用到的时候再开辟空间,比如iOS开发中的最常用控件UITableView,实现数据源方法的时候,通常我们都会这样写 Objective-C - ( ...

  10. JAVASE02-Unit01: API文档 、 字符串基本操作

    API文档 . 字符串基本操作 文档注释 package day01; /** * 文档注释只能定义在三个地方: * 类,方法,常量 * * 文档注释是功能注释,用来说明功能作用 * 在类上使用的目的 ...