zoukankan      html  css  js  c++  java
  • LG 2341 受欢迎的牛

    SCC
    再DFS一下
    即可

    不过为什么我代码在
    BZOJ RE
    LG+O2 WA
    LG AC

    诡异诡异

    #include <cstdio>
    
    int read(){
        int x=0, f=1;char ch=getchar();
        while(ch<'0' || ch>'9'){if(ch=='-')f=-f;ch=getchar();}
        while(ch>='0' && ch<='9'){x=x*10+(ch-'0');ch=getchar();}
        return x*f;
    }
    
    const int MAXV=10111;
    const int MAXE=50111;
    
    int N, M;
    
    struct Vert{
        int FE;
        bool Vis;
        int Bel;
    } V[MAXV];
    
    struct Edge{
        int x, y, next;
        bool i;
    } E[MAXE<<1];
    
    int Ecnt=0;
    
    void addE(int a, int b){
        ++Ecnt;
        E[Ecnt].x=a;E[Ecnt].y=b;E[Ecnt].next=V[a].FE;V[a].FE=Ecnt;E[Ecnt].i=false;
        ++Ecnt;
        E[Ecnt].x=b;E[Ecnt].y=a;E[Ecnt].next=V[b].FE;V[b].FE=Ecnt;E[Ecnt].i=true;
    }
    
    void Vis_init(){
        for(int i=1;i<=N;++i)
            V[i].Vis=false;
    }
    
    int Dfn[MAXV], DFN=0;
    
    void DFS(int at){
        V[at].Vis=true;
        for(int k=V[at].FE, to;k>0;k=E[k].next){
            if(E[k].i)	continue;
            to=E[k].y;
            if(/*!*/V[to].Vis)	continue;
            DFS(to);
        }
        Dfn[++DFN]=at;
    }
    
    void DFS(int at, int b){
        V[at].Vis=true;
        for(int k=V[at].FE, to;k>0;k=E[k].next){
            if(!E[k].i)	continue;
            to=E[k].y;
            if(/*!*/V[to].Vis)	continue;
            DFS(to, b);
        }
        V[at].Bel=b;
    }
    
    int Bcnt=0;
    
    void scc(){
        Vis_init();
        for(int i=1;i<=N;++i){
            if(V[i].Vis)	continue;
            DFS(i);
        }
        Vis_init();
        for(int i=N, j;i>=1;--i){
            j=Dfn[i];
            if(V[j].Vis)	continue;
            DFS(j, ++Bcnt);
        }
    }
    
    bool Win=true;
    int ANS=0;
    
    int main(){
        
        N=read();M=read();
        for(int i=1, a, b;i<=M;++i){
            a=read();b=read();
            addE(b, a);
        }
        
        scc();
        
        Vis_init();
        DFS(Dfn[N]);
        
        for(int i=1;i<=N;++i){
            if(!V[i].Vis){
                Win=false;
                break;
            }
        }
        
        if(Win){
            for(int i=1;i<=N;++i){
                if(V[i].Bel==1)	++ANS;
            }
            printf("%d
    ", ANS);
        }
        else	puts("0");
        
        return 0;
    }
    
    
  • 相关阅读:
    010 --- 第14章 观察者模式
    009 --- 第13章 建造者模式
    008 --- 第12章 外观模式
    007 --- 第10章 模板方法模式
    006 --- 第9章 原型模式
    redis lua 中keys[1] 和argv[1] 的理解
    redis 入门总结
    mysql 8.0 特性简单总结
    MySql事务隔离级别 浅见
    浅谈Java中的String、StringBuffer、StringBuilder
  • 原文地址:https://www.cnblogs.com/Pickupwin/p/9096147.html
Copyright © 2011-2022 走看看