Title Link: codeforces 461B appleman and Tree
Main topic: A tree, with 0 nodes as the root node, given the Father node of each node, and the color of each point (0 for White, 1 for black), cut off the K-side of the tree, so that the tree into a k+1 of the components, to ensure that each unicom component has and only 1 black nodes. Ask how many methods of partitioning are available.
Problem-Solving ideas: Tree-shaped dp,dp[i][0] and dp[i][1, respectively, the sub-tree segmentation method, I node is located in the Unicom block there is no black node and there is already a black node scheme number.
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace STD;typedef Long LongllConst intMOD =1e9+7;Const intMAXN =1e5+5;intN, V[maxn];ll dp[maxn][2]; vector<int>G[MAXN];voidInit () {memset(DP,0,sizeof(DP));intXscanf("%d", &n); for(inti =1; i < N; i++) {scanf("%d", &x); G[x].push_back (i); } for(inti =0; i < N; i++)scanf("%d", &v[i]);} ll Pow_mod (ll X,intN, ll MoD) {LL ret =1; while(n) {if(n&1RET = ret * x% MoD; x = x * x% MoD; N >>=1; }returnRET;} LL INV (ll x) {if(x = =0)return 1;returnPow_mod (x, mod-2, MOD);}voidSolve (intu) {if(g[u].size () = =0) {Dp[u][v[u]] =1;return; } for(inti =0; I < g[u].size (); i++) solve (g[u][i]);if(V[u]) {dp[u][0] =0; ll& ans = dp[u][1]; Ans =1; for(inti =0; I < g[u].size (); i++) {intk = G[u][i]; Ans = ans * (dp[k][1] + dp[k][0])% MOD; } }Else{dp[u][1] =0; ll& ans = dp[u][0]; Ans =1; for(inti =0; I < g[u].size (); i++) {intk = G[u][i]; Ans = ans * (dp[k][0] + dp[k][1])% MOD; } for(inti =0; I < g[u].size (); i++) {intk = G[u][i]; dp[u][1] + = ans * INV (dp[k][0] + dp[k][1])% MOD * dp[k][1]% MOD; dp[u][1]%= MOD; } }//printf ("%d%d%d\n", U, Dp[u][0], dp[u][1]);}intMain () {init (); Solve0);printf("%lld\n", dp[0][1]% MOD);return 0;}
Codeforces 461B Appleman and tree (DP)