博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ1864[ZJOI2006]三色二叉树[树形DP]
阅读量:6932 次
发布时间:2019-06-27

本文共 1423 字,大约阅读时间需要 4 分钟。

1864: [Zjoi2006]三色二叉树

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 773  Solved: 548
[][][]

Description

Input

仅有一行,不超过500000个字符,表示一个二叉树序列。

Output

输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色。

Sample Input

1122002010

Sample Output

5 2

HINT

 

Source


先建树

d[i][0/1]表示以i为根的子树i绿色1其他0下最多几个点绿色 

三个颜色转移就是父子三人两个其他一个绿色

见代码

/**************************************************************    Problem: 1864    User: thwfhk    Language: C++    Result: Accepted    Time:68 ms    Memory:13884 kb****************************************************************/#include
#include
#include
#include
using namespace std;const int N=5e5+5,INF=1e9;int n,mx=-INF,mn=INF;char s[N];struct node{ int ls,rs; node():ls(0),rs(0){}}tree[N];int cnt=0,pos=1;int build(){ int u=++cnt,num=s[pos]-'0'; pos++; if(num>=1) tree[u].ls=build(); if(num==2) tree[u].rs=build(); return u;}int d[N][2],f[N][2];void dp(int u){
//printf("dp %d\n",u); if(u==0) return; int ls=tree[u].ls,rs=tree[u].rs; dp(ls);dp(rs); d[u][0]=max(d[ls][0]+d[rs][1],d[ls][1]+d[rs][0]); f[u][0]=min(f[ls][0]+f[rs][1],f[ls][1]+f[rs][0]); d[u][1]=d[ls][0]+d[rs][0]+1; f[u][1]=f[ls][0]+f[rs][0]+1;}int main(int argc, const char * argv[]) { scanf("%s",s+1); n=strlen(s+1); build(); dp(1); mx=max(d[1][0],d[1][1]); mn=min(f[1][0],f[1][1]); printf("%d %d",mx,mn); return 0;}

 

转载地址:http://qcmjl.baihongyu.com/

你可能感兴趣的文章
Elasticsearch 2.3.0 安装成服务
查看>>
UIkit 分页组件动态加载简单实现
查看>>
【工具使用系列】关于 MATLAB HDL Coder, 你需要知道的事
查看>>
演讲实录 | 招银云创:容器PaaS正在让开发人员再也看不到IaaS
查看>>
Spring+Mybatis之多数据源配置
查看>>
阿里数据库专家博客
查看>>
jetty插件java.lang.OutOfMemoryError: PermGen space内存溢出
查看>>
使用 Shell 转换 Linux 目录下的所有其他编码文本文件转为UTF-8编码
查看>>
twisted 线程池管理
查看>>
利用shell + python + expect实现类似xshell功能(测试环境:ubuntu)
查看>>
什么是web前端
查看>>
sessionStorage 、localStorage 和 cookie 之间的区别
查看>>
linux 常用流量查看命令
查看>>
Linux常用命令
查看>>
mysql数据库与数据库实例的区别
查看>>
设计模式-单例模式
查看>>
【LeetCode OJ】Climbing Stairs
查看>>
Mac 在启动eclipse时 Failed to load JavaHL Library解决方法
查看>>
Struts2表单重复提交
查看>>
【React Native开发】React Native库版本升级(Upgrading)与降级讲解
查看>>