bzoj1864 Tri-color two-fork tree

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.