有向图的强连通分量

HDU - 2767(强连通分量)
求一个有向图里加最少的边使得任意两点可达。。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main
{   
    static int n,m;
    static int maxx=20005;
    static int maxn=50005;
    static int head[]=new int[maxx];
    static int next[]=new int[maxn];
    static int to[]=new int[maxn];
    static int cnt;
    static void addEdge(int x,int y)
    {
        to[++cnt]=y;next[cnt]=head[x];head[x]=cnt;
    }
    static int DFN[]=new int[maxx];
    static int low[]=new int[maxx];
    static boolean inS[]=new boolean[maxx];
    static int be[]=new int[maxx];
    static int index;
    static int st[]=new int[maxx];
    static int p=0;
    static int count;
    static void tarjan(int v)
    {
        DFN[v]=low[v]=++index;
        st[++p]=v;
        inS[v]=true;
        for(int i=head[v];i>0;i=next[i])
        {
            int u=to[i];
            if(DFN[u]==0)
            {
                tarjan(u);
                low[v]=Math.min(low[v], low[u]);
            }
            else
                if(inS[u])
                    low[v]=Math.min(low[v], DFN[u]);
        }
        if(DFN[v]==low[v])
        {
            count++;
            while(st[p]!=v)
            {
                be[st[p]]=count;
                inS[st[p]]=false;
                p--;
            }
            be[st[p]]=count;
            inS[st[p]]=false;
            p--;
        }
    }
    static int in[]=new int[maxx];
    static int out[]=new int[maxx];
    static int work()
    {
        //System.out.println("haha");
        if(count==1)
            return 0;
        for(int i=1;i<=n;i++)
        {
            for(int j=head[i];j>0;j=next[j])
            {
                if(be[i]!=be[to[j]])
                {
                    out[be[i]]++;
                    in[be[to[j]]]++;
                }
            }
        }
        int inNum=0,outNum=0;
        for(int i=1;i<=count;i++)
        {
            inNum+=in[i]==0?1:0;
            outNum+=out[i]==0?1:0;
        }
        return Math.max(inNum, outNum);
    }
    static void init()
    {
        Arrays.fill(head, 0);
        Arrays.fill(in, 0);
        Arrays.fill(out, 0);
        Arrays.fill(DFN, 0);
        cnt=0;
        index=0;
        count=0;
    }
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            init();
            n=sc.nextInt();
            m=sc.nextInt();
            while(m-->0)
                addEdge(sc.nextInt(),sc.nextInt());
            for(int i=1;i<=n;i++)
                if(DFN[i]==0)
                    tarjan(i);
            System.out.println(work());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值