Submission #750923

#TimeUsernameProblemLanguageResultExecution timeMemory
750923LCJLYRace (IOI11_race)C++14
9 / 100
193 ms51876 KiB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int>pii;

vector<pii>adj[200005];
multiset<pii>storage[200005];
int offset[200005];
int dist[200005];
int d[200005]; 
int k;
int ans=INT_MAX;

void dp(int index, int par){
	//cout << index << " visit\n";
	storage[index].insert({dist[index],d[index]});
	for(auto it:adj[index]){
		if(it.first==par) continue;
		dist[it.first]=dist[index]+it.second;
		d[it.first]=d[index]+1;
		//cout << index << " " << it.first << " call\n";
		dp(it.first,index);
		//cout << index << " " << it.first << " check\n";
		//swap
		if(storage[it.first].size()>storage[index].size()){
			swap(storage[it.first],storage[index]);
		}
		
		//check
		for(auto it2:storage[it.first]){
			int hold=k-it2.first+dist[index]*2;
			//cout << hold << " target\n";
			pii hold2=*storage[index].lower_bound({hold,0});
			if(hold2.first==hold){
				ans=min(ans,hold2.second+it2.second-d[index]*2);
				//cout << hold2.second<< " " << it2.second << " " << d[index] << "trigger\n";
			}
		}
		
		//merge
		for(auto it2:storage[it.first]){
			storage[index].insert(it2);
		}
	}
	//cout << ans << " " << index << " done\n";
}


int best_path(int n, int K, int H[][2], int L[]){
	k=K;
	
	for(int x=0;x<n-1;x++){
		adj[H[x][0]].push_back({H[x][1],L[x]});
		adj[H[x][1]].push_back({H[x][0],L[x]});
		//cout << H[x][0] << " " << H[x][1] << " edge\n";
	}
	
	dp(0,-1);
	if(ans==INT_MAX) return -1;
	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...