zoukankan      html  css  js  c++  java
  • BZOJ 4562: [Haoi2016]食物链 拓扑排序

    建反图,跑一个拓扑排序dp即可. 

    Code: 

    #include <bits/stdc++.h>   
    #define ll long long  
    #define N 100005    
    #define setIO(s) freopen(s".in","r",stdin) 
    using namespace std;     
    ll f[N];     
    queue<int>q;    
    int edges,n,m;    
    int in[N],out[N],hd[N],to[N<<1],nex[N<<1],mk[N];   
    void add(int u,int v) 
    {
        nex[++edges]=hd[u],hd[u]=edges,to[edges]=v;   
    }
    int main() 
    { 
        int i,j; 
        // setIO("input"); 
        scanf("%d%d",&n,&m); 
        for(i=1;i<=m;++i) 
        {
            int a,b; 
            scanf("%d%d",&a,&b),add(b,a),++in[a],++out[b];   
        }   
        for(i=1;i<=n;++i) 
        { 
            if(in[i]==0 && out[i]!=0) 
            {
                q.push(i),f[i]=1,mk[i]=1;        
            } 
        } 
        for(;!q.empty();) 
        {
            int u=q.front();q.pop(); 
            for(int i=hd[u];i;i=nex[i]) 
            {
                int v=to[i];        
                f[v]+=f[u];                     
                --in[v];   
                if(in[v]==0) q.push(v);   
            }
        }   
        ll ans=0; 
        for(i=1;i<=n;++i) 
        {
            if(out[i]==0 && !mk[i]) 
            {
                ans+=f[i];    
            }
        } 
        printf("%lld
    ",ans);    
        return 0;   
    }
    

      

  • 相关阅读:
    平板涂色
    速算游戏_NOI导刊2011提高(04)
    信息学奥赛一本通——配套刷题网站
    求10000以内n的阶乘
    大整数的因子
    计算2的N次方
    大整数加法
    带余除法
    A/B 高精度
    A*B 高静度
  • 原文地址:https://www.cnblogs.com/guangheli/p/11557464.html
Copyright © 2011-2022 走看看