zoukankan      html  css  js  c++  java
  • nyoj 120 建边构强连通

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    #define  N  200
    
    struct node
    {
        int u,v,next;
    } bian[N*N];
    int n,dfn[N],low[N],stac[N],yong,head[N],visit[N],ans,top,inde,suo[N],indegree[N],outdegree[N];
    void init()
    {
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(head,-1,sizeof(head));
        memset(stac,0,sizeof(stac));
        memset(visit,0,sizeof(visit));
        memset(suo,0,sizeof(suo));
        memset(indegree,0,sizeof(indegree));
        memset(outdegree,0,sizeof(outdegree));
        ans=0;
        yong=0;
        top=0;
        inde=0;
    }
    void addedge(int u,int v)
    {
        bian[yong].u=u;
        bian[yong].v=v;
        bian[yong].next=head[u];
        head[u]=yong++;
    }
    int Max(int a,int b)
    {
        return a>b?a:b;
    }
    int Min(int a,int b)
    {
        return a>b?b:a;
    }
    void tarjan(int u)
    {
        dfn[u]=low[u]=++inde;
        stac[++top]=u;
        visit[u]=1;
        int i;
        for(i=head[u]; i!=-1; i=bian[i].next)
        {
            if(!dfn[bian[i].v])
            {
                tarjan(bian[i].v);
                low[u]=Min(low[u],low[bian[i].v]);
            }
            else if(visit[bian[i].v]==1)
                low[u]=Min(low[u],dfn[bian[i].v]);
    
        }
        if(dfn[u]==low[u])
        {
            ans++;
            int t;
            do
            {
                t=stac[top--];
                visit[t]=2;
                suo[t]=ans;
            }
            while(t!=u);
        }
    }
    int main()
    {
        int t,n,m,i,fin,fout;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            init();
            for(i=1; i<=n; i++)
            {
                while(scanf("%d",&m),m)
                    addedge(i,m);
            }
            for(i=1; i<=n; i++)
                if(visit[i]!=2)
                    tarjan(i);
                if(ans==1) {//important
                    printf("0
    ");
                    continue;
                }
         for(i=0;i<yong;i++)
         if(suo[bian[i].u]!=suo[bian[i].v]) {
            indegree[suo[bian[i].u]]++;
            outdegree[suo[bian[i].v]]++;
         }
            fin=0;
            fout=0;
            for(i=1; i<=ans; i++)
            {
                if(indegree[i]==0)
                    fin++;
                if(outdegree[i]==0)
                    fout++;
            }
            printf("%d
    ",Max(fin,fout));
        }
    
        return 0;
    }
    

  • 相关阅读:
    iterm2 关闭命令行的行数限制
    设置Mac关闭显示器后不睡眠
    设置iPhone内容隔空投送到mac
    Mac关闭动画效果
    mac使用Jmeter
    Mac 设置文件夹共享
    设置iPhone可以投屏到mac上
    Mac关闭显示亮度自动调节功能
    Mac系统无法使用root用户
    Mac设置主显示器
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410731.html
Copyright © 2011-2022 走看看