Submission #856105

# Submission time Handle Problem Language Result Execution time Memory
856105 2023-10-02T16:43:02 Z wii Race (IOI11_race) C++17
100 / 100
353 ms 65984 KB
#include <iostream>
#include <vector>
using namespace std;
 
const int Lim = 1e6 + 5;
const int maxn = 1e6 + 5;
const int Inf = 1e9 + 7;
typedef pair<int, int> pii;
 
template<class T> bool minimize(T &res, const T &x) { if (x < res) { res = x; return true; } return false; }
 
int n, k;
vector<pii> adj[maxn];
int ans[Lim];
 
int sz[maxn], f[maxn];
int dfs_sz(int u, int p) {
    sz[u] = 1;
    for (auto [v, w]: adj[u]) if (v != p && !f[v]) {
        sz[u] += dfs_sz(v, u);
    }
    return sz[u];
}
 
int find_centroid(int u, int p, int n) {
    for (auto [v, w]: adj[u]) if (v != p && !f[v])
        if (sz[v] * 2 > n) return find_centroid(v, u, n);
    return u;
}
 
int dp[maxn], dist[maxn], dep[maxn];
void add(int u) {
    minimize(dp[dist[u]], dep[u]);
}
 
void sub(int u) {
    dp[dist[u]] = Inf;
}
 
void push(int u, int p) {
    if (dist[u] > k) return;
 
    add(u);
    for (auto [v, w]: adj[u]) if (v != p && !f[v])
        push(v, u);
}
 
int get(int u, int p) {
    if (dist[u] > k) return Inf;
 
    int ans = dp[k - dist[u]] + dep[u];
    for (auto [v, w]: adj[u]) if (v != p && !f[v])
        minimize(ans, get(v, u));
    return ans;
}
 
void pop(int u, int p) {
    if (dist[u] > k) return;
 
    sub(u);
    for (auto [v, w]: adj[u]) if (v != p && !f[v])
        pop(v, u);
}
 
void fill(int u, int p) {
    if (p == -1) {
        dist[u] = 0; dep[u] = 0;
    }
 
    for (auto [v, w]: adj[u]) if (v != p && !f[v]) {
        dist[v] = dist[u] + w;
        dep[v] = dep[u] + 1;
 
        fill(v, u);
    }
}
 
int calc(int r) {
    fill(r, -1);
 
    int ans = Inf;
    add(r);
 
    for (auto [u, w]: adj[r]) if (!f[u]) {
        minimize(ans, get(u, r));
        push(u, r);
    }
 
    pop(r, -1);
    return ans;
}
 
int centroid(int u) {
    int n = dfs_sz(u, -1);
    u = find_centroid(u, -1, n);
 
    int ans = calc(u);
 
    f[u] = true;
    for (auto [v, w]: adj[u]) if (!f[v])
        ans = min(ans, centroid(v));
    f[u] = false;
 
    return ans;
}
 
