제출 #1112071

#제출 시각아이디문제언어결과실행 시간메모리
1112071FucKanh경주 (Race) (IOI11_race)C++14
100 / 100
585 ms186036 KiB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<long long,long long>

using namespace std;

const ll maxn = 5e5 + 2;

ll sz[maxn],h[maxn],k;
vector<pii> a[maxn];
ll ans = LLONG_MAX;
map<ll,ll> mp[maxn];

void dfs(ll u, ll w, ll pa) {
    assert(h[u]!=0);
    for (pii tmp : a[u]) {
        ll wv,v; tie(v,wv) = tmp;
        if (v==pa) continue;
        h[v] = h[u] + 1;
        dfs(v,w+wv,u);

        mp[v][w+wv] = h[v];

        if (mp[u].size() < mp[v].size()) swap(mp[u],mp[v]);

        for (pii val : mp[v]) {
            if (val.second==0) continue;
            if (val.first - w < k && mp[u][k-val.first+2*w] != 0) {
                ans = min(ans, mp[u][k-val.first+2*w] - h[u] + val.second - h[u]);
            }
        }
        for (pii val : mp[v]) {
            if (val.second==0) continue;
             mp[u][val.first] = mp[u][val.first] == 0 ? val.second : min(val.second,mp[u][val.first]);
        }

        if (mp[u][w + k] != 0) {
            ans = min(ans, mp[u][w+k]-h[u]);
        }
    }

}

int best_path(int _n, int _k, int _h[][2], int _L[])
{
    k = _k;
    for (ll i = 0; i < _n - 1; i++){
        ll x = _h[i][0], y = _h[i][1];
        ll w = _L[i];
        a[x].push_back({y,w});
        a[y].push_back({x,w});
    }
    h[0] = 1;
    dfs(0,0,-1);
    assert(ans > 0);
    return ans == LLONG_MAX ? -1 : ans;
}
//void file(string name) {
//    if (fopen((name + ".inp").c_str(), "r")) {
//        freopen((name + ".inp").c_str(), "r", stdin);
//        freopen((name + ".out").c_str(), "w", stdout);
//    }
//}
//signed main() {
//    file("race");
//    int N,K; cin >> N >> K;
//    int h[N][2],l[N];
//    for (int i = 0; i < N-1; i++) {
//        int x,y,w; cin >> x >> y >> w;
//        h[i][1] = x;
//        h[i][0] = y;
//        l[i] = w;
//    }
//    cout << best_path(N,K,h,l);
//}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...