Submission #881017

#TimeUsernameProblemLanguageResultExecution timeMemory
881017minhi1Race (IOI11_race)C++14
0 / 100
9 ms31208 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 6;

int n, res = (int)1e9;
long long target;
int sz[N];
int mp[N];
bool is_removed[N];
bool in_map[N];
vector < int > del;
vector < pair < int , int > > adj[N];

int dfs_sz(int u, int p = -1) {
    sz[u] = 1;
    for (auto [v, w] : adj[u]) {
        if (v == p || is_removed[v]) continue;
        sz[u] += dfs_sz(v, u);
    }
    return sz[u];
}

int get_cent(int u, int tree_size, int p) {
    for (auto [v, w] : adj[u]) {
        if (v == p || is_removed[v]) continue;
        if (sz[v] * 2 > tree_size) return get_cent(v, tree_size, u);
    }
    return u;
}

void solve(int u, int cent, int p, long long sum, int dist) {
    if (sum <= target && mp[target - sum] > 0)
        res = min(res, dist + mp[target - sum]);
    if (sum == target) res = min(res, dist);

    for (auto [v, w] : adj[u]) {
        if (v == p || is_removed[v]) continue;
        if (sum + w > target) continue;
        solve(v, cent, u, sum + w, dist + 1);
    }
}

void upd(int u, int cent, int p, long long sum, int dist) {
    if (sum <= target) {
        int val = mp[sum];
        val = (val == 0 ? dist : min(val, dist));
        mp[sum] = val;
        if (!in_map[sum]) {
            in_map[sum] = true;
            del.push_back(sum);
        }
    }

    for (auto [v, w] : adj[u]) {
        if (v == p || is_removed[v]) continue;
        if (sum + w > target) continue;
        upd(v, cent, u, sum + w, dist + 1);
    }
}

void decomp(int u) {
    int cent = get_cent(u, dfs_sz(u), -1);

    for (auto [v, w] : adj[cent]) {
        if (is_removed[v]) continue;
        solve(v, cent, cent, w, 1);
        upd(v, cent, cent, w, 1);
    }

    // TODO: refresh
    for (int sum : del) {
        mp[sum] = 0;
        in_map[sum] = 0;
    }
    del.clear();

    is_removed[cent] = true;

    for (auto [v, w] : adj[cent]) {
        if (is_removed[v]) continue;
        decomp(v);
    }
}

int best_path(int n, int target, int h[][2], int l[]) {
    for (int i = 0; i < n; ++i) {
        int u = h[i][0], v = h[i][1], w = l[i];
        ++u; ++v;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }

    decomp(1);

    return (res == (int)1e9 ? -1 : res);
}

//int main() {
//    ios::sync_with_stdio(0);
//    cin.tie(0);
//
//    #ifdef trcute
//        freopen("test.inp", "r", stdin);
//        // freopen("test.out", "w", stdout);
//    #endif
//
//    cin >> n >> target;
//    for (int i = 1; i < n; ++i) {
//        int u, v, w;
//        cin >> u >> v >> w;
//        ++u; ++v;
//        adj[u].push_back({v, w});
//        adj[v].push_back({u, w});
//    }
//
//    decomp(1);
//
//    cout << (res == (int)1e9 ? -1 : res);
//
//    return 0;
//}

Compilation message (stderr)

race.cpp: In function 'int dfs_sz(int, int)':
race.cpp:17:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   17 |     for (auto [v, w] : adj[u]) {
      |               ^
race.cpp: In function 'int get_cent(int, int, int)':
race.cpp:25:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   25 |     for (auto [v, w] : adj[u]) {
      |               ^
race.cpp: In function 'void solve(int, int, int, long long int, int)':
race.cpp:37:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   37 |     for (auto [v, w] : adj[u]) {
      |               ^
race.cpp: In function 'void upd(int, int, int, long long int, int)':
race.cpp:55:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   55 |     for (auto [v, w] : adj[u]) {
      |               ^
race.cpp: In function 'void decomp(int)':
race.cpp:65:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   65 |     for (auto [v, w] : adj[cent]) {
      |               ^
race.cpp:80:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   80 |     for (auto [v, w] : adj[cent]) {
      |               ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...