传送门

题目大意:

给一颗二叉树染色红绿蓝,父亲和儿子颜色必须不同,两个儿子颜色必须不同,问最多和最少能染多少个绿色的、

题目分析:

裸的树型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的更多相关文章

  1. bzoj 1864 三色二叉树

    Written with StackEdit. Description Input 仅有一行,不超过\(5*10^5\)个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次 ...

  2. BZOJ_1864_[Zjoi2006]三色二叉树_树形DP

    BZOJ_1864_[Zjoi2006]三色二叉树_树形DP 题意: 分析:递归建树,然后DP,从子节点转移. 注意到红色和蓝色没有区别,因为我们可以将红蓝互换而方案是相同的.这样的话我们只需要知道当 ...

  3. BZOJ 1509 逃学的小孩 - 树型dp

    传送门 题目大意: 在一棵树中, 每条边都有一个长度值, 现要求在树中选择 3 个点 X.Y. Z , 满足 X 到 Y 的距离不大于 X 到 Z 的距离, 且 X 到 Y 的距离与 Y 到 Z 的距 ...

  4. BZOJ 1564 :[NOI2009]二叉查找树(树型DP)

    二叉查找树 [题目描述] 已知一棵特殊的二叉查找树.根据定义,该二叉查找树中每个结点的数据值都比它左儿子结点的数据值大,而比它右儿子结点的数据值小. 另一方面,这棵查找树中每个结点都有一个权值,每个结 ...

  5. 三色二叉树 ---伪树形dp

    题目描述 一棵二叉树可以按照如下规则表示成一个由0.1.2组成的字符序列,我们称之为"二叉树序列S": 0 该树没有子节点 1S1 该树有一个子节点,S1为其二叉树序列 1S1S2 ...

  6. 1864. [ZJOI2006]三色二叉树【树形DP】

    Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. Sample ...

  7. BZOJ-1864-[Zjoi2006]三色二叉树(树形dp)

    Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. Sample ...

  8. 二叉苹果树 - 二叉树树型DP

    传送门 中文题面: 题目描述 有一棵苹果树,如果树枝有分叉,一定是分 2 叉(就是说没有只有 1 个儿子的结点,这棵树共有N 个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一 ...

  9. 三色二叉树_树形DP

    Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description 一棵二叉树可以按照如下规则表示成一个由0.1.2组成的字符序 ...

随机推荐

  1. 洛谷—— P1967 货车运输 || COGS——C 1439. [NOIP2013]货车运输

    https://www.luogu.org/problem/show?pid=1967#sub  ||  http://www.cogs.pro/cogs/problem/problem.php?pi ...

  2. LeetCode Algorithm 04_Median of Two Sorted Arrays

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  3. POJ 2718 Smallest Difference 枚举

    http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...

  4. Spring拦截器和Servlet过滤器区别

    http://blog.csdn.net/chenleixing/article/details/44573495

  5. [React Intl] Format a Date Relative to the Current Date Using react-intl FormattedRelative

    Given a date, we’ll use the react-intl FormattedRelative component to render a date in a human reada ...

  6. [Android 4.4.2] 泛泰A870 Mokee4.4.2 20140531 RC1.0 by syhost

    欢迎关注泛泰非盈利专业第三方开发团队 VegaDevTeam  (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo cr ...

  7. log4j 2.x 版本的 properties 配置

    #用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,会看到log4j2内部各种详细输出status = debugdest = errname = PropertiesConf ...

  8. nginx启用https访问

    什么是https? https 全称:Hyper Text Transfer Protocol over Secure Socket Layer,是http的安全版.即http下加入SSL协议层,因此 ...

  9. 11.1 Android显示系统框架_framebuffer原理及改进

    1. Android显示系统框架Android Graphic UI with GPU Hardware Accelerationhttps://community.nxp.com/docs/DOC- ...

  10. 链表(三)——链表删除冗余结点&amp;插入结点到有序链表

    1.一个以递增方式排列的链表,去掉链表中的冗余值. 思路一:设有两个指针p和q.使p不动,q依次往后循环直到p->data不等于q->data,再将中间的冗余数据删除. 思路二:设有两个指 ...