pb_jiang

CF ROUND 1027E AC

May 26th, 2025
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. // Problem: E. Kirei Attacks the Estate
  2. // Contest: Codeforces - Codeforces Round  1027 (Div. 3)
  3. // URL: https://codeforces.com/contest/2114/problem/E
  4. // Memory Limit: 256 MB
  5. // Time Limit: 2000 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. #ifndef __DEBUG__
  13. #define dbg(...) 42
  14. #endif
  15. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  16.  
  17. namespace rngs = std::ranges;
  18. using ll = long long;
  19. using a2l = array<ll, 2>;
  20. using pll = pair<ll, ll>;
  21. using vl = vector<ll>;
  22.  
  23. void solve()
  24. {
  25.     ll n;
  26.     cin >> n;
  27.     vl a(n), ans, alt(n + 1), dep(n + 1);
  28.     for (auto &x : a)
  29.         cin >> x;
  30.     a.insert(a.begin(), 0ll);
  31.     ans = a;
  32.  
  33.     vector<vl> g(n + 1);
  34.     for (ll i = 0, u, v; i < n - 1; ++i) {
  35.         cin >> u >> v;
  36.         g[u].push_back(v), g[v].push_back(u);
  37.     }
  38.  
  39.     auto dfs1 = [&](auto &&self, ll u, ll fa, ll v, ll d) -> void {
  40.         alt[u] = a[u] - v;
  41.         dep[u] = d;
  42.         for (auto v : g[u]) {
  43.             if (v == fa)
  44.                 continue;
  45.             self(self, v, u, alt[u], d + 1);
  46.         }
  47.     };
  48.     dfs1(dfs1, 1, -1, 0, 0);
  49.  
  50.     auto dfs2 = [&](auto &&self, ll u, ll fa, ll val, ll v1, ll v2, ll d) -> void {
  51.         val = a[u] - val;
  52.         ans[u] = max(ans[u], val - v1);
  53.         ans[u] = max(ans[u], val + v2);
  54.         v1 = max(v1, val);
  55.  
  56.         for (auto v : g[u]) {
  57.             if (v == fa)
  58.                 continue;
  59.             self(self, v, u, val, v2, v1, d + 1);
  60.         }
  61.     };
  62.     dfs2(dfs2, 1, -1, 0, 0, 0, 0);
  63.  
  64.     for (ll i = 1; i <= n; ++i)
  65.         cout << ans[i] << ' ';
  66.     cout << '\n';
  67. }
  68.  
  69. int main(int argc, char **argv)
  70. {
  71.     std::ios::sync_with_stdio(false);
  72.     std::cin.tie(nullptr);
  73.  
  74.     ll t;
  75.     cin >> t;
  76.     while (t--)
  77.         solve();
  78.  
  79.     return 0;
  80. };
  81.  
Advertisement
Add Comment
Please, Sign In to add comment