Submission #1084194

# Submission time Handle Problem Language Result Execution time Memory
1084194 2024-09-05T15:08:07 Z f0rizen Race (IOI11_race) C++17
0 / 100
5 ms 8284 KB
#include "race.h"

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;

struct Edge {
    int to, w;
};

int k;
vector<vector<Edge>> g;
vector<bool> used;
vector<int> sz;
vector<int> dist;
int ans = inf;

void dfs_sz(int v, int p = -1) {
    sz[v] = 1;
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            dfs_sz(u, v);
            sz[v] += sz[u];
        }
    }
}

void dfs_ans(int v, int p = -1, int sum = 0, int d = 0) {
    if (k - sum >= 0 && dist[k - sum] < inf) {
        ans = min(ans, dist[k - sum] + d);
    }
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            dfs_ans(u, v, sum + w, d + 1);
        }
    }
}

void dfs_mp(int v, int p = -1, int sum = 0, int d = 0) {
    dist[sum] = min(dist[sum], d);
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            dfs_mp(u, v, sum + w, d + 1);
        }
    }
}

void dfs_clear(int v, int p = -1, int sum = 0) {
    dist[sum] = inf;
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            dfs_clear(u, v, sum + w);
        }
    }
}

int centroid(int v, int p, int n) {
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            if (sz[u] * 2 > n) {
                return centroid(u, v, n);
            }
        }
    }
    return v;
}

void build(int v) {
    dfs_sz(v);
    dist[0] = 0;
    for (auto [u, w] : g[v]) {
        if (!used[u]) {
            dfs_ans(u, v, w, 1);
            dfs_mp(u, v, w, 1);
        }
    }
    dfs_clear(v);
    used[v] = 1;
    for (auto [u, w] : g[v]) {
        if (!used[u]) {
            build(centroid(u, v, sz[u]));
        }
    }
}

int best_path(int N, int K, int H[][2], int L[]) {
    k = K;
    g.resize(N);
    for (int i = 0; i < N - 1; ++i) {
        int u = H[i][0], v = H[i][1], w = L[i];
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    used.resize(N);
    sz.resize(N);
    dist.resize(1e6 + 5, inf);
    dfs_sz(0);
    build(centroid(0, -1, sz[0]));
    if (ans == inf) {
        return -1;
    } else {
        return ans;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4188 KB Output is correct
2 Correct 2 ms 4188 KB Output is correct
3 Correct 2 ms 4188 KB Output is correct
4 Correct 2 ms 4188 KB Output is correct
5 Runtime error 5 ms 8284 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4188 KB Output is correct
2 Correct 2 ms 4188 KB Output is correct
3 Correct 2 ms 4188 KB Output is correct
4 Correct 2 ms 4188 KB Output is correct
5 Runtime error 5 ms 8284 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4188 KB Output is correct
2 Correct 2 ms 4188 KB Output is correct
3 Correct 2 ms 4188 KB Output is correct
4 Correct 2 ms 4188 KB Output is correct
5 Runtime error 5 ms 8284 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4188 KB Output is correct
2 Correct 2 ms 4188 KB Output is correct
3 Correct 2 ms 4188 KB Output is correct
4 Correct 2 ms 4188 KB Output is correct
5 Runtime error 5 ms 8284 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -