题目地址: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 ;
}

最新文章

  1. CI框架--事务
  2. Hex string convert to Binary String and Vise-Versa(16进制和2进制字符串的相互转换)
  3. Hibernate提供的内置标识符生成器
  4. AngularJS - 服务简介
  5. Experience all that SharePoint 15 has to offer. Start now or Remind me later.
  6. RMAN备份与恢复实例
  7. node 上传文件 路径 重命名等问题
  8. HTTP&#160;错误
  9. 编程练习------C/C++分别实现字符串与整数的转换
  10. js中实现继承的不同方式以及其缺点
  11. 深入理解JVM垃圾收集机制,下次面试你准备好了吗
  12. 题解:[APIO/CTSC 2007]数据备份
  13. java BIO/NIO/AIO 学习
  14. 解决url传中文参数问题
  15. 如何安装ipa文件(二)
  16. WEB下载显示下载名称乱码--java
  17. HDU 5832 A water problem 水题
  18. C# 托管资源 与 非托管资源
  19. Android数据库升级不丢失数据解决方案
  20. git 填坑之 git 暂存区忽略文件

热门文章

  1. root of factory hierarchy
  2. thymeleaf 拼接 超链接
  3. ASP.NET-使用事件监视诊断程序异常
  4. 洛谷 P2049 魔术棋子
  5. Android自己定义处理崩溃异常
  6. reactor模式与java nio
  7. Java 8 Stream API具体解释
  8. iOS AES的加密解密
  9. duplicate报ORA-01017权限问题
  10. BEGINNING SHAREPOINT&amp;#174; 2013 DEVELOPMENT 第10章节--SP2013中OAuth概览 创建和管理应用程序身份