您的位置:首页 > 其它

[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]));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  树上dp bzoj zjoi