Submission #868936

#TimeUsernameProblemLanguageResultExecution timeMemory
868936TB_Race (IOI11_race)C++17
100 / 100
264 ms50260 KiB
#include <bits/stdc++.h>
using namespace std;

#undef _GLIBCXX_DEBUG                // disable run-time bound checking, etc
#pragma GCC optimize("Ofast,inline") // Ofast = O3,fast-math,allow-store-data-races,no-protect-parens
#pragma GCC optimize ("unroll-loops")

// #pragma GCC target("bmi,bmi2,lzcnt,popcnt")                      // bit manipulation
// #pragma GCC target("movbe")                                      // byte swap
// #pragma GCC target("aes,pclmul,rdrnd")                           // encryption
// #pragma GCC target("avx,avx2,f16c,fma,sse3,ssse3,sse4.1,sse4.2") // SIMD

// #include <bits/extc++.h>
// using namespace __gnu_pbds;
// template<class T>using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
// template<class T>using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define ll long long
#define INF (ll)1e16+7
#define fo(i, n) for(ll i=0;i<((ll)n);i++)
#define deb(x) cout << #x << " = " << (x) << endl;
#define deb2(x, y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define LSOne(S) ((S) & (-S))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
inline int readint(){ int v = 0; char c; while((c = getchar()) != EOF && c != ' ' && c != '\n'){ v *= 10; v += c - '0'; } return v; }
inline int readintsigned() { int v = 0; int sign = 1; char c = getchar(); if (c == '-') { sign = -1; } else { v += c - '0'; } while ((c = getchar()) != EOF && c != ' ' && c != '\n') { v *= 10; v += c - '0'; } return v * sign; }
inline string readstring() { string s; char c; while ((c = getchar()) != EOF && c != '\n' && c != ' ') { s.push_back(c); } return s; }
template <class result_t=std::chrono::milliseconds,class clock_t=std::chrono::steady_clock,class duration_t = std::chrono::milliseconds>
auto since(std::chrono::time_point<clock_t, duration_t> const& start){return std::chrono::duration_cast<result_t>(clock_t::now() - start);}
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vpii> vvpii;
typedef vector<vpl> vvpl;

vvpl adj(200001);
int removed[200001], subtreeSiz[200001];
ll k, currIt = 0;
vector<pl> found(1000001, pl{1, INF});
vpl saved;

int getSiz(int u, int last = -1){
    if(removed[u]) return 0;
    int res = 1;
    for(auto &[v, w] : adj[u]){
        if(v == last) continue;
        res += getSiz(v, u);
    }
    subtreeSiz[u] = res;
    return res;
}

int getCentroid(int u, int last, int siz){
    for(auto &[v, w] : adj[u]){
        if(v == last || removed[v]) continue;
        if(subtreeSiz[v] * 2 > siz) return getCentroid(v, u, siz);
    }
    return u;
}

ll compute(int u, int last, ll l, int steps = 1){
    if(removed[u] || k-l<0) return INF;
    saved.pb({l, steps});
    ll res = INF;
    if(found[k-l].F==currIt) res = found[k-l].S+steps;
    for(auto &[v, w] : adj[u]){
        if(v == last) continue;
        res = min(res, compute(v, u, l+w, steps+1));
    }
    return res;
}

ll CentroidDecomp(int node){
    int centroid = getCentroid(node, -1, getSiz(node));
    currIt--;

    removed[centroid] = 1;
    ll res = INF;
    found[0] = {currIt, 0}; 
    for(auto &[v, w] : adj[centroid]){
        saved.clear();
        res = min(res, compute(v, -1, w));
        for(auto &[v1, v2] : saved) {
            found[v1] = min(found[v1], {currIt, v2});
        }
    }

    for(auto &[v, w] : adj[centroid]){
        if(removed[v]) continue;
        res = min(res, CentroidDecomp(v));
    }
    return res;
}

int best_path(int N, int K, int H[][2], int L[]){
    k = K;
    fo(i, N-1){
        adj[H[i][0]].pb({H[i][1], L[i]});
        adj[H[i][1]].pb({H[i][0], L[i]});
    }
    ll res = CentroidDecomp(0);

    return (res>1e16?-1:res);
}


// #define MAX_N 500000

// static int N, K;
// static int H[MAX_N][2];
// static int L[MAX_N];
// static int solution;

// inline 
// void my_assert(int e) {if (!e) abort();}

// void read_input()
// {
//   int i;
//   my_assert(2==scanf("%d %d",&N,&K));
//   for(i=0; i<N-1; i++)
//     my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
//   my_assert(1==scanf("%d",&solution));
// }

// int main()
// {
//   int ans;
//   read_input();
//   ans = best_path(N,K,H,L);
//   if(ans==solution)
//     printf("Correct.\n");
//   else
//     printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);

//   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...