제출 #1067149

#제출 시각아이디문제언어결과실행 시간메모리
1067149TrinhKhanhDung경주 (Race) (IOI11_race)C++14
100 / 100
297 ms37204 KiB
#include <bits/stdc++.h>
#include "race.h"
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 19972207)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

const int MAX = 2e5 + 10, MAXVAL = 1e6 + 10;

int N, K;
vector<pair<int, int>> adj[MAX];
int nChild[MAX], ans = INF, dp[MAXVAL];
bool isDel[MAX];

void input(){
    cin >> N >> K;
    for(int i=1; i<N; i++){
        int u, v, c;
        cin >> u >> v >> c;
        adj[u].push_back(make_pair(v, c));
        adj[v].push_back(make_pair(u, c));
    }
}

void countChild(int u, int p){
    nChild[u] = 1;
    for(auto o: adj[u]){
        int v = o.fi;
        if(v != p && !isDel[v]){
            countChild(v, u);
            nChild[u] += nChild[v];
        }
    }
}

int centroid(int u, int p, int n){
    for(auto o: adj[u]){
        int v = o.fi;
        if(v != p && !isDel[v]){
            if(nChild[v] * 2 > n) return centroid(v, u, n);
        }
    }
    return u;
}

void dfs(int u, int p, int dist, int height, bool filling){

    if(dist > K) return;
    if(!filling){
        minimize(ans, dp[K - dist] + height);
    }
    else{
        minimize(dp[dist], height);
    }
    for(auto o: adj[u]){
        int v = o.fi;
        int newDist = dist + o.se;
        if(v != p && !isDel[v])
            dfs(v, u, newDist, height + 1, filling);
    }
}

void reset(int u, int p, int dist){
    if(dist > K) return;
    dp[dist] = INF;
    for(auto o: adj[u]){
        int v = o.fi;
        int newDist = dist + o.se;
        if(v != p && !isDel[v])
            reset(v, u, newDist);
    }
}

void calc(int ver){
    countChild(ver, 0);
    int u = centroid(ver, 0, nChild[ver]);
    
    dp[0] = 0;
    for(auto o: adj[u]){
        int v = o.fi;
        int dist = o.se;
        if(!isDel[v]){
            dfs(v, u, dist, 1, 0);
            dfs(v, u, dist, 1, 1);
        }
    }
    reset(u, 0, 0);

    isDel[u] = true;
    for(auto o: adj[u]){
        int v = o.fi;
        if(!isDel[v]) calc(v);
    }
}

int best_path(int n, int k, int H[][2], int L[]){
    N = n;
    K = k;
    for(int i=0; i+1<N; i++){
        int u = H[i][0];
        int v = H[i][1];
        int c = L[i];
        adj[u].push_back(make_pair(v, c));
        adj[v].push_back(make_pair(u, c));
    }
    memset(dp, 0x3f, sizeof dp);  
    calc(0); 
    return (ans >= INF ? -1 : ans);
}

// void solve(){
    
// }

// int main(){
//     ios_base::sync_with_stdio(0); cin.tie(0);
// //    freopen("mmgraph.inp", "r", stdin);
// //    freopen("mmgraph.out", "w", stdout);

//     int t = 1;
// //    cin >> t;
//     while(t--){
//         input();
//         solve();
//     }

//     return 0;
// }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...