Submission #418699

#TimeUsernameProblemLanguageResultExecution timeMemory
418699snasibov05Race (IOI11_race)C++14
21 / 100
3076 ms17952 KiB
#include "race.h"
#include <vector>
#include <unordered_map>
#include <cassert>

using namespace std;

#define oo 1000000000
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second

int ans = oo;
vector<vector<pii>> ed;
vector<bool> used;
vector<pii> v;

int findCentroid(int cur, int pr, int n, int &centroid) {
    int size = 1;
    for (auto[x, w]: ed[cur]) {
        if (x == pr || used[x]) continue;
        size += findCentroid(x, cur, n, centroid);
    }
    if (size * 2 >= n && centroid == -1) centroid = cur;
    return size;
}

void dfs(int cur, int pr, int k, int d, int l){
    if (d > k) return;
    v.pb({d, l});
    for (auto [x, w] : ed[cur]){
        if (x == pr || used[x]) continue;
        dfs(x, cur, k, d + w, l + 1);
    }
}

void calc(int cur, int n, int k){
    findCentroid(cur, -1, n, cur);

    unordered_map<int, int> dp;
    used[cur] = true;

    for (auto [x, w] : ed[cur]){
        v.clear();
        dfs(x, cur, k, w, 1);

        for (auto [d, l] : v){
            if (d == k) ans = min(ans, l);
            if (dp.count(k-d) == 0) continue;
            ans = min(ans, l + dp[k - d]);
        }
        for (auto [d, l] : v){
            if (dp[d] == 0) dp[d] = l;
            else dp[d] = min(dp[d], l);
        }
    }

    for (auto [x, w] : ed[cur]){
        if (used[x]) continue;
        calc(x, (n+1)/2, k);
    }
}

int best_path(int n, int k, int h[][2], int l[]){
    ed.resize(n);
    used.resize(n);

    for (int i = 0; i < n - 1; ++i) {
        ed[h[i][0]].pb({h[i][1], l[i]});
        ed[h[i][1]].pb({h[i][0], l[i]});
    }

    calc(0, n, k);

    if (ans == oo) ans = -1;

    return ans;

}

Compilation message (stderr)

race.cpp: In function 'int findCentroid(int, int, int, int&)':
race.cpp:21:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   21 |     for (auto[x, w]: ed[cur]) {
      |              ^
race.cpp: In function 'void dfs(int, int, int, int, int)':
race.cpp:32:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   32 |     for (auto [x, w] : ed[cur]){
      |               ^
race.cpp: In function 'void calc(int, int, int)':
race.cpp:44:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   44 |     for (auto [x, w] : ed[cur]){
      |               ^
race.cpp:48:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   48 |         for (auto [d, l] : v){
      |                   ^
race.cpp:53:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   53 |         for (auto [d, l] : v){
      |                   ^
race.cpp:59:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   59 |     for (auto [x, w] : ed[cur]){
      |               ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...