Submission #1112287

# Submission time Handle Problem Language Result Execution time Memory
1112287 2024-11-14T02:47:07 Z fryingduc Paths (RMI21_paths) C++17
100 / 100
208 ms 16464 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef duc_debug
#include "bits/debug.h"
#else 
#define debug(...)      
#endif
 
 const int maxn = 1e5 + 5;
int n, k;
int a[maxn];
vector<pair<int, int>> g[maxn];
long long res[maxn];
long long mx[maxn];
multiset<long long> st;
multiset<long long, greater<long long>> ot;
 
long long cur;
void insert(long long x) {
  st.insert(x);
  cur += x;
  if((int)st.size() > k) {
    cur -= *st.begin();
    ot.insert(*st.begin());
    st.erase(st.begin());
  }
}
void erase(long long x) {
  auto it = st.find(x);
  if(it != st.end()) {
    cur -= x;
    st.erase(it);
    if (!ot.empty()) {
      cur += *ot.begin();
      st.insert(*ot.begin());
      ot.erase(ot.begin());
    }
  } else {
    //if (ot.find(x) == ot.end()) return;
    ot.erase(ot.find(x));
  }
}
void dfs(int u, int prev) {
  mx[u] = 0;
  bool leaf = 1;
  for(auto e:g[u]) {
    int v = e.first, w = e.second;
    if (v == prev) continue;
    leaf = 0;
    dfs(v, u);
    erase(mx[v]);
    insert(mx[v] + w);
    mx[u] = max(mx[u], mx[v] + w);
  }
  if(leaf) {
    insert(0);
  }
}
void reroot(int u, int prev) {
  pair<long long, int> fmx = make_pair(0ll, u);
  pair<long long, int> smx = make_pair(0ll, u);
  for(auto e:g[u]) {
    int v = e.first, w = e.second;
    if(mx[v] + w >= fmx.first) {
        smx = fmx;
        fmx = make_pair(mx[v] + w, v);
    }
    else if(mx[v] + w > smx.first) {
      smx = make_pair(mx[v] + w, v);
    }
  }
  
  res[u] = cur;
  
  for(auto e:g[u]) {
    int v = e.first, w = e.second;
    if (v == prev) continue;
    insert(mx[v]);
    erase(mx[v] + w);
    long long  omx = mx[u];
    if(v == fmx.second) {
      mx[u] = smx.first;
    } else {
      mx[u] = fmx.first;
    }
    insert(mx[u] + w);
    
    if(mx[u] > 0) {
      erase(mx[u]);
    }
    
    reroot(v, u);
    
    if(mx[u] > 0) {
      insert(mx[u]);
    }
    erase(mx[u] + w);
    
    mx[u] = omx;
    insert(mx[v] + w);
    erase(mx[v]);
  }
}
void solve() {
  cin >> n >> k;
  for(int i = 1; i < n; ++i) {
    int u, v, w; cin >> u >> v >> w;
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
  }
  dfs(1, 0);
  reroot(1, 0);
  for(int i = 1; i <= n; ++i) {
    cout << res[i] << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  
  solve();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 2 ms 4604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 2 ms 4604 KB Output is correct
3 Correct 2 ms 4432 KB Output is correct
4 Correct 2 ms 4572 KB Output is correct
5 Correct 2 ms 4432 KB Output is correct
6 Correct 2 ms 4432 KB Output is correct
7 Correct 2 ms 4600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 2 ms 4604 KB Output is correct
3 Correct 2 ms 4432 KB Output is correct
4 Correct 2 ms 4572 KB Output is correct
5 Correct 2 ms 4432 KB Output is correct
6 Correct 2 ms 4432 KB Output is correct
7 Correct 2 ms 4600 KB Output is correct
8 Correct 4 ms 4432 KB Output is correct
9 Correct 2 ms 4680 KB Output is correct
10 Correct 2 ms 4432 KB Output is correct
11 Correct 2 ms 4688 KB Output is correct
12 Correct 3 ms 4816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 2 ms 4604 KB Output is correct
3 Correct 2 ms 4432 KB Output is correct
4 Correct 2 ms 4572 KB Output is correct
5 Correct 2 ms 4432 KB Output is correct
6 Correct 2 ms 4432 KB Output is correct
7 Correct 2 ms 4600 KB Output is correct
8 Correct 4 ms 4432 KB Output is correct
9 Correct 2 ms 4680 KB Output is correct
10 Correct 2 ms 4432 KB Output is correct
11 Correct 2 ms 4688 KB Output is correct
12 Correct 3 ms 4816 KB Output is correct
13 Correct 4 ms 4700 KB Output is correct
14 Correct 4 ms 4688 KB Output is correct
15 Correct 3 ms 4688 KB Output is correct
16 Correct 3 ms 4688 KB Output is correct
17 Correct 3 ms 4688 KB Output is correct
18 Correct 3 ms 4688 KB Output is correct
19 Correct 5 ms 4688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 160 ms 13896 KB Output is correct
2 Correct 134 ms 15944 KB Output is correct
3 Correct 122 ms 11600 KB Output is correct
4 Correct 140 ms 13908 KB Output is correct
5 Correct 143 ms 14920 KB Output is correct
6 Correct 143 ms 14020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 2 ms 4604 KB Output is correct
3 Correct 2 ms 4432 KB Output is correct
4 Correct 2 ms 4572 KB Output is correct
5 Correct 2 ms 4432 KB Output is correct
6 Correct 2 ms 4432 KB Output is correct
7 Correct 2 ms 4600 KB Output is correct
8 Correct 4 ms 4432 KB Output is correct
9 Correct 2 ms 4680 KB Output is correct
10 Correct 2 ms 4432 KB Output is correct
11 Correct 2 ms 4688 KB Output is correct
12 Correct 3 ms 4816 KB Output is correct
13 Correct 4 ms 4700 KB Output is correct
14 Correct 4 ms 4688 KB Output is correct
15 Correct 3 ms 4688 KB Output is correct
16 Correct 3 ms 4688 KB Output is correct
17 Correct 3 ms 4688 KB Output is correct
18 Correct 3 ms 4688 KB Output is correct
19 Correct 5 ms 4688 KB Output is correct
20 Correct 160 ms 13896 KB Output is correct
21 Correct 134 ms 15944 KB Output is correct
22 Correct 122 ms 11600 KB Output is correct
23 Correct 140 ms 13908 KB Output is correct
24 Correct 143 ms 14920 KB Output is correct
25 Correct 143 ms 14020 KB Output is correct
26 Correct 186 ms 14408 KB Output is correct
27 Correct 174 ms 15900 KB Output is correct
28 Correct 158 ms 16416 KB Output is correct
29 Correct 117 ms 11848 KB Output is correct
30 Correct 186 ms 14312 KB Output is correct
31 Correct 187 ms 12616 KB Output is correct
32 Correct 167 ms 15180 KB Output is correct
33 Correct 208 ms 14408 KB Output is correct
34 Correct 103 ms 11496 KB Output is correct
35 Correct 168 ms 14532 KB Output is correct
36 Correct 160 ms 16464 KB Output is correct