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\n", ANS);
    }
    else    puts("0");
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Pickupwin/p/9096147.html
今日推荐