Appleman have a tree with n vertices. Some of the vertices (at least one) is colored black and other vertices is colored white.
Consider a set consisting of K (0≤ k < n) edges of Appleman ' s tree. If Appleman deletes these edges from the tree and then it'll split into (K + 1) parts. Note, that all part is a tree with colored vertices.
Now Appleman wonders, what's the number of sets splitting the tree in such a-the-the-each-resulting part would have exact Ly one black vertex? Find this number modulo 1000000007 (9 + 7).
Input
The first line contains an integer n (2≤ n ≤105)-the number of tree vertices.
n -1 integers P 0, p 1, ..., p n -2 (0≤ p I ≤ i ). Where p i means that there was an edge Connecting Vertex ( i + 1) of the tree and vertex p i . Consider tree vertices is numbered from 0 to N -1.
n integers x 0, x 1, ..., x N -1 ( x i is either 0 or 1). If x i is equal To 1, Vertex i is colored black. Otherwise, Vertex i is colored white.
Output
Output a single integer-the number of ways to split the tree modulo 1000000007 (9 + 7).
Sample Test (s)input
3
0 0
0 1 1
Output
2
input
6
0 1 1) 0 4
1 1 0 0 1 0
Output
1
input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
Test instructions
A tree, n nodes, numbered 0~n-1
Each node is painted black or white, 1 for black, 0 for White
If the tree is removed by the K-bar, the tree is divided into k+1 parts, each part is also a tree, if each part has and only one node is black,
Then this is a reasonable operation.
Number of scenarios for reasonable operation (1E9+7)
If the value of black as a node is 1, the value of white as a node is 0
The value of a tree = The sum of the values of all nodes on the tree
The problem is translated into:
Divides a tree into several parts, each of which has a value of 1 of the number of programs.
Tree-shaped DP
DP[I][0]: A subtree with the root of I, and the number of scenarios where I have a value of 0%mod
DP[I][1]: A subtree with the root of I, and the number of scenarios where I have a value of 1%mod
DFS with root=0
Then output: dp[0][1]
Code:
1#include <cstdio>2#include <cstring>3 4 using namespacestd;5 6 Const intmaxn=1e5+5;7 Const intmod=1e9+7;8 #definell Long Long9 Ten structEdge One { A intTo,next; - }; -Edge edge[maxn<<1]; the intHEAD[MAXN]; - inttot=0; -ll dp[maxn][2]; - intCOST[MAXN]; + - voidInit () + { Amemset (head,-1,sizeofhead); atMemset (DP,0,sizeofDP); - } - - voidAddedge (intUintv) - { -edge[tot].to=v; inedge[tot].next=Head[u]; -head[u]=tot++; to } + - voidSolveint ); the voidDfsint,int ); * $ intMain ()Panax Notoginseng { - init (); the intN; +scanf"%d",&n); A for(intI=1; i<n;i++) the { + intp; -scanf"%d",&p); $ Addedge (i,p); $ Addedge (p,i); - } - for(intI=0; i<n;i++) the { -scanf"%d",&cost[i]);Wuyi } the solve (n); - return 0; Wu } - About voidSolveintN) $ { -Dfs0,-1); -printf"%d\n",(int) dp[0][1]); - return ; A } + the voidDfsintUintpre) - { $ if(Cost[u]) the { thedp[u][1]=1; thedp[u][0]=0; the } - Else in { thedp[u][0]=1; thedp[u][1]=0; About } the for(intI=head[u];~i;i=edge[i].next) the { the intv=edge[i].to; + if(v==pre) - Continue; the DFS (v,u);Bayi if(Cost[u]) the { thedp[u][1]*= (dp[v][0]+dp[v][1]); -dp[u][1]%=MoD; - } the Else the { thedp[u][1]=dp[u][1]* (dp[v][0]+dp[v][1]) +dp[u][0]*dp[v][1]; thedp[u][1]%=MoD; -dp[u][0]*= (dp[v][0]+dp[v][1]); thedp[u][0]%=MoD; the } the }94}View Code
CF 461B Appleman and tree DP