• 548 - Tree (UVA)


    题目链接如下:

    Online Judge

    开始的代码如下:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. const int INF = 99999999;
    7. // #define debug
    8. std::vector<int> inorder, postorder;
    9. std::string line;
    10. int t, root, minn, key, sum;
    11. std::map<int, int> inloc, left, right;
    12. int findRoot(int inLeft, int inRight, int postLeft, int postRight){
    13. if (inLeft == inRight){
    14. return 0;
    15. }
    16. int rt = postorder[postRight - 1];
    17. int loc = inloc[rt];
    18. left[rt] = findRoot(inLeft, loc, postLeft, loc - inLeft + postLeft);
    19. right[rt] = findRoot(loc + 1, inRight, loc - inLeft + postLeft, postRight - 1);
    20. return rt;
    21. }
    22. void dfs(int k){
    23. sum += k;
    24. if (!left[k] && !right[k]){
    25. if (sum < minn){
    26. minn = sum;
    27. key = k;
    28. } else if (sum == minn && key > k){
    29. key = k;
    30. }
    31. sum -= k;
    32. return;
    33. }
    34. if (left[k]){
    35. dfs(left[k]);
    36. }
    37. if (right[k]){
    38. dfs(right[k]);
    39. }
    40. sum -= k;
    41. }
    42. int main(){
    43. #ifdef debug
    44. freopen("0.txt", "r", stdin);
    45. freopen("1.txt", "w", stdout);
    46. #endif
    47. while (getline(std::cin, line)){
    48. std::stringstream in(line);
    49. while (in >> t){
    50. inloc[t] = inorder.size();
    51. inorder.push_back(t);
    52. }
    53. getline(std::cin, line);
    54. std::stringstream post(line);
    55. while (post >> t){
    56. postorder.push_back(t);
    57. }
    58. root = findRoot(0, inorder.size(), 0, inorder.size());
    59. minn = INF;
    60. sum = 0;
    61. dfs(root);
    62. printf("%d\n", key);
    63. inorder.clear();
    64. postorder.clear();
    65. left.clear();
    66. right.clear();
    67. inloc.clear();
    68. }
    69. #ifdef debug
    70. fclose(stdin);
    71. fclose(stdout);
    72. #endif
    73. return 0;
    74. }

    后来看别人的代码发现dfs可以省掉,修改后如下:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. const int INF = 99999999;
    7. // #define debug
    8. std::vector<int> inorder, postorder;
    9. std::string line;
    10. int t, root, minn, key;
    11. std::map<int, int> inloc, left, right;
    12. int findRoot(int inLeft, int inRight, int postLeft, int postRight, int tot){
    13. if (inLeft == inRight){
    14. return 0;
    15. }
    16. int rt = postorder[postRight - 1];
    17. int sum = tot + rt;
    18. int loc = inloc[rt];
    19. left[rt] = findRoot(inLeft, loc, postLeft, loc - inLeft + postLeft, sum);
    20. right[rt] = findRoot(loc + 1, inRight, loc - inLeft + postLeft, postRight - 1, sum);
    21. if (!left[rt] && !right[rt]){
    22. if (sum < minn || (sum == minn && rt < key)){
    23. minn = sum;
    24. key = rt;
    25. }
    26. }
    27. return rt;
    28. }
    29. int main(){
    30. #ifdef debug
    31. freopen("0.txt", "r", stdin);
    32. freopen("1.txt", "w", stdout);
    33. #endif
    34. while (getline(std::cin, line)){
    35. std::stringstream in(line);
    36. while (in >> t){
    37. inloc[t] = inorder.size();
    38. inorder.push_back(t);
    39. }
    40. getline(std::cin, line);
    41. std::stringstream post(line);
    42. while (post >> t){
    43. postorder.push_back(t);
    44. }
    45. minn = INF;
    46. root = findRoot(0, inorder.size(), 0, inorder.size(), 0);
    47. printf("%d\n", key);
    48. inorder.clear();
    49. postorder.clear();
    50. left.clear();
    51. right.clear();
    52. inloc.clear();
    53. }
    54. #ifdef debug
    55. fclose(stdin);
    56. fclose(stdout);
    57. #endif
    58. return 0;
    59. }

  • 相关阅读:
    【考研数学】概率论与数理统计 —— 第七章 | 参数估计(2,参数估计量的评价、正态总体的区间估计)
    vue版本升级导致vant这个UI组件中的loading失效问题
    leetcode-946:验证栈序列
    flex 完成六等分布局
    Android 打开系统文件管理器,并返回选中文件的路径
    gradle编译spring源码过程问题整理
    Vue3 select循环多个,选项option不能重复被选
    目录信息收集
    OAuth 2.0一键登录那些事
    jvm 垃圾回收
  • 原文地址:https://blog.csdn.net/linh2006/article/details/134556777