记录编号 119645 评测结果 AAAAAAAAAAAAA
题目名称 [POI 1999] 三色二叉树 最终得分 100
用户昵称 Gravatar天一阁 是否通过 通过
代码语言 C++ 运行时间 0.020 s
提交时间 2014-09-13 11:57:18 内存使用 0.64 MiB
显示代码纯文本
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #define Maxn 20003
  5. using namespace std;
  6. class tree{
  7. public:
  8. int da,lc,rc;
  9. void push(int x){
  10. if(lc!=0) rc=x;
  11. else lc=x;
  12. }
  13. }T[Maxn]={0};
  14. int tot=0,fw[Maxn]={0},fb[Maxn]={0};
  15. int Dp_w(int),Dp_b(int),Dp_w0(int),Dp_b0(int);
  16. string s;
  17. void build(int now){
  18. tot++;
  19. if(s[tot]!='0'){
  20. if(s[tot]=='1'){
  21. T[now].push(tot+1); build(tot+1);
  22. return;
  23. }
  24. T[now].push(tot+1); build(tot+1);
  25. T[now].push(tot+1); build(tot+1);
  26. }
  27. }
  28. int Dp_b(int now){
  29. if(now==0 || fb[now]) return fb[now];
  30. int t1=Dp_b(T[now].rc)+Dp_w(T[now].lc);
  31. int t2=Dp_w(T[now].rc)+Dp_b(T[now].lc);
  32. fb[now]=max(t1,t2);
  33. return fb[now];
  34. }
  35.  
  36. int Dp_w(int now){
  37. if(now==0 || fw[now]) return fw[now];
  38. fw[now]=Dp_b(T[now].rc)+Dp_b(T[now].lc)+1;
  39. return fw[now];
  40. }
  41. int Dp_b0(int now){
  42. if(now==0 || fb[now]) return fb[now];
  43. if(!T[now].rc) fb[now]=Dp_b0(T[now].lc);
  44. else{
  45. int t1=Dp_b0(T[now].rc)+Dp_w0(T[now].lc);
  46. int t2=Dp_w0(T[now].rc)+Dp_b0(T[now].lc);
  47. fb[now]=min(t1,t2);
  48. }
  49. return fb[now];
  50. }
  51. int Dp_w0(int now){
  52. if(now==0 || fw[now]) return fw[now];
  53. fw[now]=Dp_b0(T[now].rc)+Dp_b0(T[now].lc)+1;
  54. return fw[now];
  55. }
  56. void init(){
  57. ios::sync_with_stdio(false);
  58. cin>>s;s="*"+s;
  59. build(1);
  60. cout<<max(Dp_w(1),Dp_b(1))<<' ';
  61. memset(fw,0,sizeof(fw));
  62. memset(fb,0,sizeof(fb));
  63. cout<<min(Dp_w0(1),Dp_b0(1))<<endl;
  64. }
  65. int main(){
  66. freopen("trot.in","r",stdin);
  67. freopen("trot.out","w",stdout);
  68. init();
  69. return 0;
  70. }