답안 #1066230

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1066230 2024-08-19T16:43:40 Z MilosMilutinovic Paths (RMI21_paths) C++14
68 / 100
600 ms 16712 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> st;
  vector<long long> mx(n);
  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) {
        st.erase(st.find(mx[u]));
      }
      st.insert(mx[u] + w);
      mx[v] = max(mx[v], mx[u] + w);
    }
  };
  Dfs(0, 0);
  auto Calc = [&]() {
    long long s = 0;
    auto it = st.end();
    for (int i = 0; i < k && it != st.begin(); i++) {
      it = prev(it);
      s += *it;
    }
    return s;
  }; 
  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) {
          st.erase(st.find(mx[u]));
        } 
        st.insert(mx[u] + w);
        mx[v] = max(mx[v], mx[u] + w);
      }
    }
    res[v] = Calc();
    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;
      }
      st.erase(st.find(mx[u] + w));
      st.insert(mx[u]);
      if (u != p.second) {
        Solve(u, v);
        st.erase(st.find(mx[u]));
        st.insert(mx[u] + w);
        continue;
      }
      long long t = mx[v];
      if (q.second != -1) {
        mx[v] = q.first;
      } else {
        mx[v] = 0;
      }
      Solve(u, v);      
      st.erase(st.find(mx[u]));
      st.insert(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) {
          st.insert(mx[u]);
        } 
        st.erase(st.find(mx[u] + w));
      }
    }
    mx[v] = save;
  };
  Solve(0, 0);
  for (int i = 0; i < n; i++) {
    cout << res[i] << '\n';
  }
  return 0; 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 460 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 460 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 624 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 460 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 624 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 8 ms 584 KB Output is correct
14 Correct 3 ms 604 KB Output is correct
15 Correct 2 ms 696 KB Output is correct
16 Correct 7 ms 724 KB Output is correct
17 Correct 5 ms 604 KB Output is correct
18 Correct 2 ms 652 KB Output is correct
19 Correct 11 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 13508 KB Output is correct
2 Correct 137 ms 16712 KB Output is correct
3 Correct 116 ms 11240 KB Output is correct
4 Correct 144 ms 13596 KB Output is correct
5 Correct 138 ms 15008 KB Output is correct
6 Correct 151 ms 13792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 460 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 624 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 8 ms 584 KB Output is correct
14 Correct 3 ms 604 KB Output is correct
15 Correct 2 ms 696 KB Output is correct
16 Correct 7 ms 724 KB Output is correct
17 Correct 5 ms 604 KB Output is correct
18 Correct 2 ms 652 KB Output is correct
19 Correct 11 ms 604 KB Output is correct
20 Correct 152 ms 13508 KB Output is correct
21 Correct 137 ms 16712 KB Output is correct
22 Correct 116 ms 11240 KB Output is correct
23 Correct 144 ms 13596 KB Output is correct
24 Correct 138 ms 15008 KB Output is correct
25 Correct 151 ms 13792 KB Output is correct
26 Execution timed out 1083 ms 12416 KB Time limit exceeded
27 Halted 0 ms 0 KB -