제출 #1365508

#제출 시각아이디문제언어결과실행 시간메모리
1365508lucaskojimaPetrol stations (CEOI24_stations)C++20
0 / 100
2540 ms2118968 KiB
#include "bits/stdc++.h"

#define int long long

using namespace std;

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);

  int N, K; cin >> N >> K;

  vector<vector<int>> adj(N + 1);
  map<pair<int, int>, int> ed;

  for (int i = 0; i < N - 1; i++) {
    int a, b, c; cin >> a >> b >> c; a++, b++;
    adj[a].push_back(b);
    adj[b].push_back(a);
    ed[{a, b}] = ed[{b, a}] = c;
  }

  vector<int> pai(N + 1), dep(N + 1);

  auto dfs = [&](auto dfs, int x, int p) -> void {
    for (int k : adj[x]) if (k != p) {
      pai[k] = x;
      dep[k] = dep[x] + 1;
      dfs(dfs, k, x);
    }
  };
  dfs(dfs, 1, -1);

  vector<int> ans(N + 1);
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) {
      if (dep[i] < dep[j]) continue;

      vector<int> path = {i};
      while (path.back() != j)
        path.push_back(pai[path.back()]);

      int sum = 0;
      for (int k = 0; k < int32_t(size(path)) - 1; k++) {
        sum += ed[{path[k], path[k + 1]}];
        if (sum > K) {
          sum = ed[{path[k], path[k + 1]}];
          ans[path[k]]++;
        }
      }

      reverse(path.begin(), path.end());

      sum = 0;
      for (int k = 0; k < int32_t(size(path)) - 1; k++) {
        sum += ed[{path[k], path[k + 1]}];
        if (sum > K) {
          sum = ed[{path[k], path[k + 1]}];
          ans[path[k]]++;
        }
      }
    }

  for (int i = 1; i <= N; i++)
    cout << ans[i] << '\n';
  return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…