Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree
题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量。保证每一个联通分量有且仅有1个黑色节点。问有多少种切割方法。
解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的切割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
const int maxn = 1e5+5;
int N, v[maxn];
ll dp[maxn][2];
vector<int> g[maxn];
void init () {
memset(dp, 0, sizeof(dp));
int x;
scanf("%d", &N);
for (int i = 1; i < N; i++) {
scanf("%d", &x);
g[x].push_back(i);
}
for (int i = 0; i < N; i++)
scanf("%d", &v[i]);
}
ll pow_mod (ll x, int n, ll mod) {
ll ret = 1;
while (n) {
if (n&1)
ret = ret * x % mod;
x = x * x % mod;
n >>= 1;
}
return ret;
}
ll inv (ll x) {
if (x == 0)
return 1;
return pow_mod(x, MOD-2, MOD);
}
void solve (int u) {
if (g[u].size() == 0) {
dp[u][v[u]] = 1;
return;
}
for (int i = 0; i < g[u].size(); i++)
solve(g[u][i]);
if (v[u]) {
dp[u][0] = 0;
ll& ans = dp[u][1];
ans = 1;
for (int i = 0; i < g[u].size(); i++) {
int k = g[u][i];
ans = ans * (dp[k][1] + dp[k][0]) % MOD;
}
} else {
dp[u][1] = 0;
ll& ans = dp[u][0];
ans = 1;
for (int i = 0; i < g[u].size(); i++) {
int k = g[u][i];
ans = ans * (dp[k][0] + dp[k][1]) % MOD;
}
for (int i = 0; i < g[u].size(); i++) {
int k = g[u][i];
dp[u][1] += ans * inv(dp[k][0] + dp[k][1]) % MOD * dp[k][1] % MOD;
dp[u][1] %= MOD;
}
}
//printf("%d %d %d\n", u, dp[u][0], dp[u][1]);
}
int main () {
init();
solve(0);
printf("%lld\n", dp[0][1] % MOD);
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
Codeforces 461B Appleman and Tree(木dp)的更多相关文章
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 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 ...
- Codeforces 461B Appleman and Tree:Tree dp
题目链接:http://codeforces.com/problemset/problem/461/B 题意: 给你一棵树(编号从0到n-1,0为根节点),每个节点有黑白两种颜色,其中黑色节点有k+1 ...
- Codeforces 461B - Appleman and Tree 树状DP
一棵树上有K个黑色节点,剩余节点都为白色,将其划分成K个子树,使得每棵树上都仅仅有1个黑色节点,共同拥有多少种划分方案. 个人感觉这题比較难. 如果dp(i,0..1)代表的是以i为根节点的子树种有0 ...
- Codeforces 461B Appleman and Tree
http://codeforces.com/problemset/problem/461/B 思路:dp,dp[i][0]代表这个联通块没有黑点的方案数,dp[i][1]代表有一个黑点的方案数 转移: ...
- codeforces 416B. Appleman and Tree 树形dp
题目链接 Fill a DP table such as the following bottom-up: DP[v][0] = the number of ways that the subtree ...
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...
- codeforces Round #263(div2) D. Appleman and Tree 树形dp
题意: 给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法. 题解: 树形dp dp[x][0]表示是以x为根的树 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
随机推荐
- 在 Ubuntu 12.04 上安装 GitLab7.x
安装环境: 操作系统: Ubuntu 12.4 LTS 英文 数据库: postgresql webserver: nginx 能够说到7.x的时候,GitLab的文档已经相当完好 ...
- IOC/DI的基本思想
IOC/DI的基本思想 1.把程序之间的依赖关系去掉 2.把程序对象设置到IOC/DI容器的配置中作为Bean 3.由IOC/D.容器来管理Bean的创建和实例化 4.由IOC/DI容器来把Bean之 ...
- iOS_38_手势
Pan平移手势 终于效果图: Swipe轻扫手势 LongPress长按手势 Pinch和Rotation手势 捏合(缩放)和旋转 终于效果图: 涂鸦 终于效果图: 事件分3大类:触摸.加速计.远程遥 ...
- UVA 11249 - Game(游戏)
UVA 11249 - Game 题目链接 题意:两堆石头.a和b.每次能取一堆随意数量,或者两堆同一时候取.可是绝对值差不能超过k,最后不能取的人输,问先手能否赢 思路:先如果(a, b)石子,a是 ...
- 一天JavaScript示例-判定web页面的区域
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 使用IronPython给.Net程序
使用IronPython给.Net程序加点料 开发的时候,经常被策划频繁变动的方案而苦恼.这时候就想要加入点动态语言来辅助一下. 在考虑用动态语言之前也曾想过使用动态加载dll的方式,实现基础接口来调 ...
- java安全性语言
java通过所谓的沙箱安全模型保证了其安全性,以下我们就来看看java提供的安全沙箱机制. 组成沙箱的基本组件例如以下: 1.类装载器结构: 2.class文件检验器: 3.内置于java虚拟机(及语 ...
- 辛星整理3linux笔记,免费下载点,我希望对你有所帮助
忙乱,这是我第一次看李指出老师的视频时,,这本书是关于116页面,在csdn下载对:点我下载 ,假设左边的地址崩溃了,也能够在浏览器中输入例如以下地址然后下载:http://download.csdn ...
- poj 3975&&hdu 1850 (nim)
//赢得了上风 //从n几年移除堆叠一堆石头,有多少可取的石头堆 # include <stdio.h> # include <string.h> # include < ...
- java 面试题汇总(未完成)
版权声明:本文博客原创文章,博客,未经同意,不得转载.