Submission #28928

#TimeUsernameProblemLanguageResultExecution timeMemory
28928h0ngjun7Race (IOI11_race)C++14
43 / 100
3057 ms69976 KiB
#include "race.h"
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;

const int MAXN = 200005;
struct EDGE { int x, w; };
int N, K, ans = -1;
vector <EDGE> v[MAXN];

void f(int x, int par, unordered_map <int, int> &d) {
	d[0] = 0;
	for (auto &y : v[x]) {
		if (y.x == par) continue;
		unordered_map <int, int> d2, d3;
		f(y.x, x, d2);
		for (auto &t : d2) {
			if (t.first + y.w > K) continue;
			d3[t.first + y.w] = t.second + 1;
		}
		d2.clear();
		if (d.size() < d3.size()) d.swap(d3);
		for (auto &t : d3) {
			if (d.count(K - t.first)) {
				if (ans == -1 || ans > d[K - t.first] + t.second)
					ans = d[K - t.first] + t.second;
			}
		}
		for (auto &t : d3) {
			if (t.first == K) {
				if (ans == -1 || ans > t.second)
					ans = t.second;
			}
			if (d.count(t.first)) d[t.first] = min(d[t.first], t.second);
			else d[t.first] = t.second;
		}
	}
}

int best_path(int _N, int _K, int H[][2], int L[]) {
	N = _N, K = _K;
	for (int i = 0; i < N - 1; i++) {
		v[H[i][0]].push_back({ H[i][1], L[i] });
		v[H[i][1]].push_back({ H[i][0], L[i] });
	}
	unordered_map <int, int> d;
	f(0, -1, d);
	return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...