zoukankan      html  css  js  c++  java
  • P3183 [HAOI2016]食物链

    
    /*
        食物链 拓扑图DP 
    */
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define ll long long
    using namespace std;
    
    const int A = 1e7+10;
    const int B = 1e6+10;
    const int mod = 1e9 + 7;
    const int inf = 0x3f3f3f3f;
    
    inline int read() {
      char c = getchar();
      int x = 0, f = 1;
      for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
      for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
      return x * f;
    }
    
    struct node{int v,nxt;} e[B];
    
    int rd[B],cd[B],n,m,f[B],cnt,head[B];
    
    void modify(int u,int v)
    {
        e[++cnt].nxt=head[u];
        e[cnt].v=v;
        head[u]=cnt;
    }
    
    queue<int>q;
    
    int main()
    {
        int x,y;
        n=read(),m=read();
        for (int i=1;i<=m;i++)
        {
            x=read(),y=read();
            modify(x,y);
            rd[y]++, cd[x]++;
        }
            
        for (int i=1;i<=n;i++)
            if (!rd[i]) 
            {
                if (cd[i]) f[i]=1;
                q.push(i);
            }
            
        
        while (!q.empty())
        {
            //cout<<1<<endl; 
            int u=q.front(); 
            q.pop();
            for (int i=head[u];i;i=e[i].nxt)
            {
                int v=e[i].v;
                f[v]+=f[u];
                rd[v]--;
                if (!rd[v]) q.push(v);
            }
        }
        
        int ans=0;
        for (int i=1;i<=n;i++) if (!cd[i]) ans+=f[i];
        printf("%d",ans); 
    }
    
    
    
  • 相关阅读:
    WindowsServer2003SP2EnterpriseEdition激活码
    2016/12/14渗透学习总结
    技术之路注定孤独
    几道office题的总结
    一路走来,只是清风
    【IIS】IIS中同时满足集成模式和经典模式
    IntelliJ IDEA如何build path
    kafka的集群安装
    尚硅谷 kafka
    队列
  • 原文地址:https://www.cnblogs.com/lToZvTe/p/14527394.html
Copyright © 2011-2022 走看看