【模板】缩点

https://www.luogu.org/problem/P3387

注意核心代码

#include<iostream>
#include<cstdio>

#define ri register int
#define u int

namespace opt {

    inline u in() {
        u x(0),f(1);
        char s(getchar());
        while(s<'0'||s>'9') {
            if(s=='-') f=-1;
            s=getchar();
        }
        while(s>='0'&&s<='9') {
            x=(x<<1)+(x<<3)+s-'0';
            s=getchar();
        }
        return x*f;
    }

}

using opt::in;

#define NN 100005

#include<cstring>

namespace aft {
    
    u N,cnt,v[NN],id[NN],h[NN];
    
    struct node{
        u to,next;
    }a[NN<<1];
    
    inline void add(const u &x,const u &y){
        a[++cnt].next=h[x],a[cnt].to=y,h[x]=cnt;
    }
    
    u q[NN],l,r,ans[NN];
    
    void run(){
        for(ri i(1);i<=N;++i) if(!id[i]) q[++r]=i,ans[i]=v[i],ans[0]=std::max(ans[0],v[i]);
        while(l+1<=r){
            u x(q[++l]);
            for(ri i(h[x]);i;i=a[i].next){
                u _y(a[i].to);
                if(!--id[_y]) q[++r]=_y;
                ans[_y]=std::max(ans[_y],ans[x]+v[_y]);
                ans[0]=std::max(ans[0],ans[_y]);
            }
        }
        printf("%d",ans[0]);
    }
    
}

namespace mainstay {
    
    u N,M,cnt,h[NN];
    
    struct node{
        u to,next;
    }a[NN<<1];
    
    inline void add(const u &x,const u &y){
        a[++cnt].next=h[x],a[cnt].to=y,h[x]=cnt;
    }
    
    u num,tcnt,v[NN],team[NN],low[NN],dfn[NN];
    
    u stk[NN],r;
    
    void dfs(const u &x){
        low[x]=dfn[x]=++num,stk[++r]=x;
        for(ri i(h[x]);i;i=a[i].next){
            u _y(a[i].to);
            if(!dfn[_y]) dfs(_y),low[x]=std::min(low[x],low[_y]);/////
            else if(!team[_y]) low[x]=std::min(low[x],dfn[_y]);/////
        }
        if(low[x]==dfn[x]){
            ++tcnt;
            while(stk[r+1]^x){
                aft::v[tcnt]+=v[stk[r]];
                team[stk[r--]]=tcnt;
            }
        }
    }
    
    inline void solve() {
        N=in(),M=in();
        for(ri i(1);i<=N;++i) v[i]=in();
        for(ri i(1);i<=M;++i){
            u _a(in()),_b(in());
            add(_a,_b);
        }
        for(ri i(1);i<=N;++i) if(!dfn[i]) dfs(i);
        aft::N=tcnt;
        for(ri x(1);x<=N;++x){
            for(ri i(h[x]);i;i=a[i].next){
                u _y(a[i].to);
                if(team[x]^team[_y]){
                    aft::add(team[x],team[_y]);
                    ++aft::id[team[_y]];
                }
            }
        }
        aft::run();
    }

}

int main() {

    //freopen("x.txt","r",stdin);
    mainstay::solve();

}

 

posted @ 2019-11-15 19:57  pai_hoo  阅读(122)  评论(0编辑  收藏  举报