xtu summer individual 5 E - Burning Bridges

Burning Bridges

Time Limit: 5000ms

Memory Limit: 32768KB

This problem will be judged on ZJU. Original ID: 2588
64-bit integer IO format: %lld      Java class name: Main

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

Source

Andrew Stankevich‘s Contest #5

Author

Andrew Stankevich

解题:割边、桥。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #define LL long long
10 #define INF 0x3f3f3f3f
11 using namespace std;
12 const int maxn = 10010;
13 struct arc{
14     int to,num,id;
15     arc():num(0){}
16 };
17 vector<arc>g[maxn];
18 int low[maxn],dfn[maxn],ans[maxn*10],id,_index;
19 int tot,n,m;
20 void addArc(int u,int v){
21     bool flag = true;
22     int i,j;
23     for(i = 0; i < g[u].size(); i++){
24         if(v == g[u][i].to){
25             flag = false;
26             g[u][i].num++;
27             for(j = 0; j < g[v].size(); j++){
28                 if(g[v][j].to == u){
29                     g[v][j].num++;
30                     break;
31                 }
32             }
33             id++;
34             break;
35         }
36     }
37     if(flag){
38         arc temp;
39         temp.num = 1;
40         temp.id = id++;
41         temp.to = v;
42         g[u].push_back(temp);
43         temp.to = u;
44         g[v].push_back(temp);
45     }
46 }
47 void tarjan(int u,int fa){
48     int v,i,j;
49     dfn[u] = low[u] = _index++;
50     for(i = 0; i < g[u].size(); i++){
51         v = g[u][i].to;
52         if(dfn[v] == -1){
53             tarjan(v,u);
54             low[u] = min(low[u],low[v]);
55         }else if(v != fa) low[u] = min(low[u],dfn[v]);
56     }
57     if(dfn[u] == low[u]){
58         v = u;
59         u = fa;
60         for(i = 0; i < g[u].size(); i++){
61             if(v == g[u][i].to){
62                 if(g[u][i].num == 1) ans[tot++] = g[u][i].id;
63                 break;
64             }
65         }
66     }
67 }
68 int main(){
69     int t,i,j,u,v;
70     scanf("%d",&t);
71     while(t--){
72         tot = 0;
73         id = _index = 1;
74         scanf("%d %d",&n,&m);
75         for(i = 0; i <= n; i++){
76             dfn[i] = low[i] = -1;
77             g[i].clear();
78         }
79         for(i = 0; i < m; i++){
80             scanf("%d%d",&u,&v);
81             addArc(u,v);
82         }
83         dfn[1] = low[1] = _index++;
84         tarjan(1,1);
85         printf("%d\n",tot);
86         if(tot){
87             sort(ans,ans+tot);
88             printf("%d",ans[0]);
89             for(i = 1; i < tot; i++)
90                 printf(" %d",ans[i]);
91             puts("");
92         }
93         if(t) puts("");
94     }
95     return 0;
96 }

xtu summer individual 5 E - Burning Bridges,布布扣,bubuko.com

时间: 2024-10-03 14:55:31

xtu summer individual 5 E - Burning Bridges的相关文章

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

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 sys

Burning Bridges 求tarjan求割边

Burning Bridges 给出含有n个顶点和m条边的连通无向图,求出所有割边的序号. 1 #include <cstdio> 2 #include <cstring> 3 #define clr(a) memset(a,0,sizeof(a)) 4 #define N 10005 5 #define M 100005 6 #define MIN(a,b) ((a)>(b)?(b):(a)) 7 typedef struct NodeStr //边结点 8 { 9 int

xtu summer individual 6 B - Number Busters

Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 382B64-bit integer IO format: %I64d      Java class name: (Any) Arthur and Alexander are number busters. Today they've got a competition.

xtu summer individual 1 E - Palindromic Numbers

E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this

xtu summer individual 6 D - Checkposts

Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 427C64-bit integer IO format: %I64d      Java class name: (Any) Your city has n junctions. There are m one-way roads between the junctions. A

xtu summer individual 5 D - Subsequence

Subsequence Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 353064-bit integer IO format: %I64d      Java class name: Main There is a sequence of integers. Your task is to find the longest subsequence that sa

xtu summer individual 1 A - An interesting mobile game

An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 329564-bit integer IO format: %I64d      Java class name: Main XQ,one of the three Sailormoon girls,is usually playing mobile games o

xtu summer individual 2 C - Hometask

Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 154A64-bit integer IO format: %I64d      Java class name: (Any) Sergey attends lessons of the N-ish language. Each lesson he receives a hometas

xtu summer individual 5 F - Post Office

Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 116064-bit integer IO format: %lld      Java class name: Main There is a straight highway with villages alongside the highway. The highway is repres