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

B - Burning Bridges

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

Submit Status

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;
}

时间: 2024-08-25 15:49:48

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

小结:双连通分量 &amp; 强连通分量 &amp; 割点 &amp; 割边

概要: 各种dfs时间戳..全是tarjan(或加上他的小伙伴)无限膜拜tarjan orzzzzzzzzz 技巧及注意: 强连通分量是有向图,双连通分量是无向图. 强连通分量找环时的决策和双连通的决策十分相似,但不完全相同. 强连通分量在if(FF[v])后边的else if还要特判是否在栈里,即vis[v],然后才更新LL[u] 割点和强连通分量因为是无向图所以要判个fa,可以在dfs时维护个fa参数 割点如果要求分割的分量,那么就是这个节点对他的子树是割点的数目+1. 割点不需要栈维护但是

并查集的应用之求解无向图中的连接分量个数

一,介绍 本文使用数据结构:并查集 来实现 求解无向图的连通分量个数. 无向图的连通分量就是:无向图的一个极大连通子图,在极大连通子图中任意两个顶点之间一定存在一条路径.对于连通的无向图而言,只有一个连通分量. 二,构造一个简单的无向图 这里仅演示求解无向图的连通分量,因此需要先构造一个无向图.图由顶点和边组成,并采用图的邻接表形式存储.顶点类和边类的定义如下: 1 private class Vertex{ 2 private Integer vertexLabel; 3 private Li

连通性2 无向图的割边 (cut edge)

这是DFS系列的第二篇 割边的概念 In graph theory, a bridge, isthmus, cut-edge, or cut arc is an edgeof a graph whose deletion increases its number of connected components. Equivalently, an edge is a bridge if and only if it is not contained in any cycle. A graph is

ZOJ 2588 Burning Bridges(无向图求割边)

ZOJ 2588 Burning Bridges 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2588 题意:给定一个无向图连通图,(其中可能有重边),要求去掉一条边之后,使得整个图不再连通.输出这些符合条件的边的序号. 思路:这就是一个简单的无向图求割边,需要注意的是这个无向图有重边,重边一定不是割边. 代码: /*========================================= 无向图求割点

【有重边与无重边的无向图的割边求法】

无向图无重边:也就每两个顶点之间最多有一条边相连[也就是根据顶点编号即可确定边][如下] 无向图有重边如:顶点1与顶点2有两条或更多的边直接相连[也就是不能根据顶点编号来确定边][如下] 首先介绍无重边的无向图的割边求法:由于无重边的无向图中可以根据顶点来确定边,所以函数中的参数 u 和 fa  都是顶点. 1 #include <stdio.h> 2 #include <string.h> 3 #include <vector> 4 using namespace s

zoj2588(无向图求割边)

这道题其实就是一道很简单的割边的模板题,不过需要处理重边导师意见有点麻烦的事 #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int MAXN=20005; const int MAXM=200005; struct edge { int to,next,id,num;//这个地方需要在原有模板

HDU 3849 无向图的割边

By Recognizing These Guys, We Find Social Networks Useful Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 2319    Accepted Submission(s): 603 Problem Description Social Network is popular these

Tarjan算法:求解图的割点与桥(割边)

简介: 割边和割点的定义仅限于无向图中.我们可以通过定义以蛮力方式求解出无向图的所有割点和割边,但这样的求解方式效率低.Tarjan提出了一种快速求解的方式,通过一次DFS就求解出图中所有的割点和割边. 欢迎探讨,如有错误敬请指正 如需转载,请注明出处 http://www.cnblogs.com/nullzx/ 1. 割点与桥(割边)的定义 在无向图中才有割边和割点的定义 割点:无向连通图中,去掉一个顶点及和它相邻的所有边,图中的连通分量数增加,则该顶点称为割点. 桥(割边):无向联通图中,去

『Tarjan算法 无向图的割点与割边』

无向图的割点与割边 定义:给定无相连通图\(G=(V,E)\) 若对于\(x \in V\),从图中删去节点\(x\)以及所有与\(x\)关联的边后,\(G\)分裂为两个或以上不连通的子图,则称\(x\)为\(G\)的割点. 若对于\(e \in E\),从图中删去边\(e\)之后,\(G\)分裂为两个不连通的子图,则称\(e\)为\(G\)的割边. 对于很多图上问题来说,这两个概念是很重要的.我们将探究如何求解无向图的割点与割边. 预备知识 时间戳 图在深度优先遍历的过程中,按照每一个节点第一