无向图的边双连通分量(FROM Redundant Paths POJ - 3177 )

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.
Input
Line 1: Two space-separated integers: F and R

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Line 1: A single integer that is the number of new paths that must be built.
Sample Input
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
2
思路:本质上就是找图的边的双连通分量。
做法就是在求出那些是桥的基础上,分别给每个点做一下dfs,给在同一个双两桶分量的点打上标记说明是属于哪个点的。
题目应该默认是应该连通图,不然做起来还会麻烦一点。
代码:

import java.util.Arrays;
import java.util.Scanner;
public class Main
{   
    static int SIZE=5005;
    static int head[]=new int[SIZE],to[]=new int[20005],next[]=new int[20005];
    static int cnt=0;
    static void addEdge(int x,int y)
    {
        to[cnt]=y;next[cnt]=head[x];head[x]=cnt++;
        to[cnt]=x;next[cnt]=head[y];head[y]=cnt++;
    }//以上建图需要
    static int dfn[]=new int[SIZE],low[]=new int[SIZE];
    static int index;
    static boolean bridge[]=new boolean[SIZE<<1];
    static void tarjan(int x,int edge)
    {
        dfn[x]=low[x]=++index;
        for(int i=head[x];i>=0;i=next[i])
        {
            int y=to[i];
            if(dfn[y]==0)
            {
                tarjan(y,i);
                low[x]=Math.min(low[x], low[y]);
                if(dfn[x]1]=true;
            }
            else
                if(i!=(edge^1))
                    low[x]=Math.min(low[x], dfn[y]);

        }
    }//以上找桥的需要
    static void init()
    {
        Arrays.fill(head, -1);//这个实际上可以去任何值,但是,我们为方便找反向边所以就初始为-1
        //比如0,1是一对 2,3是一对,只要对每个值和1异或一下,就能找到对应的反向边
        cnt=0;
    }
    static int n,m;
    static int be[]=new int[SIZE];//给每个点标记是属于哪个连通分量
    static int deg[]=new int[SIZE];//统计缩点以后的的图(树)中的点的度数
    static int count=0;
    static void dfs(int x)
    {
        be[x]=count;
        for(int i=head[x];i>=0;i=next[i])
        {
            int y=to[i];
            if(bridge[i]||be[y]!=0)
                continue;
            dfs(y);
        }
    }
    static int work()
    {
        count=0;
        for(int i=1;i<=n;i++)
            if(be[i]==0)
            {
                ++count;
                dfs(i);//给每个点打上标记
            }
        Arrays.fill(deg, 0);
        for(int i=1;i2)
        {
            if(bridge[i])//然后统计缩点之后每个点的度数
            {
                deg[be[to[i]]]++;
                deg[be[to[i^1]]]++;
            }
        }
        int ans=0;
        for(int i=1;i<=count;i++)
            if(deg[i]==1)ans++;
        return (ans+1)/2;

    }
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        m=sc.nextInt();
        init();
        for(int i=0;i1,-1);//因为是连通的,所以直接dfs一次所有的点都可以访问到
        System.out.println(work()); 
    }
}

你可能感兴趣的:(边双连通分量,图论java实现)