[bzoj1864][Zjoi2006]三色二叉树(树上dp)
2016-07-19 15:37
519 查看
【题目链接】http://www.lydsy.com/JudgeOnline/problem.php?id=1864
【呆马】
【呆马】
#include<cstdio> #include<algorithm> #include<cmath> #include<cstdlib> #include<iostream> #include<cstring> const int N=500001; using namespace std; int n,l[N],r[N],f[N][2]; void build(int x) { char ch=getchar(); if (ch=='0') return; build(l[x]=++n); if (ch=='2') build(r[x]=++n); } void dp(int x,int y) { if (!x) return; dp(l[x],y); dp(r[x],y); f[x][1]=f[l[x]][0]+f[r[x]][0]+1; f[x][0]=y?min(f[l[x]][0]+f[r[x]][1],f[l[x]][1]+f[r[x]][0]):max(f[l[x]][0]+f[r[x]][1],f[l[x]][1]+f[r[x]][0]); } int main() { build(n=1); dp(1,0); printf("%d ",max(f[1][0],f[1][1])); dp(1,1); printf("%d",min(f[1][0],f[1][1])); }
相关文章推荐
- BZOJ3275 Number (最小割)
- BZOJ2809——[Apio2012]dispatching
- BZOJ2809——[Apio2012]dispatching
- [bzoj1003] [ZJOI2006]物流运输trans
- [bzoj1500][NOI2005]维修数列
- [bzoj1208] [HNOI2004]宠物收养所
- [bzoj1269][AHOI2006]文本编辑器editort
- [bzoj1503][NOI2004]郁闷的出纳员
- bzoj4305 数学
- bzoj3926 广义后缀自动机
- bzoj2780 广义后缀自动机+parent树+Dfs序+树状数组
- BZOJ1997 2-sat
- bzoj4027 贪心
- [BZOJ2038][2009国家集训队][莫队][分块]小z的袜子
- [BZOJ2594][WC2006][LCT][MST]水管局长数据加强版
- [BZOJ2300][HAOI2011][动态凸包]防线修建
- [BZOJ1045][HAOI2008][贪心]糖果传递
- [BZOJ2539][CTSC2000][KM]丘比特的烦恼
- [BZOJ1004][HNOI2008][Burnside引理][DP]Cards
- [BZOJ1202][HNOI2005][并查集]狡猾的商人