Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
The 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 p0, p1, ..., pn - 2 (0 ≤ pi ≤ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex iis colored black. Otherwise, vertex i is colored white.
Output a single integer — the number of ways to split the tree modulo 1000000007 (109 + 7).
3 0 0 0 1 1
2
6 0 1 1 0 4 1 1 0 0 1 0
1
10 0 1 2 1 4 4 4 0 8 0 0 0 1 0 1 1 0 0 1
27
题目大意:
给出N个点的一棵树,每个节点都有一个颜色(0/1),我们现在可以将这棵树割分成若干个联通块,但是要求每个联通块中必须有且只有一个颜色1的点,问有多少种割分方式。。
思路:
设定Dp【i】【2】:
①Dp【i】【0】表示以点i为根的子树形成割分情况的方案数,且点i连通部分没有颜色1的点。
②Dp【i】【0】表示以点i为根的子树形成割分情况的方案数,且点i连通部分有颜色1的点。
问题关键点是在于兄弟节点之间的并 ,那么有:
①Dp【i】【1】=Dp【i】【1】*(Dp【v】【0】+Dp【v】【1】)+Dp【i】【0】*Dp【v】【1】;
②Dp【i】【0】=Dp【i】【0】*(Dp【v】【0】+Dp【v】【1】);
过程维护一下即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
using namespace std;
int n;
vector<int>mp[150000];
int color[150000];
__int64 dp[150000][2];
__int64 mod=1e9+7;
void Dfs(int u,int from)
{
if(color[u]==1)dp[u][1]=1;
else dp[u][0]=1;
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(v==from)continue;
Dfs(v,u);
dp[u][1]=((dp[u][1]*(((dp[v][0]+dp[v][1]))%mod))%mod+(dp[u][0]*dp[v][1])%mod)%mod;
dp[u][0]=(dp[u][0]*((dp[v][0]+dp[v][1]))%mod)%mod;
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)mp[i].clear();
for(int i=2;i<=n;i++)
{
int x;scanf("%d",&x);
x++;
mp[x].push_back(i);
mp[i].push_back(x);
}
for(int i=1;i<=n;i++)scanf("%d",&color[i]);
Dfs(1,-1);
printf("%I64d\n",dp[1][1]%mod);
}
}