CF461B Appleman and Tree (树DP)

时间:2022-09-25 18:30:23

CF462D

Codeforces Round #263 (Div. 2) D

Codeforces Round #263 (Div. 1) B

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

Sample test(s)
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

题意:有n个结点的树,结点编号0~n-1,0为根,分别给出1~n-1的父亲,再给出0~n-1各个结点的颜色(0为白,1为黑),要将其中一些边切掉,使每个联通块有且只有1个黑点,求切法种类数。

题解:树形DP。

从根DFS,f[x][0]表示对{x点和它的子树、x点连接父亲结点的边}这一整坨,有多少种方案使得x这个联通块没黑点(x是黑点的时候这个也不为0,是把x的父边切掉的种类数)

f[x][1]是这个联通块有黑点的种类数。

太难了!怪不得大家都掉分飞起,虽然题解的代码看起来很短,我根本想不出来啊看了半天还是不懂啊!

具体还是看代码吧,写了点注释,这个统计方法太碉了,我也弄得不是很清楚,算了日后再说。

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back const int maxn=;
const int MOD=1e9+; int n;
int a[maxn]; struct Edge {
int next,v;
} e[*maxn];
int en=;
int head[maxn]; void add(int x,int y) {
e[en].v=y;
e[en].next=head[x];
head[x]=en++;
} bool u[maxn];
ll f[maxn][];///f[x][j] j=1表示x所在联通块有黑点,0表示无黑店 的种类数,包括x连接父亲的边和子树所有的边
void dfs(int x){
//printf("[in %d]",x);
int i;
u[x]=;
f[x][]=;
f[x][]=;///先假设当前点是个白点
for(i=head[x]; i!=-; i=e[i].next) {
if(!u[e[i].v]) {
dfs(e[i].v);
f[x][]=(f[x][]*f[e[i].v][] + f[x][]*f[e[i].v][])%MOD;///有黑点的情况,先用已经统计的有黑点的情况乘一发儿子没黑点的情况,然后用已经统计的没黑点的情况乘一发儿子有黑点的情况
f[x][]=f[x][]*f[e[i].v][]%MOD;///没黑点的情况直接乘儿子没黑点的情况
}
}
u[x]=;
///下面是对x点的父边的处理
if(a[x]==)f[x][]=(f[x][]+f[x][])%MOD;///x是白点,儿子要是有黑点,砍了x的父边就是没黑点,所以没黑点(f[x][0])的情况要加上有黑点的情况(f[x][1])
else f[x][]=f[x][];///x点是黑点,那不砍父边的情况(f[x][1])只有让x的儿子都不黑,砍父边的情况(f[x][0])也是x的儿子都不黑,因为x自己黑嘛,儿子再黑就连到一起了
//printf("[out %d,flag=%d,re=%I64d,a[x]=%d]\n",x,flag,re,a[x]);
} ll farm() {
if(n==)return ;
mz(u);
dfs();
return f[][];
} int main() {
int i;
int x;
RD(n);
memset(head,-,sizeof(head));
en=;
REP(i,n-) {
scanf("%d",&x);
add(i+,x);
add(x,i+);
}
for(i=; i<n; i++)
scanf("%d",&a[i]);
printf("%I64d",farm());
return ;
}

CF461B Appleman and Tree (树DP)的更多相关文章

  1. CF461B Appleman and Tree

    CF461B Appleman and Tree 传送门 一道比较容易的树形DP. 考虑用\(dp[i][1]\)代表将\(i\)分配给\(i\)的子树内黑点的方案数,\(dp[i][0]\)代表将\ ...

  2. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

  3. Codeforces 461B Appleman and Tree&lpar;木dp&rpar;

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  4. Codeforces 461B&period; Appleman and Tree&lbrack;树形DP 方案数&rsqb;

    B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. &lbrack;CF1060F&rsqb;Shrinking Tree&lbrack;树dp&plus;组合计数&rsqb;

    题意 你有一棵 \(n\) 个点的树,每次会随机选择树上的一条边,将两个端点 \(u,v\) 合并,新编号随机为 \(u,v\).问最后保留的编号分别为 \(1\) 到 \(n\) 的概率. \(n\ ...

  6. Codeforces 461B - Appleman and Tree 树状DP

    一棵树上有K个黑色节点,剩余节点都为白色,将其划分成K个子树,使得每棵树上都仅仅有1个黑色节点,共同拥有多少种划分方案. 个人感觉这题比較难. 如果dp(i,0..1)代表的是以i为根节点的子树种有0 ...

  7. Codeforces Round &num;263 Div&period;1 B Appleman and Tree --树形DP【转】

    题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...

  8. codeforces Round &num;263&lpar;div2&rpar; D&period; Appleman and Tree 树形dp

    题意: 给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法. 题解: 树形dp dp[x][0]表示是以x为根的树 ...

  9. Codeforces 161 D&period; Distance in Tree &lpar;树dp&rpar;

    题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点 ...

随机推荐

  1. EQueue文件持久化消息关键点设计思路

    要持久化的关键数据有三种 消息: 队列,队列中存放的是消息索引信息,即消息在文件中的物理位置(messageOffset)和在队列中的逻辑位置(queueOffset)的映射信息: 队列消费进度,表示 ...

  2. C&plus;&plus;STL - 模板的其他特性

    之前已经总结过函数模板和类模板了,对于模板还有一些其他的特性,这篇主要介绍这些特性.主要都是一些特殊情况. 模板的其他特性 1.缺省参数 (1)类模板的模板参数可以带有缺省值,实例化该模板时,如果提供 ...

  3. Loading加载小插件&comma;用户可以选择html格式或canvas格式,自定义loading图片,canvas色彩搭配可根据个人喜好

    ;(function($) { $.Loading = function(options) { //暴漏插件默认值 $.Loading.defaults = { speed: 200, //弹出层淡出 ...

  4. Git进行代码管理的心得

    git从网上下载安装后 直接可在开始菜单中找到 打开可以看到一个类似命令窗口的东西 用代码进行用户名和邮箱的设置 虽然不知道为什么但是教程叫我这么做 (ˇˍˇ) 然后似乎就可以用了 进入文件夹 输入对 ...

  5. 跨域文件 clientaccesspolicy&period;xml

    <?xml version="1.0" encoding="utf-8" ?> <access-policy> <cross-do ...

  6. Shared Assembilies and Strongly Named Assemblies

    the .NET Framework has in place to deal with versioning problems. Two Kinds of Assemblies, Two Kinds ...

  7. 显示Mac电脑下的隐藏文件

    1. 在应用程序里打开终端, cd 你的文件夹名 ls -a 即可显示该文件夹下的所有隐藏文件 2. 如果你想打开整个系统的隐藏文件可以在终端下输入以下命令 defaults write com.ap ...

  8. asp&period;net页面之间的跳转

    调用Request.CurrentExecutionFilePath方法返回到当前页面 站点中常常要跳转页面,调用Request.CurrentExecutionFilePath方法能够获取当前页面的 ...

  9. Asp&period;net mvc 知多少(十)

    本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...

  10. 《前端之路》之 Cookie &amp&semi;&amp&semi; localStorage &amp&semi;&amp&semi; Session Storage 缓存相关

    08: Cookie && localStorage && Session Storage 缓存相关 客户端.前端 存储 一. 起 因 首先解释下为什么想来写这个关于前 ...