题目链接

Fill a DP table such as the following bottom-up:

  • DP[v][0] = the number of ways that the subtree rooted at vertex v has no black vertex.
  • DP[v][1] = the number of ways that the subtree rooted at vertex v has one black vertex.

The recursion pseudo code is folloing:

DFS(v):
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]

The answer is DP[root][1]

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e5+;
ll dp[maxn][];
int head[maxn], num, k, a[maxn];
struct node
{
int to, nextt;
}e[maxn*];
void add(int u, int v) {
e[num].to = v, e[num].nextt = head[u], head[u] = num++;
}
void init() {
num = ;
mem1(head);
}
void dfs(int u, int fa) {
dp[u][] = ;
dp[u][] = ;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
dfs(v, u);
dp[u][] = dp[u][]*dp[v][]%mod;
dp[u][] = (dp[u][]+dp[u][]*dp[v][]%mod)%mod;
dp[u][] = dp[u][]*dp[v][]%mod;
}
if(a[u]) {
dp[u][] = dp[u][];
} else {
dp[u][] = (dp[u][]+dp[u][])%mod;
}
}
int main()
{
int n, x, y;
cin>>n;
init();
for(int i = ; i<n; i++) {
scanf("%d", &x);
add(x, i);
add(i, x);
}
for(int i = ; i<n; i++)
scanf("%d", &a[i]);
dfs(, -);
cout<<dp[][]<<endl;
return ;
}

codeforces 416B. Appleman and Tree 树形dp的更多相关文章

  1. Codeforces 461B. Appleman and Tree[树形DP 方案数]

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

  2. Codeforces 461B Appleman and Tree(木dp)

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

  3. Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】

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

  4. codeforces Round #263(div2) D. Appleman and Tree 树形dp

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

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

  6. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  7. 熟练剖分(tree) 树形DP

    熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...

  8. Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)

    题目:http://codeforces.com/contest/1153/problem/D 题意:给你一棵树,每个节点有一个操作,0代表取子节点中最小的那个值,1代表取子节点中最大的值,叶子节点的 ...

  9. hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)

    题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: ...

随机推荐

  1. CentOS7配置VNC Server

    CentOS7与6有些许变化,感觉有点不太适应. Step 1: 安装tigervnc server 和 X11 fonts: [root@mdrill ~]# yum install tigervn ...

  2. OC中文件的操作

    OC中文件操作,在之前的文章中,已经接触到了文件的创建了,但是那不是很具体和详细,这篇文章我们就来仔细看一下OC中是如何操作文件的: 第一.首先来看一下本身NSString类给我们提供了哪些可以操作文 ...

  3. su 切换用户

    大部分Linux发行版的默认账户是普通账户,而更改系统文件或者执行某些命令,需要root身份才能进行,这就需要从当前用户切换到root用户,Linux中切换用户的命令是su或su - 前者只是切换ro ...

  4. 在MFC主对话框OnInitDialog()中弹出对话框

    BOOL CXXXDlg::OnInitDialog(){ CDialogEx::OnInitDialog(); SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FA ...

  5. Java提高学习之Object(3)

    终止 问: finalize()方法是用来做什么的? 答: finalize()方法可以被子类对象所覆盖,然后作为一个终结者,当GC被调用的时候完成最后的清理工作(例如释放系统资源之类).这就是终止. ...

  6. php程序员的弱点

    今天在在知乎上看到一个问题,题目是<看了laravel的php框架怎么感觉很不靠谱?>,我最近也在想学习一下laravel,laravel听说是受到很大ruby on rails的影响. ...

  7. WEB开发之如何改善PHP开发方式

    改善PHP开发方式一般可以分为以下几种实现方式: 1.组织和样式     找出一种适合你的组织方法和编码样式,并且一直坚持下去,这样的话,你的代码的组织和布局会变得十分有条理.我们不应该轻视代码的组织 ...

  8. Confluent

    Confluent介绍(一)   最开始接触confluent是通过这篇博客,How to Build a Scalable ETL Pipeline with Kafka Connect,对于做大数 ...

  9. 微软 Office 2010 SP2 正式版下载大全(含简中)

    7月24日消息,微软正式为 Office 2010 和 SharePoint 2010 系列产品发布 SP 2服务包,带来重要更新和修复.除了提供产品补丁,SP2服务包还将提升产品的稳定性.性能以及安 ...

  10. [Drools]JAVA规则引擎 -- Drools

    Drools是一个基于Java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效. 本文所使用的de ...