经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
TopcoderSRM679 Div1 250 FiringEmployees(树形dp)
来源:cnblogs  作者:自为风月马前卒  时间:2018/10/12 9:45:34  对本文有异议

题意

[题目链接]这怎么发链接啊。。。。。

有一个 \(n\) 个点的树,每个点有点权(点权可能为负) ,求包含点\(1\)的最
大权连通子图(的权值和) 。
\(n \leqslant 2500\)

Sol

刚开始还以为是个树形依赖背包呢。。结果发现后面给的两个vector根本就没用

直接减一下得到每个点的点权,然后xjb dp一波

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 1e5 + 10;
  4. inline int read() {
  5. char c = getchar(); int x = 0, f = 1;
  6. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  7. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  8. return x * f;
  9. }
  10. int a[MAXN], f[MAXN];
  11. vector<int> v[MAXN];
  12. class FiringEmployees{
  13. public:
  14. void dfs(int x, int fa) {
  15. f[x] = a[x];
  16. for(int i = 0, to; i < v[x].size(); i++) {
  17. if((to = v[x][i]) == fa) continue;
  18. dfs(to, x);
  19. f[x] = max(f[x], f[x] + f[to]);
  20. }
  21. }
  22. int fire(vector <int> fa, vector <int> salary, vector <int> productivity) {
  23. int N = fa.size();
  24. for(int i = 1; i <= N; i++) {
  25. a[i] = productivity[i - 1] - salary[i - 1];
  26. //cout << fa[i - 1] << endl;
  27. v[fa[i - 1]].push_back(i);
  28. }
  29. dfs(0, -1);
  30. return f[0];
  31. }
  32. };
  33. int main() {
  34. int N = read();
  35. vector<int> a, b, c;
  36. for(int i = 1; i <= N; i++) a.push_back(read());
  37. for(int i = 1; i <= N; i++) b.push_back(read());
  38. for(int i = 1; i <= N; i++) c.push_back(read());
  39. cout << FiringEmployees().fire(a, b, c);
  40. }
  41. /*
  42. 6
  43. 0 0 1 1 2 2
  44. 1 1 1 2 2 2
  45. 2 2 2 1 1 1
  46. 9
  47. 0 1 2 1 2 3 4 2 3
  48. 5 3 6 8 4 2 4 6 7
  49. 2 5 7 8 5 3 5 7 9
  50. 2
  51. 0 1
  52. 1 10
  53. 5 5
  54. 4
  55. {{0, 1, 2, 3}
  56. {4, 3, 2, 1}
  57. {2, 3, 4, 5}}
  58. */
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号