ZOJ 2588 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 

题意,给出边,求桥。

思路:模板题,就是有个坑点。wa了很多发。当桥的个数为0时,输出一个空行。

#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
struct path
{
    int to,nextt,k;
}A[200010];
int x,y,t,n,m,tot,carry,num,len;
int head[10010],DFN[10010],LOW[10010],ans[100010];
void add(int from,int to,int k)
{
    A[tot].to=to;
    A[tot].k=k;
    A[tot].nextt=head[from];
    head[from]=tot++;
}
int tarjan(int u,int pre)
{
    int tem;
    DFN[u]=LOW[u]=++carry;
    for(int i=head[u];i!=-1;i=A[i].nextt)
    {
        tem=A[i].to;
        if(i==pre) continue;
        if(u==1) num++;
        if(DFN[tem]==-1)
        {
            tarjan(tem,i^1);
            LOW[u]=min(LOW[u],LOW[tem]);
            if(LOW[tem]>DFN[u])
            {
                ans[len++]=A[i].k;
            }
        }
        else
        {
            LOW[u]=min(LOW[u],DFN[tem]);
        }
    }
}
void init()
{
    tot=carry=num=len=0;
    memset(head,-1,sizeof(head));
    memset(DFN,-1,sizeof(DFN));
    memset(LOW,-1,sizeof(LOW));
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y,i);
            add(y,x,i);
        }
        tarjan(1,-1);
        sort(ans,ans+len);
        printf("%d\n",len);
        if(len==0)//这个地方被坑了无数发,到后面看题解才发现。
        {
            printf("\n");
            continue;
        }
        if(len>=0)
            printf("%d",ans[0]);
        for(int i=1;i<len;i++)
            printf(" %d",ans[i]);
        printf("\n");
        if(t)
            printf("\n");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41380961/article/details/84610349
ZOJ