Topic Portal
This problem we know is a topological sort, however, in how to transform the problem took a lot of work, one hours after the last still helpless to see the QWQ.
Obviously we can get a DAG for each task, from his leading task to the one where he even has a side. The topology is sorted on this DAG, and the cost of updating to the task J is Max (F[j],f[i]+val[j]). When we find a point with a degree of 0, we can use it to update the final answer.
Code
1#include <cstdio>2#include <algorithm>3#include <queue>4 5 using namespacestd;6 7 intn,ans,tot,cnt;8 intval[10090],du[10090],head[10090],f[10090],cdu[10090];9 structnode{Ten intnext,to; One}edge[200900]; A - voidAddintXinty) - { theedge[++tot].next=Head[x]; -head[x]=tot; -edge[tot].to=y; - } + - voidtopo () + { Aqueue<int>Q; at for(intI=1; i<=n;i++) - if(du[i]==0) Q.push (i), f[i]=Val[i]; - while(!q.empty ()) - { - intx=Q.front (); Q.pop (); - for(intI=head[x];i;i=edge[i].next) in { - inty=edge[i].to; toF[y]=max (f[y],f[x]+val[y]); + if(--du[y]==0) - { the if(!cdu[y]) ans=Max (ans,f[y]); * ElseQ.push (y); $ }Panax Notoginseng } - } the } + A intMain () the { +scanf"%d",&n); - for(intI=1; i<=n;i++) $ { $ intopt=0, y=0; -scanf"%d%d",&opt,&val[i]); - while(SCANF ("%d", &y) &&y!=0) Add (opt,y), du[y]++,cdu[opt]++; the } - topo ();Wuyiprintf"%d", ans); the return 0; -}
View Code
Details: The size of the open array is very fastidious, the size of the adjacency table is not control, began to re a point.
Luogu P1113 Chores "topological sort" by cellur925