Advertisement
pb_jiang

abc222f wa

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