cf462d
Codeforces Round #263 (Div. 2) D
Codeforces Round #263 (Div. 1) B
B. Appleman and Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output 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). InputThe first line contains an integer n (2≤ n ≤105)-the number of tree vertices. The second line contains the description of the tree: 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. The third line contains the description of the colors of the vertices: n integers x0, x< /c5>1, ..., xn -1 (xi is either 0 or 1). If xi was equal to 1, vertex i was colored black. Otherwise, vertex i was colored white. OutputOutput a single integer-the number of ways to split the tree modulo 1000000007 (9 + 7). Sample Test (s) Input3 0 0 0 1 1 Output2 Input6 0 1 1) 0 4 1 1 0 0 1 0 Output1 Input10 0 1 2 1 4 4 4 0 8 0 0 0 1 0 1 1 0 0 1 Output27
|
Test Instructions: there are n nodes of the tree, the node number 0~n-1,0 for the root, respectively, give 1~n-1 father, and then give 0~n-1 each node color (0 for white, 1 for black), to be some of the edges cut off, so that each unicom block has and only 1 black spots, to find the type of cut method.
The puzzle : tree-shaped DP.
From the root dfs,f[x][0] represents the {x-point and its subtree, X-point connected to the Father node of the edge} This whole lump, how many ways to make x this link block no black spot (X is black point when this is not 0, is the parent of X cut off the number of species)
F[X][1] is the number of black dots in this unicom block.
It's too hard! No wonder we all dropped points fly, although the code looks very short, I do not want to come out ah look at half a day or do not understand Ah!
Specific or see code, write a note, this statistical method is too pillbox, I also made not very clear, forget later.
Code:
1 //#pragma COMMENT (linker, "/stack:102400000,102400000")2#include <cstdio>3#include <cmath>4#include <iostream>5#include <cstring>6#include <algorithm>7#include <cmath>8#include <map>9#include <Set>Ten#include <stack> One#include <queue> A using namespacestd; - #definell Long Long - #defineUSLL unsigned ll the #defineMZ (Array) memset (array, 0, sizeof (array)) - #defineMinf (array) memset (array, 0x3f, sizeof (array)) - #defineREP (I,n) for (i=0;i< (n); i++) - #definefor (I,x,n) for (i= (x); i<= (n); i++) + #defineRD (x) scanf ("%d", &x) - #defineRD2 (x, y) scanf ("%d%d", &x,&y) + #defineRD3 (x, Y, z) scanf ("%d%d%d", &x,&y,&z) A #defineWN (x) printf ("%d\n", X); at #defineRE freopen ("d.in", "R", stdin) - #defineWE freopen ("1biao.out", "w", stdout) - #defineMP Make_pair - #definePB Push_back - - Const intmaxn=111111; in Const intmod=1e9+7; - to intN; + intA[MAXN]; - the structEdge { * intnext,v; $} e[2*MAXN];Panax Notoginseng inten=0; - intHEAD[MAXN]; the + voidAddintXinty) { Ae[en].v=y; thee[en].next=Head[x]; +head[x]=en++; - } $ $ BOOLU[MAXN]; -ll f[maxn][2];///F[x][j] J=1 means that X is located in the link block has a black point, 0 means no shops of the number of species, including x to connect to the father's Costian all sides - voidDfsintx) { the //printf ("[in%d]", x); - inti;Wuyiu[x]=1; thef[x][0]=1; -f[x][1]=0;///Let 's assume the current point is a white dot. Wu for(I=head[x]; i!=-1; I=E[i].next) { - if(!U[E[I].V]) { About DFS (E[I].V); $f[x][1]= (f[x][1]*f[e[i].v][0] + f[x][0]*f[e[i].v][1])%mod;///In the case of black spots, the case of a son without black spots is first used in the case of a black spot which has been counted, and then a black spot is used in the case where there is no black spot. -f[x][0]=f[x][0]*f[e[i].v][0]%mod;///no black spots, just a son without black spots. - } - } Au[x]=0; + ///The following is the processing of the parent edge of X point the if(a[x]==0) f[x][0]= (f[x][0]+f[x][1])%mod;///X is the white point, if the son has a black spot, cut X's parent is no black spot, so no black spot (f[x][0]) situation to add a black spot (f[x][1]) - Elsef[x][1]=f[x][0];///x dot is the black point, that does not cut the parent side of the case (F[x][1]) only let X's son is not black, cut the father side of the situation (F[x][0]) is also X's son is not black, because x himself black, son and then the black is connected together $ //printf ("[Out%d,flag=%d,re=%i64d,a[x]=%d]\n", x,flag,re,a[x]); the } the the the ll Farm () { - if(n==1)return 1; in mz (u); theDfs0); the returnf[0][1]; About } the the intMain () { the inti; + intx; - RD (n); thememset (head,-1,sizeof(head));Bayien=0; theREP (i,n-1) { thescanf"%d",&x); -Add (i+1, x); -Add (x,i+1); the } the for(i=0; i<n; i++) thescanf"%d",&a[i]); theprintf"%i64d", Farm ()); - return 0; the}
View Code
cf461b Appleman and Tree (DP)