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组成的字符序 ...
随机推荐
- 水题ing
T1: https://www.luogu.org/problemnew/show/P1724幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆 ...
- jvm compile
>>>Making sec-files-win @ Thu Oct 17 20:34:02 CST 2013 ... >>>Making jgss-files @ ...
- JS中的闭包问题总结
严格意义上的闭包,严格闭包通过栈内存不销毁,保护内部变量,而且下一级作用域可以访问内部变量 更严格意义上的闭包,函数可以在父函数外面调用父函数作用域的值 在函数执行的时候,函数体中有返回值,函数执行的 ...
- swift开发多线程篇 - NSThread 线程相关简单说明(一些使用和注意点)
一 说明 本文涉及代码可以从https://github.com/HanGangAndHanMeimei/Code地址获得. 二 NSThread的基本使用和创建 1)基本用法(主线程|当前线程) 1 ...
- Oracle 带回滚的存储过程
create or replace procedure PROC_insertUserAmount ( userid number, msgtype number, amountvalue numbe ...
- UVA 11280 - Flying to Fredericton SPFA变形
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...
- Compmgmtlauncher.exe问题解决方法
修改注册表:HKEY_CLASSES_ROOT\CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}\shell\Manage\command 原来的默认键值为 ...
- Mybatis全面详解——上(学习总结)
原文地址:https://blog.csdn.net/ITITII/article/details/79969447 一.什么是Mybatis 这里借用官网的一句话介绍什么是mybatis:MyBat ...
- [RxJS] Use takeUntil instead of manually unsubscribing from Observables
Manually unsubscribing from subscriptions is safe, but tedious and error-prone. This lesson will tea ...
- 2. pushd . :将当前文件夹压入栈,使用popd能够回到该文件夹。
1.man -t ls | ps2pdf -> ls.pdf生成pdf格式的ls帮助文件. 2. pushd . :将当前文件夹压入栈,使用popd能够回到该文件夹. 3.find -type ...
