刷水ing…
AC code:
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int N=1000010;
int l,tot=1,cnt=-1,ans1,ans2;
int f[N][3],g[N][3];
char s[N];
vector<int> G[N];
void addedge(int u,int v){
G[u].push_back(v);
G[v].push_back(u);
}
void build(int x){
cnt++;
int t=cnt;
for(int i=1;i<=s[t]-'0';i++){
addedge(x,++tot);
build(tot);
}
}
void DP(int x){
if(G[x].size()==1&&x!=1){
f[x][0]=g[x][0]=1;
return ;
}
int lc=0,rc=0;
for(int i=0;i<(int)G[x].size();i++){
if(x<G[x][i]){
if(!lc) lc=G[x][i];
else rc=G[x][i];
DP(G[x][i]);
}
}
if(rc){
f[x][0]=max(f[lc][1]+f[rc][2],f[lc][2]+f[rc][1])+1;
g[x][0]=min(g[lc][1]+g[rc][2],g[lc][2]+g[rc][1])+1;
f[x][1]=max(f[lc][0]+f[rc][2],f[lc][2]+f[rc][0]);
g[x][1]=min(g[lc][0]+g[rc][2],g[lc][2]+g[rc][0]);
f[x][2]=max(f[lc][0]+f[rc][1],f[lc][1]+f[rc][0]);
g[x][2]=min(g[lc][0]+g[rc][1],g[lc][1]+g[rc][0]);
}
else{
f[x][0]=max(f[lc][1],f[lc][2])+1;
g[x][0]=min(g[lc][1],g[lc][2])+1;
f[x][1]=max(f[lc][0],f[lc][2]);
g[x][1]=min(g[lc][0],g[lc][2]);
f[x][2]=max(f[lc][0],f[lc][1]);
g[x][2]=min(g[lc][0],g[lc][1]);
}
}
int main(){
scanf("%s",s);
build(1);
DP(1);
ans1=max(max(f[1][0],f[1][1]),f[1][2]);
ans2=min(min(g[1][0],g[1][1]),g[1][2]);
printf("%d %d\n",ans1,ans2);
return 0;
}