Submission #28930

#TimeUsernameProblemLanguageResultExecution timeMemory
28930h0ngjun7Race (IOI11_race)C++14
100 / 100
518 ms66484 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, int &L, int &W) {
	d[0] = 0;
	for (auto &y : v[x]) {
		if (y.x == par) continue;
		unordered_map <int, int> d2;
		int L2 = 0, W2 = 0;
		f(y.x, x, d2, L2, W2);
		L2++; W2 += y.w;
		if (d.size() < d2.size()) {
			d.swap(d2);
			swap(L, L2);
			swap(W, W2);
		}
		for (auto &t : d2) {
			if (d.count(K - (t.first + W2 + W))) {
				if (ans == -1 || ans > d[K - (t.first + W2 + W)] + t.second + L + L2)
					ans = d[K - (t.first + W2 + W)] + t.second + L + L2;
			}
		}
		for (auto &t : d2) {
			if (t.first + W2 > K) continue;
			if (t.first + W2 == K) {
				if (ans == -1 || ans > t.second + L2)
					ans = t.second + L2;
			}
			if (d.count(t.first + W2 - W)) d[t.first + W2 - W] = min(d[t.first + W2 - W], t.second + L2 - L);
			else d[t.first + W2 - W] = t.second + L2 - L;
		}
	}
}

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;
	int A = 0, B = 0;
	f(0, -1, d, A, B);
	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...