Submission #1084198

# Submission time Handle Problem Language Result Execution time Memory
1084198 2024-09-05T15:13:10 Z f0rizen Race (IOI11_race) C++17
100 / 100
657 ms 42500 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;
map<ll, 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);
    mp[0] = 0;
    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]));
    if (ans == inf) {
        return -1;
    } else {
        return ans;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 356 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 356 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 600 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 612 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 612 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 352 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 356 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 168 ms 9620 KB Output is correct
20 Correct 169 ms 9392 KB Output is correct
21 Correct 150 ms 9300 KB Output is correct
22 Correct 135 ms 9300 KB Output is correct
23 Correct 206 ms 9780 KB Output is correct
24 Correct 97 ms 9556 KB Output is correct
25 Correct 143 ms 14684 KB Output is correct
26 Correct 71 ms 14932 KB Output is correct
27 Correct 204 ms 18732 KB Output is correct
28 Correct 583 ms 42500 KB Output is correct
29 Correct 582 ms 41552 KB Output is correct
30 Correct 198 ms 18772 KB Output is correct
31 Correct 195 ms 18720 KB Output is correct
32 Correct 228 ms 19032 KB Output is correct
33 Correct 326 ms 17748 KB Output is correct
34 Correct 577 ms 30676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 356 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 600 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 612 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 612 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 352 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 168 ms 9620 KB Output is correct
40 Correct 169 ms 9392 KB Output is correct
41 Correct 150 ms 9300 KB Output is correct
42 Correct 135 ms 9300 KB Output is correct
43 Correct 206 ms 9780 KB Output is correct
44 Correct 97 ms 9556 KB Output is correct
45 Correct 143 ms 14684 KB Output is correct
46 Correct 71 ms 14932 KB Output is correct
47 Correct 204 ms 18732 KB Output is correct
48 Correct 583 ms 42500 KB Output is correct
49 Correct 582 ms 41552 KB Output is correct
50 Correct 198 ms 18772 KB Output is correct
51 Correct 195 ms 18720 KB Output is correct
52 Correct 228 ms 19032 KB Output is correct
53 Correct 326 ms 17748 KB Output is correct
54 Correct 577 ms 30676 KB Output is correct
55 Correct 16 ms 1628 KB Output is correct
56 Correct 9 ms 1344 KB Output is correct
57 Correct 94 ms 9616 KB Output is correct
58 Correct 31 ms 9168 KB Output is correct
59 Correct 210 ms 19792 KB Output is correct
60 Correct 595 ms 40652 KB Output is correct
61 Correct 227 ms 20564 KB Output is correct
62 Correct 194 ms 19028 KB Output is correct
63 Correct 226 ms 18772 KB Output is correct
64 Correct 657 ms 24912 KB Output is correct
65 Correct 569 ms 31984 KB Output is correct
66 Correct 604 ms 41224 KB Output is correct
67 Correct 81 ms 19396 KB Output is correct
68 Correct 349 ms 30032 KB Output is correct
69 Correct 360 ms 30288 KB Output is correct
70 Correct 343 ms 28756 KB Output is correct