Submission #320087

#TimeUsernameProblemLanguageResultExecution timeMemory
320087nekiRace (IOI11_race)C++14
100 / 100
656 ms42568 KiB
#include "race.h"
#include <bits/stdc++.h>
#define loop(i, a, b) for(long long i=a;i<b;i++)
#define pool(i, a, b) for(long long i=a-1;i>=b;i--)
#define fore(i, a) for(auto&& i:a)
#define fi first
#define se second
#define ps(a) push_back(a)
#define pb(a) pop_back(a)
#define sc scanf
#define vc vector
#define pa pair<ll, ll>
#define ll int
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define llmax LLONG_MAX/2
#define llmin -LLONG_MAX/2
using namespace std;
#define mn 201000
#define pa pair<ll, ll>
#define ld long double 
#define intmax INT_MAX/2
vc<pa> edg[mn]; 
ll n, k, vis[mn], ans=intmax, dp[1000100], sz[mn];

ll fnd(ll st){
    function<void (ll, ll)> psa=[&](ll u, ll p){
        sz[u]=1;
        fore(v, edg[u]) if(!vis[v.fi] and v.fi!=p) psa(v.fi, u), sz[u]+=sz[v.fi];
    };
    psa(st, -1);
    ll nsiz=sz[st];
    function<ll (ll, ll)> dfs=[&](ll u, ll p){
        fore(v, edg[u]) if(!vis[v.fi] and v.fi!=p and sz[v.fi]>nsiz/2) return dfs(v.fi, u);
        return u;
    };
    return dfs(st, -1);
}

vc<pa> cur, al;
void getd(ll u, ll p, ll dist, ll le){
    cur.ps(make_pair(dist, le));
    al.ps(make_pair(dist, le));
    fore(v, edg[u]) if(!vis[v.fi] and v.fi!=p) getd(v.fi, u, dist+v.se, le+1);
}

void dec(ll st){
    ll c=fnd(st);
    dp[0]=0;
    al.clear();
    fore(v, edg[c]) if(!vis[v.fi]){
        cur.clear();
        getd(v.fi, c, v.se, 1);
        
        fore(d, cur) if(0<=k-d.fi)ans=min(ans, dp[k-d.fi]+d.se);
        fore(d, cur) if(d.fi<=k)dp[d.fi]=min(dp[d.fi], d.se);

    }
    fore(d, al) if(d.fi<=k)dp[d.fi]=intmax;
    
    vis[c]=1;
    fore(v, edg[c]) if(!vis[v.fi]) dec(v.fi);
}
ll best_path(ll N, ll K, ll h[][2], ll l[]){
    n=N, k=K;
    loop(i, 0, k+1) dp[i]=intmax;
    dp[0]=0;
    loop(i, 0, n-1){
        edg[h[i][0]].ps(make_pair(h[i][1], l[i]));
        edg[h[i][1]].ps(make_pair(h[i][0], l[i]));
    }
    dec(0);
    return (ans>n+539)? -1: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...