Advertisement
pb_jiang

luogu P1122 WA

Jan 16th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. // Problem: P1122 最大子树和
  2. // Contest: Luogu
  3. // URL: https://www.luogu.com.cn/problem/P1122
  4. // Memory Limit: 128 MB
  5. // Time Limit: 1000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  13. template <typename... Args> void logger(string vars, Args &&... values)
  14. {
  15.     cerr << vars << " = ";
  16.     string delim = "";
  17.     (..., (cerr << delim << values, delim = ", "));
  18.     cerr << endl;
  19. }
  20.  
  21. template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
  22. template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
  23. template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
  24. template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
  25.  
  26. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  27.  
  28. using ll = long long;
  29. using pii = pair<int, int>;
  30. using pll = pair<ll, ll>;
  31. using vl = vector<ll>;
  32. using vi = vector<int>;
  33.  
  34. vi g[200003];
  35. ll p[200003];
  36. ll ans, tot;
  37. int n;
  38.  
  39. pll dfs(int u, int fa)
  40. {
  41.     ll take = p[u], no_take = 0;
  42.     for (auto v : g[u]) {
  43.         if (v == fa)
  44.             continue;
  45.  
  46.         auto [st, snt] = dfs(v, u);
  47.  
  48.         take += max(st, 0ll);
  49.         no_take = max({no_take, st, snt});
  50.     }
  51.     ans = max({ans, take, no_take});
  52.     return {take, no_take};
  53. }
  54.  
  55. int main(int argc, char **argv)
  56. {
  57.     cin >> n;
  58.     for (int i = 1; i <= n; ++i)
  59.         cin >> p[i], tot += p[i];
  60.     for (int i = 1; i < n; ++i) {
  61.         int a, b;
  62.         cin >> a >> b;
  63.         g[a].push_back(b), g[b].push_back(a);
  64.     }
  65.  
  66.     ans = LLONG_MIN;
  67.     dfs(1, -1);
  68.     cout << ans << endl;
  69.     return 0;
  70. };
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement