Submission #1082595

# Submission time Handle Problem Language Result Execution time Memory
1082595 2024-08-31T17:04:53 Z f0rizen Race (IOI11_race) C++17
0 / 100
1 ms 2396 KB
#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;
map<int, int> mp;
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 (mp.find(k - sum) != mp.end()) {
        ans = min(ans, mp[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) {
    if (mp.find(sum) == mp.end()) {
        mp[sum] = d;
    } else {
        mp[sum] = min(mp[sum], d);
    }
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            dfs_mp(u, v, sum + w, d + 1);
        }
    }
}

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);
    for (auto [u, w] : g[v]) {
        if (!used[u]) {
            dfs_ans(u, v, w, 1);
            dfs_mp(u, v, w, 1);
        }
    }
    mp.clear();
    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);
    dfs_sz(0);
    build(centroid(0, -1, sz[0]));
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -