记录编号 383814 评测结果 AAAAAAAAAA
题目名称 火车站饭店 最终得分 100
用户昵称 GravatarkZime 是否通过 通过
代码语言 C++ 运行时间 0.162 s
提交时间 2017-03-16 16:10:31 内存使用 2.21 MiB
显示代码纯文本
  1. /*kZime*/
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <vector>
  7. #include <stack>
  8. #include <queue>
  9. #include <algorithm>
  10. #define MAXN 100233
  11. using namespace std;
  12. inline int read() {
  13. int k = 0, f = 1; char c = getchar();
  14. for(; !isdigit(c); c = getchar())if(c == '-') f = -1;
  15. for(; isdigit(c); c = getchar()) k = k * 10 + c - '0';
  16. return k * f;
  17. }
  18. /*-----------------------------------------------------------------------------*/
  19.  
  20. stack <int> abfs;
  21.  
  22. struct node{
  23. int fa;
  24. vector<int> ch;
  25. }nd[MAXN];
  26. int w[MAXN], n, f[MAXN][2];
  27.  
  28.  
  29. void f_ab() {
  30. queue<int> bfs;
  31. bfs.push(1);
  32. while(!bfs.empty()) {
  33. int x = bfs.front();
  34. bfs.pop();
  35. abfs.push(x);
  36. for(int i = 0; i < nd[x].ch.size(); i++) {
  37. if(nd[x].fa == nd[x].ch[i]) continue;
  38. nd[nd[x].ch[i]].fa = x;
  39. bfs.push(nd[x].ch[i]);
  40. }
  41. }
  42. }
  43.  
  44. int AC() {
  45. #ifndef MYLAB
  46. freopen("profitz.in", "r", stdin);
  47. freopen("profitz.out", "w", stdout);
  48. #else
  49. freopen("in.txt", "r", stdin);
  50. #endif
  51. n = read();
  52. for(int i = 1; i <= n; i++) {
  53. w[i] = read();
  54. f[i][1] = w[i];
  55. }
  56. for(int i = 1; i < n; i++) {
  57. int x = read();
  58. int y = read();
  59. nd[x].ch.push_back(y);
  60. nd[y].ch.push_back(x);
  61. }
  62.  
  63. f_ab();
  64.  
  65. while(!abfs.empty()) {
  66. int x = abfs.top();
  67. abfs.pop();
  68. for(int i = 0; i < nd[x].ch.size(); i++) {
  69. if(nd[x].ch[i] == nd[x].fa)continue;
  70. f[x][1] += f[nd[x].ch[i]][0];
  71. f[x][0] += max(f[nd[x].ch[i]][1], f[nd[x].ch[i]][0]);
  72. }
  73. }
  74. printf("%d", max(f[1][0], f[1][1]));
  75.  
  76. return 0;
  77. }
  78. int HA = AC();
  79. int main(){;}
  80.