Advertisement
pb_jiang

ABC222F AC

Jan 19th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 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. vector<pll> top1, top2;
  41.  
  42. pll dfs(ll u, ll fa)
  43. {
  44.     for (auto v : g[u]) {
  45.         if (v == fa)
  46.             continue;
  47.         auto [weight, end_vid] = dfs(v, u);
  48.         pll key = {min(u, v), max(u, v)};
  49.         auto &a = top1[u], &b = top2[u];
  50.  
  51.         ll r = max(weight, vw[v]) + ew[key];
  52.         if (r > a.first) {
  53.             b = a;
  54.             a = {r, v};
  55.         } else if (r > b.first) {
  56.             b = {r, v};
  57.         }
  58.         /*
  59.         ll new_weight = ew[key] + weight;
  60.         if (a.first < new_weight) {
  61.             b = a;
  62.             a.first = new_weight;
  63.             a.second = v;
  64.         } else if (b.first < new_weight) {
  65.             b.first = new_weight;
  66.             b.second = v;
  67.         }
  68.         */
  69.     }
  70.     return top1[u];
  71. }
  72.  
  73. void dfs2(ll u, ll fa, ll lp)
  74. {
  75.     ans[u] = max(lp, top1[u].first);
  76.     for (auto v : g[u]) {
  77.         if (v == fa)
  78.             continue;
  79.         pll key = {min(u, v), max(u, v)};
  80.         ll uv_dist = ew[key];
  81.         // ll this_lp = 0;
  82.         // dbg(u, v, top1[u].first, top2[u].first, top1[u].second, top2[u].second, lp);
  83.         ll this_lp = max(lp, vw[u]) + uv_dist;
  84.         if (top1[u].second == v) {
  85.             this_lp = max(this_lp, top2[u].first + uv_dist);
  86.         } else {
  87.             this_lp = max(this_lp, top1[u].first + uv_dist);
  88.         }
  89.         dfs2(v, u, this_lp);
  90.     }
  91. }
  92.  
  93. int main(int argc, char **argv)
  94. {
  95.     cin >> n;
  96.     vw = vl(n + 1);
  97.     ans = vl(n + 1);
  98.     top1 = top2 = vector<pll>(n + 1);
  99.     ll a, b, c;
  100.     for (int i = 1; i < n; ++i)
  101.         cin >> a >> b >> c, g[a].push_back(b), g[b].push_back(a), ew[{min(a, b), max(a, b)}] = c;
  102.     for (int i = 1; i <= n; ++i)
  103.         cin >> vw[i];
  104.  
  105.     dfs(1, -1);
  106.     dfs2(1, -1, 0);
  107.     for (int i = 1; i <= n; ++i)
  108.         cout << ans[i] << '\n';
  109.     return 0;
  110. };
  111. A
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement