ZOJ 2588 Burning Bridges(求桥的数量,邻接表)
题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2588
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
解题代码:
1 // File Name :zoj2588.cpp 2 // Author :Freetion 3 // Created Time :2013年09月12日 星期四 19时37分34秒 4 5 #define LOCAL //Please annotate this line when you submit 6 /********************************************************/ 7 #include <iostream> 8 #include <stdio.h> 9 #include <math.h> 10 #include <algorithm> 11 #include <string.h> 12 #include <string> 13 #include <map> 14 #define CLE(name, n) memset(name, n, sizeof(name)) 15 using namespace std; 16 17 const int max_n = 10005; 18 const int max_m = 100005; 19 struct node 20 { 21 int ok; 22 int to; 23 int id; 24 int next; 25 }Edge[2*max_m]; 26 int ver[max_n], num; 27 int dfn[max_n], brg[max_n], brg_num; 28 int tmdfn, son; 29 30 bool OK(int u, int v) 31 { 32 for (int q = ver[u]; ~q; q = Edge[q].next) 33 if (Edge[q].to == v) 34 { 35 Edge[q].ok = 1; 36 return true; 37 } 38 return false; 39 } 40 41 void add(int u, int v, int id) 42 { 43 if (OK(u, v)) 44 return; 45 Edge[num].to = v; 46 Edge[num].ok = 0; 47 Edge[num].id = id; 48 Edge[num].next = ver[u]; 49 ver[u] = num ++; 50 } 51 52 void init() 53 { 54 CLE(ver, -1); 55 CLE(dfn, 0); 56 tmdfn = son = brg_num = 0; 57 num = 1; 58 } 59 60 int dfs(int u, int fa) 61 { 62 int low_u; 63 low_u = dfn[u] = ++ tmdfn; 64 for (int i = ver[u]; ~i; i = Edge[i].next) 65 { 66 int v = Edge[i].to; 67 if ( !dfn[v]) 68 { 69 int low_v = dfs(v, u); 70 low_u = min(low_u, low_v); 71 if (low_v > dfn[u] && !Edge[i].ok) 72 brg[brg_num ++] = Edge[i].id; 73 } 74 else if(v != fa) 75 low_u = min(low_u, dfn[v]); 76 } 77 return low_u; 78 } 79 80 int main() 81 { 82 int T, n, m; 83 int u, v; 84 scanf ("%d", &T); 85 while (T --) 86 { 87 init(); 88 scanf ("%d%d", &n, &m); 89 for (int i = 1; i <= m; i ++) 90 { 91 scanf ("%d%d", &u, &v); 92 add(u, v, i); 93 add(v, u, i); 94 } 95 dfs(1, 0); 96 printf ("%d\n", brg_num); 97 sort(brg, brg + brg_num); 98 for (int i = 0; i < brg_num; i ++) 99 { 100 printf ("%d%c", brg[i], i == brg_num -1 ? '\n' : ' '); 101 } 102 if (T) puts(""); 103 } 104 return 0; 105 }