Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: E. Kirei Attacks the Estate
- // Contest: Codeforces - Codeforces Round 1027 (Div. 3)
- // URL: https://codeforces.com/contest/2114/problem/E
- // Memory Limit: 256 MB
- // Time Limit: 2000 ms
- //
- // Powered by CP Editor (https://cpeditor.org)
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #ifndef __DEBUG__
- #define dbg(...) 42
- #endif
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- namespace rngs = std::ranges;
- using ll = long long;
- using a2l = array<ll, 2>;
- using pll = pair<ll, ll>;
- using vl = vector<ll>;
- void solve()
- {
- ll n;
- cin >> n;
- vl a(n), ans, alt(n + 1), dep(n + 1);
- for (auto &x : a)
- cin >> x;
- a.insert(a.begin(), 0ll);
- ans = a;
- vector<vl> g(n + 1);
- for (ll i = 0, u, v; i < n - 1; ++i) {
- cin >> u >> v;
- g[u].push_back(v), g[v].push_back(u);
- }
- auto dfs1 = [&](auto &&self, ll u, ll fa, ll v, ll d) -> void {
- alt[u] = a[u] - v;
- dep[u] = d;
- for (auto v : g[u]) {
- if (v == fa)
- continue;
- self(self, v, u, alt[u], d + 1);
- }
- };
- dfs1(dfs1, 1, -1, 0, 0);
- auto dfs2 = [&](auto &&self, ll u, ll fa, ll val, ll v1, ll v2, ll d) -> void {
- val = a[u] - val;
- ans[u] = max(ans[u], val - v1);
- ans[u] = max(ans[u], val + v2);
- v1 = max(v1, val);
- for (auto v : g[u]) {
- if (v == fa)
- continue;
- self(self, v, u, val, v2, v1, d + 1);
- }
- };
- dfs2(dfs2, 1, -1, 0, 0, 0, 0);
- for (ll i = 1; i <= n; ++i)
- cout << ans[i] << ' ';
- cout << '\n';
- }
- int main(int argc, char **argv)
- {
- std::ios::sync_with_stdio(false);
- std::cin.tie(nullptr);
- ll t;
- cin >> t;
- while (t--)
- solve();
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment