Title Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1804
Chinese test instructions will not say.
Dfs can be traced back from the bottom to the root, and the code should look clear.
1 //#pragma COMMENT (linker, "/stack:102400000, 102400000")2#include <algorithm>3#include <iostream>4#include <cstdlib>5#include <cstring>6#include <cstdio>7#include <vector>8#include <cmath>9#include <ctime>Ten#include <list> One#include <Set> A#include <map> - using namespacestd; -typedefLong LongLL; thetypedef pair <int,int>P; - Const intN = 1e5 +5; - structEdge { - intnext, to; + }edge[n]; - intHead[n], tot,inch[N]; +LL A[n], B[n], ans, d[n], mod = 1e9 +7;//D[i] = b[i.son]*count[i,j]+b[i] A BOOLVis[n]; at - voidInitintN) { - for(inti =1; I <= N; ++i) { -Head[i] =-1; - inch[I] =0; -Vis[i] =false; in } -tot =0; toAns =0; + } - theInlinevoidAdd_edge (intUintv) { *Edge[tot].next =Head[u]; $Edge[tot].to =v;Panax NotoginsengHead[u] = tot++; - } the + voidDfsintu) { AD[u] = B[u]%MoD; the for(inti = Head[u]; ~i; i =Edge[i].next) { + intv =edge[i].to; - if(!vis[v]) {//indicating that this point and the subtree are not visited $ Dfs (v); $VIS[V] =true; - } -Ans = (ans + a[u] * d[v]% MoD)%MoD; theD[u] = (D[v] + d[u])%MoD; - }Wuyi } the - intMain () Wu { - intN, M, U, v; About while(SCANF ("%d%d", &n, &m)! =EOF) { $ for(inti =1; I <= N; ++i) { -scanf"%lld%lld", A + I, B +i); - } - init (n); A for(inti =1; I <= m; ++i) { +scanf"%d%d", &u, &v); the Add_edge (U, v); -++inch[v]; $ } the for(inti =1; I <= N; ++i) { the if(!inch[i]) {//0 in the degree the DFS (i); the } - } inprintf"%lld\n", ans); the } the return 0; About}
CSU OJ 1804: A Forward-free graph (Dfs backtracking)