CodeForces 416 B Appleman and Tree DP
题解:
定义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的更多相关文章
- Codeforces 486D Valid Sets:Tree dp【n遍O(n)的dp】
题目链接:http://codeforces.com/problemset/problem/486/D 题意: 给你一棵树,n个节点,每个节点的点权为a[i]. 问你有多少个连通子图,使得子图中的ma ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 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 ...
- CF461B Appleman and Tree (树DP)
CF462D Codeforces Round #263 (Div. 2) D Codeforces Round #263 (Div. 1) B B. Appleman and Tree time l ...
- 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 ...
- CF461B Appleman and Tree
CF461B Appleman and Tree 传送门 一道比较容易的树形DP. 考虑用\(dp[i][1]\)代表将\(i\)分配给\(i\)的子树内黑点的方案数,\(dp[i][0]\)代表将\ ...
- 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 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- 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 ...
随机推荐
- django+uwsgi+nginx 部署生产环境
一.Uwsgi安装 python3 -m pip install uwsgi cp /usr/local/python3/bin/uwsgi /usr/bin/ 测试 在django项目主目录下cre ...
- 通过jmeter发送webservice接口请求
1.webservice接口地址:http://ip:port/...?wsdl 2.接口数据类型:<cuxGmiChukuRmaTrxV><salesrepId xmlns:xsi ...
- print('', end='')
print函数的end参数,从python3才开始支持,所以如果要使用这种模式,需要对应使用python3
- Viper-Go一站式配置管理工具
什么是Viper Viper是一个方便Go语言应用程序处理配置信息的库.它可以处理多种格式的配置.它支持的特性: 设置默认值 从JSON.TOML.YAML.HCL和Java properties文件 ...
- Alfred Workflow
实用的 Alfred Workflow Alfred Workflow 介绍 alfred-pkgman-workflow 快速从各个软件仓库(maven, gradle 等等)中查找需要的软件包 A ...
- 5月29日 Java性能调优指南 读后感
并行垃圾收集器 串行垃圾收集器 并发标记清除(CMS)垃圾收集器 Garbage First(G1)垃圾收集器 没有深入的学习G1的原理,只是看了大概的思想; SA工具:待学习
- (十八)c#Winform自定义控件-提示框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- 100天搞定机器学习|Day33-34 随机森林
前情回顾 机器学习100天|Day1数据预处理 100天搞定机器学习|Day2简单线性回归分析 100天搞定机器学习|Day3多元线性回归 100天搞定机器学习|Day4-6 逻辑回归 100天搞定机 ...
- python案例:实现一个函数版的名片管理系统
本案例使用了自定义函数以及对字符串的常见操作.判断语句和循环语句等知识. 要求 必须使用自定义函数,完成对程序的模块化. 名片信息至少包括:姓名.电话.住址. 必须完成的功能:增.删.改.查.退出. ...
- Django-channels 实现WebSocket实例
引入 先安装三个模块 pip install channels pip install channels_redis pip install pywin32 创建一个Django项目和一个app 项目 ...