Link:
Http://codeforces.com/contest/462/problem/D
Test instructions
A tree given n points,
0 is the root, the following n-1 line represents the parent node of each point
The last row n number represents the color of each point, 0 is white, and 1 is black.
Divide the tree into several unicom blocks so that each unicom block has and only one black point, asking how many different methods (result mod1e9+7)
Exercises
Tree DP, each point has 2 states, has been attributed to a black spot and does not belong to a black spot.
Code:
to intN; + intX[MAXN]; - VI G[MAXN]; thell dp[maxn][2]; * $ voidDfsintu) {Panax Notoginseng if(X[u]) dp[u][1] =1; - Elsedp[u][0] =1; theRep (I,0, G[u].size ()) { + intv =G[u][i]; A Dfs (v); thell old[2] = {dp[u][0], dp[u][1] }; +dp[u][0] = (old[0] * dp[v][1] + old[0] * dp[v][0]) %MOD; -dp[u][1] = (old[1] * dp[v][1] + old[1] * dp[v][0] + old[0] * dp[v][1]) %MOD; $ } $ } - - intMain () { theIos::sync_with_stdio (false), Cin.tie (0); -CIN >>N;WuyiRep (I,1, N) { the intp; -CIN >>p; Wu G[P].PB (i); - } AboutRep (I,0, N) Cin >>X[i]; $Dfs0); -cout << dp[0][1] <<Endl; - return 0; -}
Codeforces Round #263 (Div. 2) D. Appleman and tree DP