제출 #945018

#제출 시각아이디문제언어결과실행 시간메모리
945018mannshah1211경주 (Race) (IOI11_race)C++17
9 / 100
134 ms13392 KiB
#include "race.h"
#include "bits/stdc++.h"

int best_path(int N, int K, int H[][2], int L[]) {
  std::vector<std::vector<std::pair<int, long long>>> adj(N);
  for (int i = 0; i < N - 1; i++) {
  	adj[H[i][0]].push_back(std::make_pair(H[i][1], 1ll * L[i]));
  	adj[H[i][1]].push_back(std::make_pair(H[i][0], 1ll * L[i]));
  }
  std::vector<long long> sum(N);
  std::vector<int> dep(N);
  dep[0] = 1;
  auto Dfs = [&](int u, int p, auto&& Dfs) -> void {
  	for (auto& [v, w] : adj[u]) {
  		if (v == p) continue;
  		sum[v] = sum[u] + w;
  		dep[v] = dep[u] + 1;
  		Dfs(v, u, Dfs);
  	}
  };
  Dfs(0, -1, Dfs);
  int mn = N + 1;
  auto small_to_large = [&](int u, int p, auto&& small_to_large) -> std::map<long long, int> {
  	long long need = K + (2 * sum[u]);
  	std::map<long long, int> here;
  	here[sum[u]] = dep[u];
  	for (auto& [v, w] : adj[u]) {
  		if (v == p) continue;
  		std::map<long long, int> child = small_to_large(v, u, small_to_large);
  		if (here.size() < child.size()) std::swap(here, child);
  		for (auto& [s, depth] : child) {
  			if (here[need - s] != 0) {
  				mn = std::min(mn, here[need - s] + depth - (2 * dep[u]));
  			}
  		}
  		for (auto& [s, depth] : child) {
  			if (here[s] == 0) here[s] = depth;
  			else here[s] = std::min(here[s], depth);
  		}
  	}
  	return here;
  };
  small_to_large(0, -1, small_to_large);
  if (mn == N + 1) return -1;
  else return mn;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...