树形dp

  1. #include <cstdio>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. #define D(x)
  6.  
  7. const int INF = 0x3f3f3f3f;
  8. const int MAX_N = (int)(2e5) + ;
  9.  
  10. int n;
  11. vector<pair<int, int> > edge[MAX_N];
  12. int re_num[MAX_N];
  13. vector<int> ans_vec;
  14.  
  15. int dfs(int father, int u, int add, int re_add)
  16. {
  17. D(printf("u=%d\n", u));
  18. int ret = ;
  19. for (int i = ; i < (int)edge[u].size(); i++)
  20. {
  21. int v = edge[u][i].first;
  22. int w = edge[u][i].second;
  23. if (v == father)
  24. {
  25. continue;
  26. }
  27. D(printf("u=%d, v=%d\n", u, v));
  28. ret += dfs(u, v, add + ( - w), re_add + w) + w;
  29. }
  30. re_num[u] = add - re_add;
  31. D(printf("%d %d\n", u, ret));
  32. return ret;
  33. }
  34.  
  35. int main()
  36. {
  37. scanf("%d", &n);
  38. for (int i = ; i < n; i++)
  39. {
  40. int a, b;
  41. scanf("%d%d", &a, &b);
  42. edge[a].push_back(make_pair(b, ));
  43. edge[b].push_back(make_pair(a, ));
  44. D(printf("%d %d\n", a, b));
  45. }
  46. fill_n(re_num, n + , );
  47. int ans = dfs(-, , , );
  48. ans_vec.push_back();
  49. int root_ans = ans;
  50. D(printf("%d\n", root_ans));
  51. for (int i = ; i <= n; i++)
  52. {
  53. int temp = re_num[i] + root_ans;
  54. if (temp == ans)
  55. {
  56. ans_vec.push_back(i);
  57. }
  58. if (temp < ans)
  59. {
  60. ans = temp;
  61. ans_vec.clear();
  62. ans_vec.push_back(i);
  63. }
  64. }
  65. printf("%d\n", ans);
  66. for (int i = ; i < (int)ans_vec.size(); i++)
  67. {
  68. printf("%d", ans_vec[i]);
  69. if (i != (int)ans_vec.size() - )
  70. putchar(' ');
  71. }
  72. puts("");
  73. return ;
  74. }

cf219d的更多相关文章

  1. CF219D. Choosing Capital for Treeland [树形DP]

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

  2. cf219d 基础换根法

    /*树形dp换根法*/ #include<bits/stdc++.h> using namespace std; #define maxn 200005 ]; int root,n,s,t ...

  3. CF219D Choosing Capital for Treeland

    嘟嘟嘟 树形dp. 首先一个很常规的想法就是如果u到v有一条边,那么建立cost(u, v) = 0, cost(v, u) = 1的两条边. 可以两遍dfs. 先任选一个点作为根节点,第一遍从下往上 ...

随机推荐

  1. C#反射机制(转)

    一:反射的定义 审查元数据并收集关于它的类型信息的能力.元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等. Sys ...

  2. R--基本统计分析方法(包及函数)

    摘要:目前经典的统计学分析方法主要有回归分析,Logistic回归,决策树,支持向量机,聚类分析,关联分析,主成分分析,对应分析,因子分析等,那么对于这些经典的分析方法在R中的使用主要有那些程序包及函 ...

  3. Java实验1-文件IO

    目标:掌握Java类的创建,Java  I/O操作,Java集合类的使用等 内容: 王老师非常喜欢读书,为了便于查阅,他每次买书回家后就在笔记本上登记每本书的详细信息(书名.作者.出版社.出版日期.价 ...

  4. gtest

    一.安装配置 1.简介 2.安装 下载地址: https://code.google.com/p/googletest/downloads/list 解压安装: unzip gtest-1.7.0.z ...

  5. 一起找bug

    帮同学找的一个bug,错误代码如下: package dai_test; public class Test1 { public static void main(String[] args) { / ...

  6. ASP.NET MVC 4 的JS/CSS打包压缩功能-------过滤文件

    今天在使用MVC4打包压缩功能@Scripts.Render("~/bundles/jquery") 的时候产生了一些疑惑,问什么在App_Start文件夹下BundleConfi ...

  7. angular $resource模块

    目录(?)[-] 安装 应用resource 扩展resource   上一篇中讲到使用$http同服务器进行通信,但是功能上比较简单,angularjs还提供了另外一个可选的服务$resource, ...

  8. CSU 1328: 近似回文词

    省赛的A题...现场都没什么人做...其实就一暴力水题......坑死了... 1328: 近似回文词 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1 ...

  9. QT点击"X"按钮,调用closeEvent()函数来实现调用特定事件(附:粗略介绍QT的信号与槽的使用方法)

    背景: QT在用户关闭窗口(直接点击"X"键)时,程序一般都需要做一些善后的事情,就我现在的程序来说,既关闭USB.如何实现? 正文: 首先,在对应窗体的".h" ...

  10. 【PHP面向对象(OOP)编程入门教程】21.多态的应用

    多态是除封装和继承之外的另一个面象对象的三大特性之一,我个人看来PHP中虽然可以实现多态,但和c++还有Java这些面向对象的语言相比,多 态性并不是那么突出,因为PHP本身就是一种弱类型的语言,不存 ...