Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数
解法:树形DP问题。定义:
dp[u][0]表示以u为根的子树对父亲的贡献为0
dp[u][1]表示以u为根的子树对父亲的贡献为1
现在假设u为白色,它的子树有x,y,z,那么有
dp[u][1]+=dp[x][1]*dp[y][0]*dp[z][0]+dp[x][0]*dp[y][1]*dp[z][0]+dp[x][0]*dp[y][0]*dp[z][1]
dp[u][0]+=dp[x][0]*dp[y][0]*dp[z][0]
然后判断u的颜色,假设u的父亲为p
1.为黑色,不切断边(u,p),那么dp[u][1]=dp[u][0],切断(u,p),那么dp[u][0]不变
2.为白色,如果切断(u,p),dp[u][0]还要加上dp[u][1]
代码:
#include <iostream>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#define Mod 1000000007
#define lll __int64
using namespace std;
#define N 100007 vector<int> G[N];
lll dp[N][];
int col[N]; void dfs(int u,int fa)
{
dp[u][] = 1LL;
dp[u][] = 0LL;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(v == fa) continue;
dfs(v,u);
dp[u][] = (dp[u][]%Mod*dp[v][]%Mod);
dp[u][] = (dp[u][]%Mod + dp[u][]*dp[v][]%Mod)%Mod;
dp[u][] = dp[u][]%Mod*dp[v][]%Mod;
}
if(col[u] == ) dp[u][] = dp[u][];
else dp[u][] = (dp[u][]+dp[u][])%Mod;
} int main()
{
int n,x,i;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<=n;i++)
G[i].clear();
for(i=;i<n-;i++)
{
scanf("%d",&x);
G[i+].push_back(x);
G[x].push_back(i+);
}
for(i=;i<n;i++)
scanf("%d",&col[i]);
dfs(,-);
printf("%I64d\n",dp[][]%Mod);
}
return ;
}
Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】的更多相关文章
- 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 ...
- 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman
题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...
- Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新
C. Appleman and a Sheet of Paper Appleman has a very big sheet of paper. This sheet has a form of ...
- Codeforces Round #263 (Div. 2) A. Appleman and Easy Task【地图型搜索/判断一个点四周‘o’的个数的奇偶】
A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces Round #196 (Div. 2) D. Book of Evil 树形dp
题目链接: http://codeforces.com/problemset/problem/337/D D. Book of Evil time limit per test2 secondsmem ...
- Codeforces Round #382 (Div. 2) 继续python作死 含树形DP
A - Ostap and Grasshopper zz题能不能跳到 每次只能跳K步 不能跳到# 问能不能T-G 随便跳跳就可以了 第一次居然跳越界0.0 傻子哦 WA1 n,k = map ...
- Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP
C. Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some g ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- Codeforces Round #263 (Div. 2)
吐槽:一辈子要在DIV 2混了. A,B,C都是简单题,看AC人数就知道了. A:如果我们定义数组为N*N的话就不用考虑边界了 #include<iostream> #include &l ...
随机推荐
- 在博客中使用MathJax写数学公式
前言 总结一些在博客园使用MathJax写数学公式的经验. 博客园 设置使用数学公式 进入你的博客:管理 > 选项 里面有个启用数学公式支持,选上后保存. 这时,你就可以在你的博客里写数学公式了 ...
- PHP系列之一traits的应用
Traits 在PHP中实现在方法的重复使用:Traits与Class相似,但是它能够在Class中使用自己的方法而不用继承: Traits在Class中优先于原Class中的方法,引用PHP Doc ...
- redis配置注意事项
最近在看redis方面的官方文档,redis-server的相关配置建议如下: 1.vm.overcommit_memory = 1 2.禁用linux内核特性transparent huge pag ...
- 我所了解的WEB开发(3) - 彩虹的颜色
据说彩虹有七彩颜色,从外至内分别为:红.橙.黄.绿.青.蓝.紫.这些我倒是没有验证过,但是学生时代就不止一次色盲检测,还是让我足够确信对颜色的分辨应该和大多数人相似的. 还听说大多数哺乳动物是色盲.如 ...
- [TypeScript] JSON对象转TypeScript对象范例
[TypeScript] JSON对象转TypeScript对象范例 Playground http://tinyurl.com/nv4x9ak Samples class DataTable { p ...
- seajs学习笔记
seajs配置 seajs.config({ //别名配置 alias:{ 'es5-safe':'gallery/es5-safe/0.9.3/es5-safe', 'jquery':'jquery ...
- Fix Internet Explorer Crashes with SharePoint 2013 Online Presence Indicators
IE中,只要是鼠标浮动到人名字上面的状态的时候,这个状态是与Lync相连接的,IE就会出现停止工作. 以下是解决方法. Until the other day when I figured this ...
- android 圆角按钮和按钮颜色
1. android 设置圆角按钮后,按下按钮后,还能改变按钮的颜色 <?xml version="1.0" encoding="UTF-8"?> ...
- 【读书笔记】iOS-GCD-block-后台运行
当一个app按home键退出的时候,只有最多5秒的时间做一些保存或清理资源的工作.但是调用beginBackgroundTaskWithExpirationHandler方法,可以最多有10分时间在后 ...
- 基础学习day12--多线程一线程之间的通信和常用方法
一.线程之间的通信 1.1.线程之间的通信方法 多个线程在处理统一资源,但是任务却不同,这时候就需要线程间通信. 等待/唤醒机制涉及的方法: 1. wait():让线程处于冻结状态,被wa ...