• 【模板】拓扑排序


    模板题

    还有6天CSP-S,慌得一批

    #include<bits/stdc++.h>
    using namespace std;
    int head[3000005],v[3000005],n,sum,l,end[3000005],rdu[3000500];
    queue<int>q;
    struct node
    {
        int to,next;
    } a[2000005];
    inline void add(int from,int to)
    {
        a[++l].to=to;
        a[l].next=head[from];
        head[from]=l;
        rdu[to]++;
    }
    int tp()
    {
        int ans=0;
        for(int i=1; i<=n; i++)
            if(!rdu[i])q.push(i);
        while(q.size())
        {
            int x=q.front();
            q.pop();
            end[x]+=v[x];
            ans=ans>end[x]?ans:end[x];
            for(register int i=head[x]; i; i=a[i].next)
            {
                rdu[a[i].to]--;
                end[a[i].to]=end[a[i].to]>end[x]?end[a[i].to]:end[x];
                if(!rdu[a[i].to])q.push(a[i].to);
            }
        }
        return ans;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            int num,x=1;
            scanf("%d%d%d",&num,&v[i],&x);
            while(x)
            {
                add(x,i);
                scanf("%d",&x);
            }
        }
        printf("%d",tp());
        return 0;
    }
  • 相关阅读:
    四十八.监控概述 、 Zabbix基础 、 Zabbix监控服务
    123D
    bzoj3879
    bzoj1699
    LA6878
    uoj#149
    687C
    codeforces round #424 div2
    803E
    713C
  • 原文地址:https://www.cnblogs.com/yige2019/p/11826561.html
Copyright © 2020-2023  润新知