Submission #492541

#TimeUsernameProblemLanguageResultExecution timeMemory
4925411neRace (IOI11_race)C++14
21 / 100
3059 ms21648 KiB
#include "race.h"
#include<bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>>adj(1e5);
vector<vector<int>>sparce(1e5,vector<int>(20,-1));
vector<int>depth(1e5,0);
vector<int64_t>dist(1e5,0);

void getsparce(int u,int par){
	for (auto x:adj[u]){
		if (x.first!=par){
			sparce[x.first][0] = u;
			depth[x.first]=depth[u]+1;
			dist[x.first] = dist[u] + x.second;
			getsparce(x.first,u);
		}
	}
}
int lca(int u,int v){
	if (depth[u]<depth[v]){
		swap(u,v);
	}
	for (int i = 19;i>=0;--i){
		if (sparce[u][i]!=-1)
		if (depth[sparce[u][i]]>=depth[v]){
			u=sparce[u][i];
		}
	}
	if (u==v)return u;
	for (int i = 19;i>=0;--i){
		if (sparce[u][i]!=sparce[v][i]){
			u=sparce[u][i];
			v=sparce[v][i];
		}
	}
	return sparce[u][0];
}
pair<int,int64_t> distt(int u,int v){
	int l = lca(u,v);
	return {depth[u] + depth[v] - 2*depth[l],dist[u] + dist[v] - 2*dist[l]};
}
void preprocess(int n){
	getsparce(0,-1);
	for (int i = 1;i<20;++i){
		for (int j = 0;j<n;++j){
			if (sparce[j][i-1]!=-1){
				sparce[j][i] = sparce[sparce[j][i-1]][i-1];
			}
		}
	}
}
int best_path(int n, int k, int H[][2], int L[])
{
	for (int i = 0;i<n-1;++i){
	   adj[H[i][0]].push_back({H[i][1],L[i]});
	   adj[H[i][1]].push_back({H[i][0],L[i]});	
	}
	preprocess(n);
	int ans = INT_MAX;
	for (int i = 0;i<n;++i){
		for (int j = i+1;j<n;++j){
			auto x = distt(i,j);
			if (x.second==k){
				ans = min(ans,x.first);
			}
		}
	}
	if (ans==INT_MAX){
		ans = -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...