http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804
Test instructions: ...
Idea: For a path, when traversing to a point, the previously traversed points can reach it, so at this time the contribution to the answer is ∑ (A1 + A2 + a3 + ... + ai) * BV, where A is the point that was traversed before, V is the point that is currently traversed.
It's easy to think about it. Like the prefix and, each time traversing to a V point, the a[u] is added to A[v], and then as usual topological sort to do it.
1#include <bits/stdc++.h>2 using namespacestd;3 #defineN 1000104typedefLong LongLL;5typedef pair<int, ll>P;6 Const intMOD = 1e9 +7;7 structEdge {8 intV, NXT;9 } Edge[n];Ten intVis[n], N, M, Head[n], tot,inch[N]; One LL A[n], b[n], ans; Aqueue<int>que; - - voidADD (intUintV) {Edge[tot] = (edge) {V, head[u]}; head[u] = tot++; } the - voidBFS () { - while(!que.empty ()) Que.pop (); - for(inti =1; I <= N; i++) + if(inch[I] = =0) Que.push (i); - while(!Que.empty ()) { + intU =Que.front (); Que.pop (); A for(inti = Head[u]; ~i; i =edge[i].nxt) { at intv =edge[i].v; -Ans = (ans + a[u] * B[v]% MOD)%MOD; -A[V] = (A[u] + a[v])%MOD; ---inch[v]; - if(inch[v] = =0) Que.push (v); - } in } - } to + intMain () { - while(~SCANF ("%d%d", &n, &m)) { the for(inti =1; I <= N; i++) scanf ("%lld%lld", &a[i], &b[i]); *Ans = tot =0; $Memset (Head,-1,sizeof(head));Panax NotoginsengMemsetinch,0,sizeof(inch)); - for(inti =1; I <= m; i++) { the intU, v; scanf"%d%d", &u, &v); +ADD (U, v);inch[v]++; A } the BFS (); +printf"%lld\n", ans%MOD); - } $ return 0; $}
CSU 1804: A forward loop graph (topological sort)