Submission #1066253

# Submission time Handle Problem Language Result Execution time Memory
1066253 2024-08-19T17:03:07 Z MilosMilutinovic Paths (RMI21_paths) C++14
100 / 100
221 ms 17804 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<vector<pair<int, int>>> g(n);
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    --u; --v;
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
  }
  multiset<long long> L, R;
  long long s = 0;
  vector<long long> mx(n);
  auto Add = [&](long long x) {
    L.insert(x);
    while ((int) R.size() < k && !L.empty()) {
      long long v = *prev(L.end());
      s += v;
      R.insert(v);
      L.erase(L.find(v));
    }
    while (!L.empty() && !R.empty() && *prev(L.end()) > *R.begin()) {
      long long p = *prev(L.end());
      long long q = *R.begin();
      L.insert(q);
      R.insert(p);
      L.erase(L.find(p));
      R.erase(R.find(q));
      s += p - q;
    }
  };
  auto Remove = [&](long long x) {
    if (R.find(x) != R.end()) {
      s -= x;
      R.erase(R.find(x));
    } else {
      L.erase(L.find(x));
    }
    while (R.size() < k && !L.empty()) {
      long long v = *prev(L.end());
      s += v;
      R.insert(v);
      L.erase(L.find(v));
    }
  };
  function<void(int, int)> Dfs = [&](int v, int pv) {
    for (auto& e : g[v]) {
      int u = e.first;
      int w = e.second;
      if (u == pv) {
        continue;
      }
      Dfs(u, v);
      if (mx[u] != 0) {
        Remove(mx[u]);
      }
      Add(mx[u] + w);
      mx[v] = max(mx[v], mx[u] + w);
    }
  };
  Dfs(0, 0);
  vector<long long> res(n);
  function<void(int, int)> Solve = [&](int v, int pv) {
    long long save = mx[v];
    for (auto& e : g[v]) {
      int u = e.first;
      int w = e.second;
      if (u == pv) {
        if (mx[u] != 0) {
          Remove(mx[u]);
        } 
        Add(mx[u] + w);
        mx[v] = max(mx[v], mx[u] + w);
      }
    }
    res[v] = s;
    pair<long long, int> p = {-1, -1};
    pair<long long, int> q = {-1, -1};
    for (auto& e : g[v]) {
      int u = e.first;
      int w = e.second;
      if (p.first < mx[u] + w) {
        q = p;
        p = {mx[u] + w, u};
      } else if (q.first < mx[u] + w) {
        q = {mx[u] + w, u};
      }
    }
    for (auto& e : g[v]) {
      int u = e.first;
      int w = e.second;
      if (u == pv) {
        continue;
      }
      Remove(mx[u] + w);
      Add(mx[u]);
      if (u != p.second) {
        Solve(u, v);
        Remove(mx[u]);
        Add(mx[u] + w);
        continue;
      }
      long long t = mx[v];
      if (q.second != -1) {
        mx[v] = q.first;
      } else {
        mx[v] = 0;
      }
      Solve(u, v);      
      Remove(mx[u]);
      Add(mx[u] + w);
      mx[v] = t;
    }
    for (auto& e : g[v]) {
      int u = e.first;
      int w = e.second;
      if (u == pv) {
        if (mx[u] != 0) {
          Add(mx[u]);
        } 
        Remove(mx[u] + w);
      }
    }
    mx[v] = save;
  };
  Solve(0, 0);
  for (int i = 0; i < n; i++) {
    cout << res[i] << '\n';
  }
  return 0; 
}

Compilation message

Main.cpp: In lambda function:
Main.cpp:46:21: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   46 |     while (R.size() < k && !L.empty()) {
      |            ~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
13 Correct 6 ms 604 KB Output is correct
14 Correct 3 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 3 ms 540 KB Output is correct
17 Correct 3 ms 604 KB Output is correct
18 Correct 2 ms 580 KB Output is correct
19 Correct 3 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 11604 KB Output is correct
2 Correct 142 ms 14676 KB Output is correct
3 Correct 117 ms 9296 KB Output is correct
4 Correct 147 ms 11600 KB Output is correct
5 Correct 143 ms 12880 KB Output is correct
6 Correct 150 ms 11660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
13 Correct 6 ms 604 KB Output is correct
14 Correct 3 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 3 ms 540 KB Output is correct
17 Correct 3 ms 604 KB Output is correct
18 Correct 2 ms 580 KB Output is correct
19 Correct 3 ms 604 KB Output is correct
20 Correct 152 ms 11604 KB Output is correct
21 Correct 142 ms 14676 KB Output is correct
22 Correct 117 ms 9296 KB Output is correct
23 Correct 147 ms 11600 KB Output is correct
24 Correct 143 ms 12880 KB Output is correct
25 Correct 150 ms 11660 KB Output is correct
26 Correct 212 ms 11856 KB Output is correct
27 Correct 183 ms 16720 KB Output is correct
28 Correct 211 ms 17484 KB Output is correct
29 Correct 129 ms 11604 KB Output is correct
30 Correct 213 ms 14028 KB Output is correct
31 Correct 221 ms 12368 KB Output is correct
32 Correct 220 ms 15188 KB Output is correct
33 Correct 210 ms 14000 KB Output is correct
34 Correct 120 ms 11184 KB Output is correct
35 Correct 206 ms 14032 KB Output is correct
36 Correct 200 ms 17804 KB Output is correct