【模板】拓扑排序

模板题

还有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;
}

 

posted @ 2019-11-09 16:29  yige_2019  阅读(163)  评论(0编辑  收藏  举报
/**/