Submission #1129696

#TimeUsernameProblemLanguageResultExecution timeMemory
1129696sad_hamsterwheePaths (RMI21_paths)C++17
0 / 100
162 ms11900 KiB
#include <string> #include <iostream> #include <queue> #include <math.h> #include <stack> #include <map> #include <algorithm> #include <set> #include <tuple> #include <stack> #include <vector> #include <sstream> #include <deque> #include <string> #include <cstring> #include <cmath> #include <numeric> using namespace std; typedef long long ll; const int maxn = 1e5 + 5; int ch[maxn], son[maxn], ch2[maxn], son2[maxn], n, k; multiset<int> s1, s2; int sum; void add(int val) { if (val == 0) return; s1.insert(val); sum += val; if (s1.size() > k) { int minv = (*s1.begin()); sum -= minv; s1.erase(s1.find(minv)); s2.insert(minv); } } void rem(int val) { if (val == 0) return; if (s1.find(val) != s1.end()) { sum -= val; s1.erase(s1.find(val)); } else { s2.erase(s2.find(val)); return; } if (s1.size() < k && s2.size() > 0) { int maxv = (*s2.rbegin()); s2.erase(s2.find(maxv)); s1.insert(maxv); sum += maxv; } } vector<pair<int, int>> G[maxn]; int ans[maxn]; void dfs1(int node, int par) { for (auto& nxt : G[node]) { int neigh = nxt.first, w = nxt.second; if (neigh == par) continue; dfs1(neigh, node); if (ch[neigh] + w > ch[node]) { son[node] = neigh; ch[node] = ch[neigh] + w; } } for (auto& nxt : G[node]) { int neigh = nxt.first, w = nxt.second; if (neigh == par || neigh == son[node]) continue; if (ch[neigh] + w > ch2[node]) { son2[node] = neigh; ch2[node] = ch[neigh] + w; } add(ch[neigh] + w); } } void dfs2(int node, int par, int parwe) { ans[node] = sum; for (auto& nxt : G[node]) { int neigh = nxt.first, w = nxt.second; if (neigh == par) continue; rem(ch[neigh] + w); add(ch[neigh]); int maxWeight; if (neigh == son[node]) { maxWeight = max(parwe, ch2[node]) + w; } else { maxWeight = max(parwe, ch[node]) + w; } rem(maxWeight - w); add(maxWeight); dfs2(neigh, node, maxWeight); rem(maxWeight); add(maxWeight - w); rem(ch[neigh]); add(ch[neigh] + w); } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (int i = 1; i < n; i++) { int u, v, w; cin >> u >> v >> w; G[u].push_back(make_pair(v, w)); G[v].push_back(make_pair(u, w)); } dfs1(1, 0); add(ch[1]); dfs2(1, 0, 0); for (int i = 1; i <= n; i++) { cout << ans[i] << '\n'; } return 0; }
#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...