Submission #954384

#TimeUsernameProblemLanguageResultExecution timeMemory
954384TAhmed33Paths (RMI21_paths)C++98
100 / 100
433 ms46184 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 1e5 + 25; vector <pair <int, ll>> adj[MAXN]; pair <ll, ll> dp[MAXN], dq[MAXN]; ll edge[MAXN]; int n, k; void dfs (int pos, int par) { if ((int)adj[pos].size() == 1) { dp[pos] = {edge[pos], pos}; return; } for (auto j : adj[pos]) { if (j.first == par) continue; edge[j.first] = j.second; dfs(j.first, pos); dp[pos] = max(dp[pos], dp[j.first]); } dp[pos].first += edge[pos]; } void dfs2 (int pos, int par, pair <ll, ll> x = {}) { x.first += edge[pos]; dq[pos] = x; deque <pair <ll, ll>> u; for (auto j : adj[pos]) { if (j.first == par) continue; u.push_back(dp[j.first]); } for (int i = (int)u.size() - 2; i >= 0; i--) u[i] = max(u[i], u[i + 1]); for (auto j : adj[pos]) { if (j.first == par) continue; u.pop_front(); auto g = x; if (!u.empty()) g = max(g, u.front()); dfs2(j.first, pos, g); x = max(x, dp[j.first]); } } struct DS { ll val[MAXN], sum; set <pair <ll, ll>> inside, outside; void init () { for (int i = 1; i <= k; i++) { inside.insert({val[i], i}); } for (int i = k + 1; i <= n; i++) { outside.insert({val[i], i}); } } void upd (int a, ll b) { if (inside.count({val[a], a})) { inside.erase({val[a], a}); sum -= val[a]; } else { outside.erase({val[a], a}); } while ((int)inside.size() < k) { if (outside.empty()) break; auto g = *(--outside.end()); inside.insert(g); sum += g.first; outside.erase(g); } val[a] += b; inside.insert({val[a], a}); sum += val[a]; while ((int)inside.size() > k) { auto g = *(inside.begin()); outside.insert(g); inside.erase(g); sum -= g.first; } } ll get () { return sum; } } cur; ll ans[MAXN]; void dfs3 (int pos, int par) { if (par != -1) { cur.upd(dp[pos].second, -edge[pos]); cur.upd(dq[pos].second, edge[pos]); } ans[pos] = cur.get(); for (auto j : adj[pos]) { if (j.first != par) { dfs3(j.first, pos); } } if (par != -1) { cur.upd(dq[pos].second, -edge[pos]); cur.upd(dp[pos].second, edge[pos]); } } int main () { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; if (n == 1) { cout << "0\n"; return 0; } if (n == 2) { int a, b, c; cin >> a >> b >> c; cout << c << '\n' << c << '\n'; return 0; } for (int i = 1; i < n; i++) { int a, b, c; cin >> a >> b >> c; adj[a].push_back({b, c}); adj[b].push_back({a, c}); } int root = -1; for (int i = 1; i <= n; i++) { if ((int)adj[i].size() >= 2) root = i; } dfs(root, -1); dfs2(root, -1); cur.init(); for (int i = 1; i <= n; i++) { if (i != root) { cur.upd(dp[i].second, edge[i]); } } dfs3(root, -1); for (int i = 1; i <= n; i++) { cout << ans[i] << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...