Burning Bridges
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
Author: Andrew Stankevich
Source: Andrew Stankevich's Contest #5
题目类型:求解割边(可能有重边) Tarjan
算法分析:求解割边的过程和求解割点的类似,只是满足:low[v] > dfsn[u],u是v的父节点。若图中存在重边,则直接特判即可,注意输出的格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
/************************************************* Author :supermaker Created Time :2016/1/21 13:05:23 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 10000 + 66; int vis[maxn], dfsn[maxn], low[maxn], out[maxn*10]; int n, m, id, len; map <PII, int> overedge; vector <PII> edge[maxn*10]; void Init (int rt) { overedge.clear (); for (int i = 0; i < maxn * 10; i++) edge[i].clear (); dfsn[rt] = low[rt] = id = 1; memset (vis, 0, sizeof (vis)); vis[rt] = 1; len = 0; } void dfs (int u, int par) { for (int i = 0; i < edge[u].size (); i++) { int v = edge[u][i].first; if (!vis[v]) { vis[v] = 1; dfsn[v] = low[v] = ++id; dfs (v, u); low[u] = min (low[u], low[v]); if (low[v] > dfsn[u] && overedge[PII(u,v)] == 1) out[++len] = edge[u][i].second; } else if (v != par) low[u] = min (low[u], dfsn[v]); } } int main() { //CFF; //CPPFF; int t, flag = 1; scanf ("%d", &t); while (t--) { if (flag++ > 1) puts (""); scanf ("%d%d", &n, &m); Init (1); for (int i = 1; i <= m; i++) { int u, v; scanf ("%d%d", &u, &v); edge[u].push_back (PII (v, i)); edge[v].push_back (PII (u, i)); if (overedge.find (PII (u, v)) == overedge.end ()) overedge[PII(u,v)] = overedge[PII(v,u)] = 1; else overedge[PII(u,v)]++, overedge[PII(v,u)]++; } dfs (1, -1); printf ("%d\n", len); sort (out + 1, out + 1 + len); for (int i = 1; i <= len; i++) { if (i == 1) printf ("%d", out[i]); else printf (" %d", out[i]); } if (len) puts (""); } return 0; } |
- « 上一篇:poj1043
- poj2942:下一篇 »