Description
![](https://www.lydsy.com/JudgeOnline/images/1864.jpg)
Input
仅有一行,不超过500000个字符,表示一个二叉树序列。
Output
输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色。
题解:本题大水题,主要卡读入. 能把树建出来就普及组水题了~
#include <bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 1000000
#define lson ch[u][0]
#define rson ch[u][1]
using namespace std;
int ch[maxn][2], f[maxn], g[maxn];
int n = 1;
void read(int u)
{
char c = getchar();
if(c == '0') return;
++n, lson = n, read(lson);
if(c == '2') ++n, rson = n, read(rson);
}
void dfs1(int u)
{
if(!u) return;
dfs1(lson), dfs1(rson);
f[u] = g[lson] + g[rson] + 1;
g[u] = max(f[lson] + g[rson], g[lson] + f[rson]);
}
void dfs2(int u)
{
if(!u) return;
dfs2(lson), dfs2(rson);
f[u] = g[u] = 0;
f[u] = g[lson] + g[rson] + 1;
g[u] = min(f[lson] + g[rson], g[lson] + f[rson]);
}
int main()
{
// setIO("input");
read(1);
dfs1(1);
printf("%d ",max(g[1], f[1]));
dfs2(1);
printf("%d\n",min(g[1], f[1]));
return 0;
}