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 ...
随机推荐
- java猜数字游戏
import java.util.Scanner; //导入包 class GuessNum { public static void main(String[] args) { int num = ...
- Linux 安装 PHP 环境
使用虚拟机玩linux时,发现CentOS中的php版本是5.1.6.如果要安装新版的php,需要把旧的版本删除. 先查看下php版本:# php -v 如果执行该命令提示该命令不存在,那么可以通过以 ...
- Design Patterns (简单工厂模式)
文章很长很精彩,如是初学请耐心观看.(大神请绕道!) 简单工厂模式: 1.创建型模式 2.简单工厂模式概述 3.简单工厂模式的结构与实现 4.简单工厂模式的应用实例 5.创建对象与使用对象 6.简单工 ...
- Cache&Session Viewer
用于查看和删除网站Cache https://github.com/sdf333/Aspy
- 百度网盘采集源码 ,直接采集网盘添加cookies功能
名称:百度网盘采集源码 程序语言:php 数据库:mysql 程序介绍: 1.直接采集百度网盘url 2.前端基于bootstrap 3.搜索考虑到后期上亿数据,是基于coreseek,搜索时间毫秒级 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q73-Q76)
Question 73You create a Web Part that calls a function named longCall.You discover that longCall tak ...
- 转发离线安装 Android Studio 更新
1.在线更新 随着 Android Studio 的越来越完善与流行,无论从功能性,还是性能上,它正在成为广大 Android 开发者的首选.但是因为总所周知墙的原因,我们在 Android Stud ...
- PL/SQL基础-异常处理
--*********异常处理一.异常的类型 ORACLE异常分为两种类型:系统异常.自定义异常. 其中系统异常又分为:预定义异常和非预定义异常.1.预定义异常 ORACLE定义了他们的错误编号和异常 ...
- 【原】iOS下KVO使用过程中的陷阱
KVO,全称为Key-Value Observing,是iOS中的一种设计模式,用于检测对象的某些属性的实时变化情况并作出响应.网上广为流传普及的一个例子是利用KVO检测股票价格的变动,例如这里.这个 ...
- mac上的替代软件
文本编辑器:bbedit 视频播放器:Movist 图片浏览:LilyView 远程桌面:Remotix 虚拟主机:parallels desktop svn client:cornerstone o ...