# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
555727 | Dan4Life | 경주 (Race) (IOI11_race) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 100100;
ll jmp[maxn][25], dis[maxn], depth[maxn], subtree[maxn], par[maxn];
int answ, KK, cnt[maxn];
set<pair<ll,ll>> adj[maxn];
set<int> sub[maxn];
vector<int> adj2[maxn];
multiset<pair<int,int>> S[maxn];
map<pair<int,int>,int> M;
void dfs(int s, int p, int ok){
subtree[s]=1;
if(ok){
depth[s] = (p==-1?0:depth[p]+1);
dis[s] = (p==-1?0:dis[p]+M[{p,s}]);
jmp[s][0] = p;
}
for(auto x : adj[s]){
int u = x.first;
if(u==p) continue;
dfs(u,s,ok), subtree[s]+=subtree[u];
}
}
int find_centroid(int s, int p, int sz){
for(auto u : adj[s])
if(u.first!=p and subtree[u.first]>sz/2)
return find_centroid(u.first, s, sz);
return s;
}
void make_centroid(int s, int p){
dfs(s,-1,0); int sz = subtree[s];
int centroid = find_centroid(s,-1,sz);
par[centroid]=p;
if(p!=-1) adj2[p].push_back(centroid);
while(!adj[centroid].empty()){
int x = adj[centroid].begin()->first;
adj[x].erase(adj[x].lower_bound({centroid,-1}));
adj[centroid].erase(adj[centroid].begin());
make_centroid(x,centroid);
}
}
int get_path(int x, int k){
for(int j = 0; j <= 18; j++)
if(k&(1<<j) and x!=-1) x = jmp[x][j];
return x;
}
int lca(int a, int b){
if(a==b) return a;
if(jmp[a][0]==jmp[b][0]) return jmp[a][0];
if(depth[a]>depth[b]) swap(a,b);
if(depth[a]<depth[b]) return lca(a,get_path(b,depth[b]-depth[a]));
for(int j = 18; j >= 0; j--)
if(jmp[a][j]!=-1 and jmp[a][j]!=jmp[b][j])
return lca(jmp[a][j], jmp[b][j]);
}
ll dist(int a, int b){
return dis[a]+dis[b]-2*dis[lca(a,b)];
}
int len(int a, int b){
return depth[a]+depth[b]-2*depth[lca(a,b)];
}
void dfs2(int s, int p, int ances, bool ok){
for(auto u : adj2[s]){
if(u==p) continue;
dfs2(u,s,s,0);
dfs2(u,s,s,1);
}
int D = dist(ances,s);
if(K-D>=0 and ok) answ = min(answ, len(ances,s)+cnt[K-D]);
if(!ok) cnt[D] = min(cnt[D], len(ances,s));
}
int best_path(int N, int K, int H[][2], int L[])
{
answ = N+1; KK = K; cnt[0]=0;
for(int i = 1; i <= K; i++) cnt[i]=N+1;
for(int i = 0; i < N-1; i++)
adj[++H[i][0]].insert({++H[i][1],L[i]}),
adj[H[i][1]].insert({H[i][0],L[i]}),
M[{H[i][0],H[i][1]}]=M[{H[i][1],H[i][0]}]=L[i];
memset(jmp, -1, sizeof(jmp));
dfs(1,-1,1); make_centroid(1,-1);
for(int j = 1; j <= 18; j++)
for(int i = 1; i <= N; i++)
if(jmp[i][j-1]!=-1) jmp[i][j] = jmp[jmp[i][j-1]][j-1];
for(int i = 1; i <= N; i++){
int x = i;
while(x!=-1){
S[x].insert({dist(i,x),len(i,x)});
x = par[x];
}
}
dfs2(1,-1,-1,0);
/*
cout << "\n\n";
for(int i = 0; i < N-1; i++)
cout << H[i][0] << " " << H[i][1] << " " << L[i] << "\n";
cout << "\n\n";*/
return (answ==N+1?-1:answ);
}