# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
992422 | AbodeKu | Race (IOI11_race) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define INFLL 1e9
#define rep(f, s, i) for (int i = f; (int) i < s; i++)
const int N = 2e5 + 9 ;
struct edge{
int u , v , w ;
};
vector<pair<int , int>> adj[N];
vector<edge>e(N);
int ans = INFLL ;
struct Centroid{
vector<int> sub , removed ;
map<int,int> mn_sub , mn ;
int n , sz , k ;
Centroid(int n , int k): n(n) , k(k){
sub = vector<int>(n + 1) ;
removed = vector<int>(n + 1);
}
void build(int node = 1){
int centroid = find_centroid(node);
removed[centroid] = true ;
mn.clear();
mn[0] = 0 ;
for(auto [nd , w] : adj[centroid]){
if(removed[nd])continue ;
mn_sub.clear();
dfs_mn(nd , centroid , w);
for(auto [key , v] : mn_sub){
if(mn.find(k - key) != mn.end()){
ans = min(ans , mn_sub[key] + mn[k - key]);
}
}
for(auto [key , v] : mn_sub){
if(mn.find(key) == mn.end()) mn[key] = v ;
else mn[key] = min(mn[key] , v);
}
}
mn.clear();
for(auto [nd , w] : adj[centroid]){
if(!removed[nd]) build(nd);
}
}
int find_centroid(int node){
sz = dfs_sub(node , node) ;
return dfs_centroid(node , node) ;
}
int dfs_sub(int node , int par){
sub[node] = 1 ;
for(auto [nd , w] : adj[node]){
if(nd == par || removed[nd]) continue ;
sub[node] += dfs_sub(nd , node);
}
return sub[node] ;
}
int dfs_centroid(int node , int par){
for(auto [nd , w] : adj[node]){
if(nd == par || removed[nd]) continue ;
if(sub[nd] * 2 > sz) return dfs_centroid(nd , node);
}
return node ;
}
void dfs_mn(int node , int par , int c , int depth = 1){
if(c > k) return ;
if(mn_sub.find(c) == mn_sub.end()) mn_sub[c] = depth ;
else mn_sub[c] = min(mn_sub[c] , depth);
for(auto [nd , w] : adj[node]){
if(nd == par || removed[nd]) continue ;
dfs_mn(nd , node , c + w , depth + 1);
}
}
};
int best_bath(int n , int k , int h[][2] , int weights[]){
rep(0 , n-1 , i){
int u = h[i][0] , v = h[i][1] ;
e[i] = {u , v , 0};
}
rep(0 , n-1 , i){
int w = weights[i];
e[i].w = w ;
auto [u , v , c] = e[i];
adj[u].push_back(mp(v , w));
adj[v].push_back(mp(u , w));
}
Centroid c(n , k);
c.build();
if(ans == INFLL) return -1;
return ans ;
}