【Codeforces Round 263 (Div 2)D】【树形DP】Appleman and Tree 树上割k个黑点为k块的方案数

Appleman and Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Input

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 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 (109 + 7).

Examples
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


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5 + 10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n;
int x;
vector<int>a[N];
int c[N];
LL save[N];
LL leave[N];
LL mul(LL x, int p)
{
	LL y = 1;
	while (p)
	{
		if (p & 1)y = y*x%Z;
		x = x*x%Z;
		p >>= 1;
	}
	return y;
}
void dfs(int x, int fa)
{
	save[x] = 0;
	leave[x] = 1;
	for (int i = a[x].size() - 1; ~i; --i)
	{
		int y = a[x][i];
		if (y == fa)continue;
		dfs(y, x);
		leave[x] = leave[x] * (leave[y] + save[y]) % Z;
	}
	for (int i = a[x].size() - 1; ~i; --i)
	{
		int y = a[x][i];
		if (y == fa)continue;
		LL tmp= leave[x] * mul(leave[y] + save[y], Z - 2) %Z *save[y] %Z;
		save[x] = (save[x] + tmp) % Z;
	}
	if (c[x])
	{
		save[x] = leave[x];
		leave[x] = 0;
	}
}
int main()
{
	while (~scanf("%d", &n))
	{
		for (int i = 1; i < n; ++i)
		{
			scanf("%d", &x);
			a[x].push_back(i);
			a[i].push_back(x);
		}
		for (int i = 0; i < n; ++i)scanf("%d", &c[i]);
		dfs(0, -1);
		printf("%lld\n", save[0]);
	}
	return 0;
}
/*
【trick&&吐槽】
这道题我的方法是需要做逆元处理的。
然而实际却并不需要。题解给了一种更高效的做法
DP[v][0] = 1
DP[v][1] = 0
foreach u : the children of vertex v
	DFS(u)
	DP[v][1] *= DP[u][0]			//表示已经有黑点的话,这个子树可以有黑点(这边就割)也可以无黑点(这边就不割)
	DP[v][1] += DP[v][0]*DP[u][1]	//表示有黑点的话,可以是之前没有黑点的方案数*这一步有黑点的方案数
	DP[v][0] *= DP[u][0]			//表示没有黑点的话,这个子树可以有黑点(这边就割)也可以无黑点(这边就不割)

	if x[v] == 1:
		DP[v][1] = DP[v][0]
	else:
		DP[v][0] += DP[v][1]

1表示有黑点,0表示无黑点。

【题意】
给你一棵树,有n(1e5)个点,树上每个点不是白点就是黑点(至少一个为黑点)
让你在树中取一些边,使得拿去这些边后,树形成了若干棵子树,每棵子树都恰好有一个黑点

【类型】
树形DP

【分析】
这道题我们模拟样例,得到了解决方法。
我们可以对于每个节点,考虑两种状态
save[x]表示我们保留了以x为根的子树中的某个黑点与x的联系的方案数
leave[x]表示我们摒弃了所有以x为根的子树中的黑点的联系的方案数
all[x]表示save[x]+leave[x]

显然:
我们先不考虑[x]自己这个节点是黑或白
leave[x]=x所有子节点的all[x]的乘积,意思是,原来保留,这条边必然切,原来不保留,这条边必然不切。
save[x]=枚举y,∑leave[x]/all[y]*save[y],
意思是,原来保留,这条边必然不切,其他边保留的必然切,不保留的必然不切。

然而,如果这个点是黑,那么save[x]=leave[x],leave[x]=0

【时间复杂度&&优化】
O(n)

【数据】
留=枚举一棵子树的留,连乘以其它子树的(留+割)
割=连乘(所有子树的留+割)

模拟第三组数据——
子树3、5、6、9:留1割0
子树2:留1割1
子树4:留2割1
子树1:留1*3+2*2=7割2*3=6
子树8:留1割1
子树0:留7*2+1*13=27

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值