제출 #637841

#제출 시각아이디문제언어결과실행 시간메모리
637841erto경주 (Race) (IOI11_race)C++17
43 / 100
319 ms86732 KiB
#include <bits/stdc++.h>
#include "race.h"
typedef long long int ll;
#define INF (int)(1e9 + 7)
#define INF2 998244353
#define N (ll)(2e5 + 5)
using namespace std;
//#define int ll
#define lsb(x) (x & (-x))
 
ll n, k;
ll g;
ll ans = INF;
vector<pair<ll, ll>> v[N];
set<pair<ll, ll>> s[N];
ll d[N];
ll dis[N];
 
void dfs(int x, int p){
    for(auto u : v[x]){
        if(u.first != p){
            d[u.first] = d[x] + 1;
            dis[u.first] = dis[x] + u.second;
            dfs(u.first, x);
        }
    }
 
    s[x].insert({dis[x], d[x]});
 
    for(auto u : v[x]){
        if(u.first != p){
            if(s[x].size() < s[u.first].size()){
                swap(s[x], s[u.first]);
            }
 
            for(auto j : s[u.first]){
                g = k - (j.first - dis[x]) + dis[x];
                auto it = s[x].lower_bound(make_pair(g, 0ll));
                if(it != s[x].end() && it->first == g){
                    ans = min(ans, j.second + it->second - 2 * d[x]);
                }
                s[x].insert(j);
            }
        }
    }
}
 
int best_path(int nn, int K, int H[][2], int L[]){
    n = nn;
    for(int i=0; i<n-1; i++){
        v[H[i][0]].push_back({H[i][1], L[i]});
        v[H[i][1]].push_back({H[i][0], L[i]});
    }
 
    k=K;
    dfs(0, -1);
    if(ans == INF)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...