Appleman and Tree

题解:

定义dp[u][1] 为以u的子树范围内,u这个点已经和某个黑点相连的方案数。

dp[u][0] 为在u的子树范围内, u这个点还未和某个黑点相连的方案数。

转移方程:

如果 u为黑点, dp[u][0] = 0, dp[u][1] = 1, 然后考虑从下面转移过来, dp[u][1] *= dp[v][0] + dp[v][1].

也就是说, 如果 v 点为黑,则切断这个边, 如果v点为白,则不切断, 即对于v来说,每个情况,切边的情况也只有一种, 不同的v的方案数相互独立。

如果 u为白点, dp[u][0] = 1, dp[u][1] = 1, 考虑转移 dp[u][0] *= dp[v][0] + dp[v][1], dp[u][1] += dp[v][1] * (除v以外的子树(dp[z][1] + dp[z][0])乘积 。

对于dp[u][0]来说,和上面的道理一样。

对于dp[u][1]来说,枚举和下面哪一个黑点连边,然后这个点对于其他的v来说就相当于一个黑点,转移的方程就是和 u 是黑点的道理一样了。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 1e5 + ;
vector<int> vc[N];
int dp[N][];
int pre[N], suf[N];
int a[N];
void dfs(int u){
if(a[u] == ) {
dp[u][] = ; dp[u][] = ;
for(int v : vc[u]){
dfs(v);
dp[u][] = (1ll * dp[u][] * (dp[v][] + dp[v][])) % mod;
}
}
else{
dp[u][] = ; dp[u][] = ;
int t = vc[u].size();
for(int v : vc[u]){
dfs(v);
}
for(int i = ; i < t; ++i){
int v = vc[u][i];
suf[i+] = pre[i+] = (dp[v][]+dp[v][]) % mod;
}
pre[] = ; suf[t+] = ;
for(int i = ; i <= t; ++i)
pre[i] = 1ll * pre[i] * pre[i-] % mod;
for(int i = t; i >= ; --i)
suf[i] = 1ll * suf[i] * suf[i+] % mod;
dp[u][] = pre[t];
for(int i = ; i <= t; ++i){
int v = vc[u][i-];
dp[u][] = (dp[u][] + 1ll * dp[v][] * (1ll * pre[i-] * suf[i+]%mod))% mod;
}
}
}
int main(){
int n, o;
scanf("%d", &n);
for(int i = ; i <= n; ++i){
scanf("%d", &o);
vc[o+].pb(i);
}
for(int i = ; i <= n; ++i)
scanf("%d", &a[i]);
dfs();
printf("%d\n", dp[][]);
return ;
}

CodeForces 416 B Appleman and Tree DP的更多相关文章

  1. Codeforces 486D Valid Sets:Tree dp【n遍O(n)的dp】

    题目链接:http://codeforces.com/problemset/problem/486/D 题意: 给你一棵树,n个节点,每个节点的点权为a[i]. 问你有多少个连通子图,使得子图中的ma ...

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

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

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

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

  4. Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

    题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...

  5. CF461B Appleman and Tree (树DP)

    CF462D Codeforces Round #263 (Div. 2) D Codeforces Round #263 (Div. 1) B B. Appleman and Tree time l ...

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

  7. CF461B Appleman and Tree

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

  8. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  9. 96. Unique Binary Search Trees (Tree; DP)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. 【Android】java.lang.SecurityException: getDeviceId: Neither user 10065 nor current process has android.permission.READ_PHONE_STATE

    RT, 异常信息如下: java.lang.SecurityException: getDeviceId: Neither user 10065 nor current process has and ...

  2. JAVA-Spring AOP基础 - 代理设计模式

    利用IOC DI实现软件分层,虽然解决了耦合问题,但是很多地方仍然存在非该层应该实现的功能,造成了无法“高内聚”的现象,同时存在大量重复的代码,开发效率低下. @Service public clas ...

  3. ansible批量管理服务 下

    1 ansible-playbook 任务剧本 1.1 剧本文件概念 (1)playbook可以将多个批量操作模块功能整合,完成一件事情.(2)简化运维工作复杂度(3)playbook通过yaml语法 ...

  4. JDK1.8源码分析01之学习建议(可以延伸其他源码学习)

    序言:目前有个计划就是准备看一下源码,来提升自己的技术实力.同时现在好多面试官都喜欢问源码,问你是否读过JDK源码等等? 针对如何阅读源码,也请教了我的老师.下面就先来看看老师的回答,也许会有帮助呢. ...

  5. 一份新的lilypond谱子,能设置页边距和设置换页符了

    给学生做的一份乐谱,这回能设置页边距了,以及设置换页符了. 顺带能设置一些代码片段(snippet),可以用热键代替使用 设置页边距的snippet: \paper { %双引号里面填页面大小 #(s ...

  6. H5中的history方法Api介绍

    最近公司在做一个微信公众号,看了项目源码,看到项目中用到了history的Api来进行控制浏览器的历史记录及前进/后退键: 下面来跟大家一起来捋捋history的Api方法和使用: history.p ...

  7. Python3基本数据类型之列表

    1.初识列表 列表(List)是Python3中的"容器型"数据类型. 列表通过中括号把一堆数据括起来的方式形成,列表的长度不限. 列表里面的元素可以是不同的数据类型,但是一般是相 ...

  8. Java网络编程 -- 网络协议

    OSI网络七层协议 为使不同计算机厂家的计算机能够互相通信,以便在更大的范围内建立计算机网络,有必要建立一个国际范围的网络体系结构标准.OSI网络七层协议就是在这个基础上制定出来的,其从最底层开始依次 ...

  9. 约会安排 HDU - 4553(线段树区间查询,区间修改,区间合并)

    题目: 寒假来了,又到了小明和女神们约会的季节.  小明虽为屌丝级码农,但非常活跃,女神们常常在小明网上的大段发言后热情回复“呵呵”,所以,小明的最爱就是和女神们约会.与此同时,也有很多基友找他开黑, ...

  10. 从头开始制作OJ-在线IDE的搭建

    大家好,我是Fred913. 之前,我看过各种OJ(OpenJudge) 但是,还是没有自己做的好. 所以,我就来写了这篇教程. 环境 这次,我打算使用这些:PHP 5.6 Nginx/Apache ...