Description
Input
Only one row, no more than 500,000 characters, represents a binary tree sequence.
Output
The output file also has only one row, containing two numbers, which in turn indicate the maximum and minimum number of points that can be dyed green.
Record the maximum and minimum number of green nodes in a subtree where each node is dyed with this node as the root of each color
Information is passed up by the leaf node
#include <cstdio>intN;intp=0, mxv=0, mnv=2147483647;intm3[]={0,1,2,0,1,2};Chars[500005];intcmax[500005][3];intcmin[500005][3];inlinevoidMAXS (int&a,intb) {if(a<b) a=b;} Inlinevoidminsint&a,intb) {if(a>b) a=b;}voidDfsintW) {P=W; if(s[w]=='0') {cmax[w][0]=cmin[w][0]=1; }Else if(s[w]=='1') {DFS (w+1); for(intI=0;i<3; i++) {Cmin[w][i]=2147483647; for(intj=0;j<3; j + +) if(i!=j) {Maxs (cmax[w][i],cmax[w+1][j]); mins (cmin[w][i],cmin[w+1][j]); }} cmax[w][0]++; cmin[w][0]++; }Else{DFS (w+1); intr=p+1; DFS (R); for(intI=0;i<3; i++) {Cmin[w][i]=2147483647; for(intj=0;j<3; j + +) if(i!=j) { intk=m3[i+J]; Maxs (Cmax[w][i],cmax[w+1][j]+Cmax[r][k]); mins (cmin[w][i],cmin[w+1][j]+Cmin[r][k]); }} cmax[w][0]++; cmin[w][0]++; }}intMain () {scanf ("%s", s); DFS (0); for(intI=0;i<3; i++) {maxs (mxv,cmax[0][i]); mins (mnv,cmin[0][i]); } printf ("%d%d\n", MXV,MNV); return 0;}
bzoj1864 Tri-color two-fork tree