ZOJ 2588 Burning Bridges

Burning Bridges
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2588

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 

题意:求桥,可能有重边

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 10001
using namespace std;
int tot,front[N],to[N*20],nxt[N*20];
int sum;
int dfn[N],low[N];
struct BRIDGE
{
    int u,v,id;
    bool f;
}e[N*10];
void add(int u,int v)
{
    to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
    to[++tot]=u; nxt[tot]=front[v]; front[v]=tot;
}
void tarjan(int now,int last)
{
    dfn[now]=low[now]=++tot;
    for(int i=front[now];i;i=nxt[i])
    {
        if(i==(last^1)) continue;
        if(!dfn[to[i]])
        {
            tarjan(to[i],i);
            low[now]=min(low[now],low[to[i]]);
        }
        else low[now]=min(low[now],dfn[to[i]]);
    }
    if(low[now]>=dfn[now] && last) 
    {
        e[++sum].v=now,
        e[sum].u=to[last^1],
        e[sum].id=last>>1,
        e[sum].f=false;
    }
}
bool cmp(BRIDGE p,BRIDGE q)
{
    return p.id<q.id;
}
int main()
{
    int T;
    int u,v,n,m,dont,cnt;
    bool ok1;
    scanf("%d",&T);
    while(T--)
    {
        memset(front,0,sizeof(front));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        sum=0;
        scanf("%d%d",&n,&m);
        tot=1;
        while(m--)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        tot=0;
        tarjan(1,0);
        dont=0;
        for(int i=1;i<=sum;i++)
        {
            ok1=true;
            for(int j=front[e[i].u];j && ok1;j=nxt[j])
             if(to[j]==e[i].v && j>>1!=e[i].id) ok1=false;
            if(ok1) e[i].f=true;
        }
        sort(e+1,e+sum+1,cmp);
        printf("%d\n",sum-dont);
        cnt=0;
        for(int i=1;i<=sum;i++) 
        {
            if(!e[i].f) continue;
            cnt++;
            if(cnt!=sum-dont) printf("%d ",e[i].id);
            else printf("%d\n",e[i].id);
        }
        if(T) puts("");
    }
}

 



posted @ 2017-08-18 08:25  TRTTG  阅读(247)  评论(0编辑  收藏  举报