ZOJ 2588--Burning Bridges【无向图边双联通 && 输出桥的编号】

来源:互联网 发布:手机淘宝满减怎么设置 编辑:程序博客网 时间:2024/05/16 12:06

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

26 71 22 32 45 41 34 53 610 162 63 76 55 95 41 29 86 42 103 87 91 42 410 51 66 10

Sample Output

23 714 
题目大意:
给出一个无向图,输入n(表示n个定点,1~n), m(m条边,有重边),(2 <= N <= 10 000, 1 <= M <= 100 000),求这个无向图中的桥,并输出桥属于输入中边的id.

tarjin模板题:

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define maxn 10000+10#define maxm 200000+10using namespace std;int n, m;int low[maxn];int dfn[maxn];int head[maxn], cnt;int dfs_clock;int num;//记录有几个桥int cutnum[maxm];//存储桥的编号struct node {    int u, v, again, id, next;};node edge[maxm];void init(){    cnt = 0;    memset(head, -1, sizeof(head));}void add(int u, int v, int id){    int i;    for(i = head[u]; i != -1; i = edge[i].next){        if(edge[i].v == v){            break;        }    }    if(i != -1)        edge[i].again = 1;    else{        edge[cnt] = {u, v, 0, id, head[u]};        head[u] = cnt++;    }}void getmap(){    for(int i = 1; i <= m; ++i){        int u, v;        scanf("%d%d", &u, &v);        add(u, v, i);        add(v, u, i);    }}void tarjan(int u, int fa){    low[u] = dfn[u] = ++dfs_clock;    for(int i = head[u]; i != -1; i = edge[i].next){        int v = edge[i].v;        if(v == fa) continue;        if(!dfn[v]){            tarjan(v, u);            low[u]  = min(low[u], low[v]);            if(low[v] > dfn[u] && edge[i].again == 0){                cutnum[num++] = edge[i].id;            }        }        else            low[u] = min(low[u], dfn[v]);    }}void find(){    memset(dfn, 0, sizeof(dfn));    memset(low, 0, sizeof(low));    num = 0;    dfs_clock = 0;    tarjan(1, -1);}void solve(){    printf("%d\n", num);    if(num == 0)        return ;    sort(cutnum, cutnum + num);    for(int i = 0; i < num; ++i){        if(!i)            printf("%d", cutnum[i]);        else            printf(" %d", cutnum[i]);    }    printf("\n");}int main (){    int T;    scanf("%d", &T);    while(T--){        scanf("%d%d", &n, &m);        init();        getmap();        find();        solve();        if(T)            printf("\n");    }    return 0;}


0 0