Test instructions: Ask you to have a black-and-white point without a tree so that there is only one black dot in each subtree number of methods.
Problem-solving idea: Tree-shaped DP,DP[I][0/1] Indicates whether there are 1 kinds of black dots in the Unicom diagram of the node I.
Problem Solving Code:
1 //File name:461c.cpp2 //Author:darkdream3 //Created time:2015 March 11 Wednesday 10:53 22 seconds4 5#include <vector>6#include <list>7#include <map>8#include <Set>9#include <deque>Ten#include <stack> One#include <bitset> A#include <algorithm> -#include <functional> -#include <numeric> the#include <utility> -#include <sstream> -#include <iostream> -#include <iomanip> +#include <cstdio> -#include <cmath> +#include <cstdlib> A#include <cstring> at#include <ctime> - #defineLL Long Long - #defineM 1000000007 - using namespacestd; - intcolor[100005]; -LL dp[100005][2]; in intN; -vector<int> mp[100005]; to intDfsintKintLA) + { - if(Color[k]) thedp[k][1] =1; *dp[k][0] = dp[k][1]^1; $ for(inti =0; i < mp[k].size (); i + +)Panax Notoginseng { - if(Mp[k][i] = =LA) the Continue; + inty =Mp[k][i]; A DFS (y,k); theLL T1 = dp[k][0] * (dp[y][0]+dp[y][1]) ; +LL t2 = dp[k][1] * (dp[y][0] + dp[y][1]) + dp[k][0]*dp[y][1]; -dp[k][0] = T1%M; $dp[k][1] = t2%M; $ - } - the } - intMain () {Wuyiscanf"%d",&n); the for(inti =0; i < n-1; i + +) - { Wu intp; -scanf"%d",&p); AboutMp[p].push_back (i+1); $mp[i+1].push_back (p); - } - for(inti =0; i < N;i + +) - { Ascanf"%d",&color[i]); + } theDfs0,0); -printf"%i64d\n", dp[0][1]); $ return 0; the}
View Code
Codeforces 461B Appleman and Tree