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 ...
随机推荐
- IDEA自学
使用Eclipse很长时间了,想换个IDE用,都说IDEA好用,今天试试 百度了一下IDEA,了解到IDEA社区版免费,上百度,下载个社区版(exe,zip两种)懒人选择exe 手动安装别怕安错,只管 ...
- 在 dotnet core (C#)下的颜色渐变
直接使用等比例抽样算法,连同透明度一起计算. public IList<Color> ShadeColors(Color c1, Color c2, int resultCount) { ...
- js 数组对象深拷贝
js 数组对象深拷贝 结论:对象的拷贝不能采用直接赋值的方式. 背景 踩过的坑如下: formData本来是父组件传过来的,但是我不想直接用,于是我直接赋值给一个formDataCopy的对象. 但是 ...
- 【C/C++】随机数的生成
C/C++:rand()函数 rand()函数的头文件:#include<stdlib.h> 该函数产生的随机数随机性差,速度慢,周期小(0-32767) 用法如下所示: #include ...
- [转载]使用Java操作Mongodb
HelloWorld程序 学习任何程序的第一步,都是编写HelloWorld程序,我们也不例外,看下如何通过Java编写一个HelloWorld的程序. 首先,要通过Java操作Mongodb,必须先 ...
- 【转】linux tar.gz zip 解压缩 压缩命令
http://apps.hi.baidu.com/share/detail/37384818 download ADT link http://dl.google.com/android/ADT-0. ...
- 逆向破解之160个CrackMe —— 013
CrackMe —— 013 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...
- Go_ go mod 命令解决墙的问题
简介 由于众所周知的原因,在下载一些库的时候会下载不了,比如 golang.org/x/... 相关的库.为此,网上出现了很多解决方案. 从 Go1.11 开始,Go 引入了 module,对包进行管 ...
- bilibili弹幕爬取与比对分析
最近受人之托研究了下b站的数据爬取做个小工具,最后朋友说不需要了,本着开源共享的原则,将研究成果与大家分享一波,话不多说直接上干货 需求分析 给定up主uid和用户uid,爬取用户在该up主所有视频中 ...
- CTPN
1. https://zhuanlan.zhihu.com/p/34757009 (原理) 2. https://www.jianshu.com/p/471bdbd0170d (bi-LSTM)