Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: F - Expensive Expense
- // Contest: AtCoder - Exawizards Programming Contest 2021(AtCoder Beginner Contest 222)
- // URL: https://atcoder.jp/contests/abc222/tasks/abc222_f
- // Memory Limit: 1024 MB
- // Time Limit: 4000 ms
- //
- // Powered by CP Editor (https://cpeditor.org)
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
- template <typename... Args> void logger(string vars, Args &&... values)
- {
- cerr << vars << " = ";
- string delim = "";
- (..., (cerr << delim << values, delim = ", "));
- cerr << endl;
- }
- template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
- template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
- template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
- template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using p4l = pair<pll, pll>;
- using vl = vector<ll>;
- using vi = vector<int>;
- int n;
- map<pll, ll> ew;
- vl vw;
- vl g[200003];
- vl ans;
- vector<pll> top1, top2;
- pll dfs(ll u, ll fa)
- {
- auto &a = top1[u], &b = top2[u];
- for (auto v : g[u]) {
- if (v == fa)
- continue;
- auto [weight, end_vid] = dfs(v, u);
- pll key = {min(u, v), max(u, v)};
- ll new_weight = (ew.count(key) ? ew[key] : 0) + weight;
- if (a.first < new_weight) {
- b = a;
- a.first = new_weight;
- a.second = v;
- } else if (b.first < new_weight) {
- b.first = new_weight;
- b.second = v;
- }
- }
- return a;
- }
- void dfs2(ll u, ll fa, ll lp)
- {
- ans[u] = max(lp, top1[u].first) + vw[u];
- for (auto v : g[u]) {
- if (v == fa)
- continue;
- pll w = (top1[u].second == v ? top2[u] : top1[u]);
- pll key = {min(u, v), max(u, v)};
- pll key2 = {min(u, w.second), max(u, w.second)};
- ll eweight = ew.count(key) ? ew[key] : 0;
- ll eweight2 = ew.count(key2) ? ew[key2] : 0;
- dfs2(v, u, max(lp + eweight, w.first + eweight2));
- }
- }
- int main(int argc, char **argv)
- {
- cin >> n;
- vw = vl(n + 1);
- ans = vl(n + 1);
- top1 = top2 = vector<pll>(n + 1);
- ll a, b, c;
- for (int i = 1; i < n; ++i)
- cin >> a >> b >> c, g[a].push_back(b), g[b].push_back(a), ew[{min(a, b), max(a, b)}] = c;
- for (int i = 1; i <= n; ++i)
- cin >> vw[i];
- dfs(1, -1);
- dfs2(1, -1, 0);
- for (int i = 1; i <= n; ++i)
- cout << ans[i] << '\n';
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement