BZOJ 1864 三色二叉树 - 树型dp
题目大意:
给一颗二叉树染色红绿蓝,父亲和儿子颜色必须不同,两个儿子颜色必须不同,问最多和最少能染多少个绿色的、
题目分析:
裸的树型dp:\(dp[u][col][type]\)表示u节点染为col(0-绿色,1-红色,2-蓝色),当前求的是type(0-最小,1-最大)解。
然后最后输出最大为\(max(dp[1][0][1], dp[1][1][1], dp[1][2][1])\),最小为\(min(dp[1][0][0], dp[1][1][0], dp[1][2][0])\)。
code
#include<bits/stdc++.h>
using namespace std;
const int N = 500005, oo = 0x3f3f3f3f;
vector<int> adj[N];
int tot, dp[N][3][2];
inline void read(int k){
int t, v; char tt; scanf("%c", &tt);
t = tt - '0';
for(int i = 1; i <= t; i++){
adj[k].push_back(v = ++tot);
read(v);
}
}
inline int DP(int u, int f, int col, int type){
if(dp[u][col][type] != -1) return dp[u][col][type];
dp[u][col][type] = col == 0 ? 1 : 0;
if(!adj[u].size()) return dp[u][col][type];
int col1, col2; //另外两种
if(col == 0) col1 = 1, col2 = 2;
else if(col == 1) col1 = 0, col2 = 2;
else col1 = 0, col2 = 1;
if(adj[u].size() == 1){ //只有一个儿子
int v = adj[u][0];
if(type == 1)
dp[u][col][type] += max(DP(v, u, col1, type), DP(v, u, col2, type));
else dp[u][col][type] += min(DP(v, u, col1, type), DP(v, u, col2, type));
}
else{ //只有两个儿子
int v1 = adj[u][0], v2 = adj[u][1];
int t1 = DP(v1, u, col1, type) + DP(v2, u, col2, type);
int t2 = DP(v1, u, col2, type) + DP(v2, u, col1, type);
if(type == 1)
dp[u][col][type] += max(t1, t2);
else dp[u][col][type] += min(t1, t2);
}
return dp[u][col][type];
}
int main(){
read(tot = 1);
memset(dp, -1, sizeof dp);
int ans1 = -1, ans2 = oo;
for(int i = 0; i < 3; i++)
ans1 = max(ans1, DP(1, 0, i, 1)), ans2 = min(ans2, DP(1, 0, i, 0));
printf("%d %d", ans1, ans2);
return 0;
}
BZOJ 1864 三色二叉树 - 树型dp的更多相关文章
- bzoj 1864 三色二叉树
Written with StackEdit. Description Input 仅有一行,不超过\(5*10^5\)个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次 ...
- BZOJ_1864_[Zjoi2006]三色二叉树_树形DP
BZOJ_1864_[Zjoi2006]三色二叉树_树形DP 题意: 分析:递归建树,然后DP,从子节点转移. 注意到红色和蓝色没有区别,因为我们可以将红蓝互换而方案是相同的.这样的话我们只需要知道当 ...
- BZOJ 1509 逃学的小孩 - 树型dp
传送门 题目大意: 在一棵树中, 每条边都有一个长度值, 现要求在树中选择 3 个点 X.Y. Z , 满足 X 到 Y 的距离不大于 X 到 Z 的距离, 且 X 到 Y 的距离与 Y 到 Z 的距 ...
- BZOJ 1564 :[NOI2009]二叉查找树(树型DP)
二叉查找树 [题目描述] 已知一棵特殊的二叉查找树.根据定义,该二叉查找树中每个结点的数据值都比它左儿子结点的数据值大,而比它右儿子结点的数据值小. 另一方面,这棵查找树中每个结点都有一个权值,每个结 ...
- 三色二叉树 ---伪树形dp
题目描述 一棵二叉树可以按照如下规则表示成一个由0.1.2组成的字符序列,我们称之为"二叉树序列S": 0 该树没有子节点 1S1 该树有一个子节点,S1为其二叉树序列 1S1S2 ...
- 1864. [ZJOI2006]三色二叉树【树形DP】
Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. Sample ...
- BZOJ-1864-[Zjoi2006]三色二叉树(树形dp)
Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. Sample ...
- 二叉苹果树 - 二叉树树型DP
传送门 中文题面: 题目描述 有一棵苹果树,如果树枝有分叉,一定是分 2 叉(就是说没有只有 1 个儿子的结点,这棵树共有N 个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一 ...
- 三色二叉树_树形DP
Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 一棵二叉树可以按照如下规则表示成一个由0.1.2组成的字符序 ...
随机推荐
- scrapy-爬取斗图
scrapy-爬取斗图代码 <一> 开局三连杀 1.创建run的文件
- DOM相关知识总结
DOM相关: 1.获取DOM元素 document.getElementById document.getElementsByName document.getElementsByTagName do ...
- (转)iptables常用规则:屏蔽IP地址、禁用ping、协议设置、NAT与转发、负载平衡、自定义链
转自:http://lesca.me/archives/iptables-examples.html 本文介绍25个常用的iptables用法.如果你对iptables还不甚了解,可以参考上一篇ipt ...
- [React Intl] Render Content Based on a Number using react-intl FormattedMessage (plural)
Using the react-intl FormattedMessage component, we’ll learn how to render content conditionally in ...
- Css fixed和absolute定位差别
fixed:固定定位 absolute:绝对定位 差别非常easy: 1.没有滚动栏的情况下没有差异 2.在有滚动栏的情况下.fixed定位不会随滚动栏移动而移动.而absolute则会随滚动栏移动 ...
- open ball、closed ball 与 open set、closed set(interior point,limit point)、dense set
0. demo 在拓扑学上,open set(开集)是对实数轴(real line)上开区间(open interval)的拓展. 红色圆盘:{(x,y)|x2+y2<r2},蓝色圆圈:{(x, ...
- TCP套接字编程模型及实例
摘要: 本文讲述了TCP套接字编程模块,包括服务器端的创建套接字.绑定.监听.接受.读/写.终止连接,客户端的创建套接字.连接.读/写.终止连接.先给出实例,进而结合代码分析. PS:本文权当 ...
- pstack.sh 查看进程堆栈
#!/bin/bash if (( $# < 1 )) then echo "usage: `basename $0` pid" 1>&2 ex ...
- flash stm32的flash编写
定义一个全局变量数组:const u8 TEXT_Buffer[]={"STM32F103 FLASH TEST"}; //u8和char* 写入到内存里会有什么区别???? ...
- thinkphp 3.2,引入第三方类库的粗略记录
首先用第三方类库我是放到vendor里面的 根目录\ThinkPHP\Library\Vendor\Wxphp 例如创建了一个Wxphp文件夹,里面有个php文件叫做 zll.php 文 ...