int best_path(int N, int K, int H[][2], int L[]) {
    n = N; k = K;
    for (int i = 0; i < n - 1; ++i) {
        int u = H[i][0];
        int v = H[i][1];
        int w = L[i];
 
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
 
    for (int i = 0; i <= k; ++i) dp[i] = Inf;
    int ans = centroid(0);
 
    if (ans > n) ans = -1;
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 34396 KB Output is correct
2 Correct 7 ms 34652 KB Output is correct
3 Correct 7 ms 34652 KB Output is correct
4 Correct 7 ms 34652 KB Output is correct
5 Correct 7 ms 34652 KB Output is correct
6 Correct 7 ms 34396 KB Output is correct
7 Correct 7 ms 34652 KB Output is correct
8 Correct 7 ms 34652 KB Output is correct
9 Correct 7 ms 34488 KB Output is correct
10 Correct 7 ms 34392 KB Output is correct
11 Correct 7 ms 34616 KB Output is correct
12 Correct 8 ms 34652 KB Output is correct
13 Correct 7 ms 34652 KB Output is correct
14 Correct 7 ms 34396 KB Output is correct
15 Correct 7 ms 34652 KB Output is correct
16 Correct 7 ms 34652 KB Output is correct
17 Correct 7 ms 34652 KB Output is correct
18 Correct 7 ms 34652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 34396 KB Output is correct
2 Correct 7 ms 34652 KB Output is correct
3 Correct 7 ms 34652 KB Output is correct
4 Correct 7 ms 34652 KB Output is correct
5 Correct 7 ms 34652 KB Output is correct
6 Correct 7 ms 34396 KB Output is correct
7 Correct 7 ms 34652 KB Output is correct
8 Correct 7 ms 34652 KB Output is correct
9 Correct 7 ms 34488 KB Output is correct
10 Correct 7 ms 34392 KB Output is correct
11 Correct 7 ms 34616 KB Output is correct
12 Correct 8 ms 34652 KB Output is correct
13 Correct 7 ms 34652 KB Output is correct
14 Correct 7 ms 34396 KB Output is correct
15 Correct 7 ms 34652 KB Output is correct
16 Correct 7 ms 34652 KB Output is correct
17 Correct 7 ms 34652 KB Output is correct
18 Correct 7 ms 34652 KB Output is correct
19 Correct 7 ms 34392 KB Output is correct
20 Correct 7 ms 34440 KB Output is correct
21 Correct 7 ms 34648 KB Output is correct
22 Correct 8 ms 38788 KB Output is correct
23 Correct 8 ms 38748 KB Output is correct
24 Correct 8 ms 38748 KB Output is correct
25 Correct 8 ms 38748 KB Output is correct
26 Correct 7 ms 36540 KB Output is correct
27 Correct 8 ms 38748 KB Output is correct
28 Correct 7 ms 34652 KB Output is correct
29 Correct 8 ms 36748 KB Output is correct
30 Correct 8 ms 36696 KB Output is correct
31 Correct 8 ms 36700 KB Output is correct
32 Correct 8 ms 36568 KB Output is correct
33 Correct 8 ms 38748 KB Output is correct
34 Correct 9 ms 36700 KB Output is correct
35 Correct 8 ms 39000 KB Output is correct
36 Correct 8 ms 38748 KB Output is correct
37 Correct 8 ms 38748 KB Output is correct
38 Correct 8 ms 36604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 34396 KB Output is correct
2 Correct 7 ms 34652 KB Output is correct
3 Correct 7 ms 34652 KB Output is correct
4 Correct 7 ms 34652 KB Output is correct
5 Correct 7 ms 34652 KB Output is correct
6 Correct 7 ms 34396 KB Output is correct
7 Correct 7 ms 34652 KB Output is correct
8 Correct 7 ms 34652 KB Output is correct
9 Correct 7 ms 34488 KB Output is correct
10 Correct 7 ms 34392 KB Output is correct
11 Correct 7 ms 34616 KB Output is correct
12 Correct 8 ms 34652 KB Output is correct
13 Correct 7 ms 34652 KB Output is correct
14 Correct 7 ms 34396 KB Output is correct
15 Correct 7 ms 34652 KB Output is correct
16 Correct 7 ms 34652 KB Output is correct
17 Correct 7 ms 34652 KB Output is correct
18 Correct 7 ms 34652 KB Output is correct
19 Correct 118 ms 42068 KB Output is correct
20 Correct 117 ms 42092 KB Output is correct
21 Correct 126 ms 42064 KB Output is correct
22 Correct 109 ms 42228 KB Output is correct
23 Correct 69 ms 42584 KB Output is correct
24 Correct 52 ms 42320 KB Output is correct
25 Correct 114 ms 45008 KB Output is correct
26 Correct 90 ms 47828 KB Output is correct
27 Correct 141 ms 52052 KB Output is correct
28 Correct 225 ms 63312 KB Output is correct
29 Correct 225 ms 62680 KB Output is correct
30 Correct 141 ms 52052 KB Output is correct
31 Correct 146 ms 52172 KB Output is correct
32 Correct 168 ms 52304 KB Output is correct
33 Correct 204 ms 51024 KB Output is correct
34 Correct 149 ms 51952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 34396 KB Output is correct
2 Correct 7 ms 34652 KB Output is correct
3 Correct 7 ms 34652 KB Output is correct
4 Correct 7 ms 34652 KB Output is correct
5 Correct 7 ms 34652 KB Output is correct
6 Correct 7 ms 34396 KB Output is correct
7 Correct 7 ms 34652 KB Output is correct
8 Correct 7 ms 34652 KB Output is correct
9 Correct 7 ms 34488 KB Output is correct
10 Correct 7 ms 34392 KB Output is correct
11 Correct 7 ms 34616 KB Output is correct
12 Correct 8 ms 34652 KB Output is correct
13 Correct 7 ms 34652 KB Output is correct
14 Correct 7 ms 34396 KB Output is correct
15 Correct 7 ms 34652 KB Output is correct
16 Correct 7 ms 34652 KB Output is correct
17 Correct 7 ms 34652 KB Output is correct
18 Correct 7 ms 34652 KB Output is correct
19 Correct 7 ms 34392 KB Output is correct
20 Correct 7 ms 34440 KB Output is correct
21 Correct 7 ms 34648 KB Output is correct
22 Correct 8 ms 38788 KB Output is correct
23 Correct 8 ms 38748 KB Output is correct
24 Correct 8 ms 38748 KB Output is correct
25 Correct 8 ms 38748 KB Output is correct
26 Correct 7 ms 36540 KB Output is correct
27 Correct 8 ms 38748 KB Output is correct
28 Correct 7 ms 34652 KB Output is correct
29 Correct 8 ms 36748 KB Output is correct
30 Correct 8 ms 36696 KB Output is correct
31 Correct 8 ms 36700 KB Output is correct
32 Correct 8 ms 36568 KB Output is correct
33 Correct 8 ms 38748 KB Output is correct
34 Correct 9 ms 36700 KB Output is correct
35 Correct 8 ms 39000 KB Output is correct
36 Correct 8 ms 38748 KB Output is correct
37 Correct 8 ms 38748 KB Output is correct
38 Correct 8 ms 36604 KB Output is correct
39 Correct 118 ms 42068 KB Output is correct
40 Correct 117 ms 42092 KB Output is correct
41 Correct 126 ms 42064 KB Output is correct
42 Correct 109 ms 42228 KB Output is correct
43 Correct 69 ms 42584 KB Output is correct
44 Correct 52 ms 42320 KB Output is correct
45 Correct 114 ms 45008 KB Output is correct
46 Correct 90 ms 47828 KB Output is correct
47 Correct 141 ms 52052 KB Output is correct
48 Correct 225 ms 63312 KB Output is correct
49 Correct 225 ms 62680 KB Output is correct
50 Correct 141 ms 52052 KB Output is correct
51 Correct 146 ms 52172 KB Output is correct
52 Correct 168 ms 52304 KB Output is correct
53 Correct 204 ms 51024 KB Output is correct
54 Correct 149 ms 51952 KB Output is correct
55 Correct 14 ms 35180 KB Output is correct
56 Correct 17 ms 35148 KB Output is correct
57 Correct 78 ms 42456 KB Output is correct
58 Correct 33 ms 42108 KB Output is correct
59 Correct 95 ms 47768 KB Output is correct
60 Correct 353 ms 65984 KB Output is correct
61 Correct 154 ms 52304 KB Output is correct
62 Correct 159 ms 55380 KB Output is correct
63 Correct 196 ms 55616 KB Output is correct
64 Correct 334 ms 54356 KB Output is correct
65 Correct 165 ms 53084 KB Output is correct
66 Correct 304 ms 64168 KB Output is correct
67 Correct 84 ms 56120 KB Output is correct
68 Correct 186 ms 56404 KB Output is correct
69 Correct 193 ms 56528 KB Output is correct
70 Correct 176 ms 55976 KB Output is correct