| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 492540 | 1ne | 경주 (Race) (IOI11_race) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
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;
}
