码迷,mamicode.com
首页 > 其他好文 > 详细

zoj2588(连通分量,求解无向图的割边)

时间:2014-08-26 11:39:15      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   使用   io   strong   

B - Burning Bridges
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

 

Description

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 


题目要求无向图的割边,并且输出,tarjan算法模板。

对于无向图 存储两次u->v,v->u  用vis标记是否被使用过,在算法中,u->v边中,如果low[v] > dnf[u],就证明u->v是割边。

 

#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
#define maxn 11000
struct node{
    int u , v ;
    int next ;
} edge[210000] ;
int head[maxn] , cnt , vis[210000] ;
int dnf[maxn] , low[maxn] , time ;
int p[maxn] , ans ;
stack <int> sta;
void add(int u,int v)
{
    edge[cnt].u = u ; edge[cnt].v = v ;
    edge[cnt].next = head[u] ; head[u] = cnt++ ;
    edge[cnt].u = v ; edge[cnt].v = u ;
    edge[cnt].next = head[v] ; head[v] = cnt++ ;
}
void tarjan(int u)
{
    dnf[u] = low[u] = ++time ;
    int v , i , j ;
    for(i = head[u] ; i != -1 ; i = edge[i].next)
    {
        if( vis[i] ) continue ;
        vis[i] = vis[i^1] = 1 ;
        v = edge[i].v ;
        if( !dnf[v] )
        {
            sta.push(i) ;
            tarjan(v) ;
            low[u] = min( low[u],low[v] );
            if( low[v] > dnf[u] )
            {
                p[ans++] = i/2+1 ;
            }
        }
        else if( dnf[v] < dnf[u] )
        {
            sta.push(i) ;
            low[u] = min( low[u],dnf[v] ) ;
        }
    }

}
int main()
{
    int t , n , m , i , u , v ;
    scanf("%d", &t);
    while(t--)
    {
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
        memset(dnf,0,sizeof(dnf));
        memset(low,0,sizeof(low));
        memset(instack,0,sizeof(instack));
        cnt = ans = time = 0 ;
        scanf("%d %d", &n, &m);
        while(m--)
        {
            scanf("%d %d", &u, &v);
            add(u,v);
        }
        while( !sta.empty() )
            sta.pop();
        tarjan(1);
        printf("%d\n", ans);
        sort(p,p+ans);
        for(i = 0 ; i < ans ; i++)
        {
            if(i == ans-1)
                printf("%d\n", p[i]);
            else
                printf("%d ", p[i]);
        }
        if(t)
            printf("\n");
    }
    return 0;
}

zoj2588(连通分量,求解无向图的割边)

标签:des   style   blog   http   color   os   使用   io   strong   

原文地址:http://blog.csdn.net/winddreams/article/details/38844705

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!