zoj Burning Bridges 无向连通图 求割点 桥 tarjan

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 
题意就是给个图,求桥,求割连,注意要判重!和求割点,基本上是一样的!桥只需要满足非重边且dfn[u]<low[v],则边u - v是割边。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define E  400050
#define N  10050
int head[N],re[N],id[E],num[E],vise[E],ans,vis[N],dfn[N],low[N],n,m,clock_m,next[E],vec[E],edge_m;
void init(){
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    clock_m=0;edge_m=0;ans=0;
    memset(head,-1,sizeof(head));
    memset(vise,0,sizeof(vise));
    memset(vis,0,sizeof(vis));
}
int addedge(int s,int e,int iid){
    for(int i=head[s];i!=-1;i=next[i])
    if(vec[i]==e){
        num[i]++;
        return 1;
    }
    num[edge_m]=0,id[edge_m]=iid;vec[edge_m]=e;next[edge_m]=head[s];head[s]=edge_m++;
}
int tarjan(int x,int pre){
    dfn[x]=low[x]=clock_m++;
    vis[x]=1;
    for(int i=head[x];i!=-1;i=next[i]){
        int goal=vec[i];
        {
            //if(goal==pre)
            //continue;
            if(vise[id[i]])
            continue;
        }//两种方法都对

        vise[id[i]]=1;
        if(!vis[goal]){
            tarjan(goal,x);
            low[x]=min(low[x],low[goal]);
            if(low[goal]>dfn[x]&&!num[i]){
               re[ans++]=id[i];
            }
        }
        else
            low[x]=min(low[x],dfn[goal]);
    }
    return 1;
}
bool cmp(int a,int b){
    return a<b;
}
int main()
{
    int tcase,s,e;
    bool fir=true;
    scanf("%d",&tcase);
    while(tcase--){
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=m;i++){
            scanf("%d%d",&s,&e);
            addedge(s,e,i);addedge(e,s,i);
        }
        tarjan(1,0);
        sort(re,re+ans,cmp);
        if(fir)fir=false;
        else printf("\n");
        printf("%d\n",ans);
        if(ans){
            for(int i=0;i<ans;i++){
            if(i)
            printf(" %d",re[i]);
            else
            printf("%d",re[i]);
        }
        printf("\n");}

    }
    return 0;
}


你可能感兴趣的:(zoj Burning Bridges 无向连通图 求割点 桥 tarjan)