博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 461B. Appleman and Tree[树形DP 方案数]
阅读量:6893 次
发布时间:2019-06-27

本文共 2554 字,大约阅读时间需要 8 分钟。

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

题意:分成若干个连通块,每个只有一个黑色节点,求方案数

f[i][0/1]表示以i为根的子树i是否在有黑色节点的连通块中的方案数
f[u][1]=(f[u][1]*(f[v][0]+f[v][1])+f[u][0]*f[v][1])%MOD; v是0 u跟他相连,v是1 不相连;u是0时要跟v是1相连f[u][0]=f[u][0]*(f[v][0]+f[v][1])%MOD;同理
#include
#include
#include
#include
using namespace std;typedef long long ll;const int N=1e5+5,MOD=1e9+7;inline int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){
if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();} return x*f;}struct edge{ int v,ne;}e[N<<1];int cnt=0,h[N],w[N];inline void ins(int u,int v){ cnt++; e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt; cnt++; e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;}int n;ll f[N][2];void dp(int u,int fa){ if(w[u]) f[u][1]=1; else f[u][0]=1; for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; if(v==fa) continue; dp(v,u); f[u][1]=(f[u][1]*(f[v][0]+f[v][1])+f[u][0]*f[v][1])%MOD; f[u][0]=f[u][0]*(f[v][0]+f[v][1])%MOD; }}int main(){ n=read(); for(int i=1;i<=n-1;i++) ins(read(),i); for(int i=0;i

 

转载地址:http://kykdl.baihongyu.com/

你可能感兴趣的文章
分布式设计与开发(二)------几种必须了解的分布式算法
查看>>
JS中typeof与instanceof的区别
查看>>
PHP中str_replace函数使用小结
查看>>
Oracle用户、角色、授权和表空间
查看>>
linux下修改SWAP空间大小
查看>>
我的友情链接
查看>>
mac 安装python3.4、django
查看>>
你想建设一个能承受500万PV/每天的网站吗?如果计算呢?
查看>>
iOS8完美越狱在路上了吗?
查看>>
编写更好的jQuery代码的建议
查看>>
linux 入门基础知识(一)
查看>>
项目质量管理
查看>>
将linux英文系统变成中文系统
查看>>
CXDVA视频组件
查看>>
给自己降降级你会发现一片广阔的天空
查看>>
Linux Apache 编译安装;
查看>>
python2.7x Django mysql在windows Ubuntu下的环境搭建
查看>>
33 一生能有几个?
查看>>
我的友情链接
查看>>
一键安装lnmp报错 pycurl.so: undefined symbol: CRYPTO_set_locking_callback
查看>>