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 ...
随机推荐
- js 创建对象 经典模式
1. 概述 通过构造函数创建对象, 有时忘记了写new, 这时函数就会返回undefined 可以创建一个函数createXXX, 在内部封装new. function Student(props){ ...
- double 类型转化为Integer类型 ---DecimalFormat
假设x是你要转换的double类型变量: 不进行四舍五入操作: (int)x 进行四舍五入操作: Integer.parseInt(new java.text.DecimalFormat(" ...
- CentOS常用指令
创建文件: 如touch a.txt 创建文件夹: mkdir -p 文件夹名,当文件夹不存在时候,创建这个文件夹 文件重命名: 把文件text.php得命名为index.php,可以是rename ...
- mysql命令行备份数据库
MySQL数据库使用命令行备份|MySQL数据库备份命令 例如: 数据库地址:127.0.0.1 数据库用户名:root 数据库密码:pass 数据库名称:myweb 备份数据库到D盘跟目录 mysq ...
- WinJs项目介绍
WinJs库是最近微软公布的一个开源项目.它与开源社区的协作共同完成.为了轻易创建HTML/JS/CSS应用程序开发的解决方案.WinJS是一个Javascripts的工具箱让开发人员使用HT ...
- 媲美oracle awr/statspack的mysql awr第一版发布
现发布alpha版mysql awr,其提供的特性类似于oracle awr或statspack+集中式监控.对于原来从事oracle dba或者相关运维的人原来说,这会是个不错的选择. 至于我为什么 ...
- web安全——防火墙
简介 用于实现服务器(Linux)的访问控制的功能的. 分硬件和软件防火墙. 主要是控制访问的流入和服务器的流出. 通过黑名单和白名单的思想来实现更细粒度的控制,这个一般结合其他的应用来定义策略实现. ...
- PHP写日志函数
初学,写一个函数用于存储日志调试. function WriteLog($msg) { $filename = dirname(__FILE__) ."\\Debug.log"; ...
- .NET破解之分享给新注册的朋友
前些日子,在论坛里看了有人发过这个软件,也有大神分析过网络版,如果是重帖,请删除吧:正好11.11注册了很多新会员,给他们一个见面礼吧,抛砖引玉,我才来论坛的时候,也是看着前人教程慢慢学习的:好久没冒 ...
- 转:HTTP 1.1与HTTP 1.0的比较
原文地址:http://blog.csdn.net/elifefly/article/details/3964766 HTTP 1.1与HTTP 1.0的比较 一个WEB站点每天可能要接收到上百万的用 ...