Advertisement
pb_jiang

abc222f wa

Jan 17th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. // Problem: F - Expensive Expense
  2. // Contest: AtCoder - Exawizards Programming Contest 2021๏ผˆAtCoder Beginner Contest 222๏ผ‰
  3. // URL: https://atcoder.jp/contests/abc222/tasks/abc222_f
  4. // Memory Limit: 1024 MB
  5. // Time Limit: 4000 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 p4l = pair<pll, pll>;
  32. using vl = vector<ll>;
  33. using vi = vector<int>;
  34.  
  35. int n;
  36. map<pll, ll> ew;
  37. vl vw;
  38. vl g[200003];
  39. vl ans;
  40.  
  41. p4l dfs(ll u, ll fa, pll lbt, pll lbnt)
  42. {
  43.     // dbg(u, fa);
  44.     if (g[u].size() == 1) {
  45.         return {{vw[u], u}, {0, u}};
  46.     }
  47.     for (auto v : g[u]) {
  48.         if (v == fa)
  49.             continue;
  50.         auto [st, snt] = dfs(v, u, lbt, lbnt);
  51.         ll weight = ew[{min(u, v), max(u, v)}];
  52.         ans[lbt.second] = max(ans[lbt.second], snt.first + weight + lbt.first);
  53.  
  54.         if (weight + st.first > lbt.first) {
  55.             lbt.first = weight + st.first;
  56.             lbt.second = lbt.second;
  57.         }
  58.  
  59.         ans[st.second] = max(ans[st.second], lbnt.first + weight + st.first);
  60.         if (weight + snt.first > lbnt.first) {
  61.             lbnt.first = weight + snt.first;
  62.             lbnt.second = lbnt.second;
  63.         }
  64.     }
  65.     if (u == 1)
  66.         ans[1] = max(ans[1], lbnt.first + vw[1]);
  67.     return {lbt, lbnt};
  68. }
  69.  
  70. int main(int argc, char **argv)
  71. {
  72.     cin >> n;
  73.     vw = vl(n + 1);
  74.     ans = vl(n + 1);
  75.     ll a, b, c;
  76.     for (int i = 1; i < n; ++i)
  77.         cin >> a >> b >> c, g[a].push_back(b), g[b].push_back(a), ew[{min(a, b), max(a, b)}] = c;
  78.     for (int i = 1; i <= n; ++i)
  79.         cin >> vw[i];
  80.  
  81.     dfs(1, -1, {0, 0}, {0, 0});
  82.     for (int i = 1; i <= n; ++i)
  83.         cout << ans[i] << '\n';
  84.     return 0;
  85. };
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement