Submission #807216

#TimeUsernameProblemLanguageResultExecution timeMemory
807216vjudge1Race (IOI11_race)C++17
21 / 100
3066 ms30720 KiB
#include <bits/stdc++.h>
#include "race.h"

using namespace std;

const int maxn = 1e6 + 5;
const int inf = 1e9 + 7;

int depth[maxn];
int road[maxn];
int n, k;
vector<pair<int, int>> tr[maxn];

void dfs(int u, int p){
	for(auto node : tr[u]){
		int v = node.first;
		int w = node.second;
		if(v == p) continue;
		depth[v] = depth[u] + 1;
		road[v] = road[u] + w;
		dfs(v, u);
	}
}

int best_path(int N, int K, int H[][2], int L[]){
	n = N;
	k = K;
	for(int i = 0; i < n - 1; i++){
		int a, b, l;
		a = H[i][0];
		b = H[i][1];
		l = L[i];
		tr[a].push_back({b, l});
		tr[b].push_back({a, l});
	}

	int result = inf;
	for(int i = 0; i < n; i++){
		depth[i] = 0;
		road[i] = 0;
		dfs(i, -1);
		for(int j = 0; j < n; j++){
			if(road[j] == k && result > depth[j]){
				result = depth[j];
			}
		}
	}

	if(result == inf){
		return -1;
	}else{
		return result;
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...