zoukankan      html  css  js  c++  java
  • 【模板】拓扑排序

    【模板】拓扑排序

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<string>
    #include<queue>
    #include<stack>
    #include<algorithm>
    #include<vector>
    #include<map>
    using namespace std;
    const int INF= 0x3f3f3f3f;
    int head[500001];
    int ans=0;
    struct Edge
    {
        int u, v, w, next;
    }e[500001];
    void add(int u,int v)
    {
        e[++ans].next = head[u];
        e[ans].v = v;
        head[u] = ans;
    }
    int IN[500001];//入度
    int OUT[500001];//出度
    int d[500001];
    int n, m;
    void topsort()//拓扑排序
    {
        queue<int> q;
        for (int i = 1; i <= n; i++)
        {
           if(IN[i]==0)//入读为0的点入队列
           {
               q.push(i);
               d[i] = 1;
           }
        }
        while(!q.empty())
         {
             int t = q.front();
             q.pop();
             for (int i = head[t]; i != 0;i=e[i].next)
             {
                 int uu = e[i].v;
                 d[uu] = (d[t]+d[uu])%80112002;
                 IN[uu]--;//入度--
                 if(IN[uu]==0)//入读为0的点入队列
                 {
                     q.push(uu);
                 }
             }
         }
    
    }
    int main()
    {
        cin >> n >> m;
         memset(IN, 0, sizeof(IN));
          memset(d, 0, sizeof(d));
         memset(OUT, 0, sizeof(OUT));
        for (int i = 1; i <= m;i++)
        {
            int x, y;
            cin >> x >> y;
            add(x, y);
            IN[y]++;
            OUT[x]++;
        }
        topsort();
        int total = 0;
        for (int i = 1; i <= n;i++)
        {
            if(OUT[i]==0)
            total= (total+d[i])%80112002;
        }
        cout << total%80112002;
        return 0;
    }
    
    
    
    
    
    
  • 相关阅读:
    [haoi2011]向量
    [haoi2008]硬币购物
    noi2001 [食物链]并查集p1697
    [haoi2012]容易题(数论+容斥的思想)
    p1620田忌赛马(贪心+模拟)
    p1368[扑街]广场铺砖
    [zjoi2010]网络扩容
    是时候写一篇总结了
    P1630 求和
    魔术棋子
  • 原文地址:https://www.cnblogs.com/a821403286/p/13693668.html
Copyright © 2011-2022 走看看