Submission #1031669

# Submission time Handle Problem Language Result Execution time Memory
1031669 2024-07-23T03:32:43 Z TrendBattles Race (IOI11_race) C++14
100 / 100
331 ms 38856 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;


const int MAX_K = 1 << 20, inf = 0x3f3f3f3f;
int mn[MAX_K];
void minimise(int& x, int y) {
    if (x > y) x = y;
}
int best_path(int N, int K, int H[][2], int L[]) {
    vector <vector <int>> graph(N);
    for (int i = 0; i < N - 1; ++i) {
        graph[H[i][0]].push_back(i);
        graph[H[i][1]].push_back(i);
    }

    vector <int> sub_sz(N), last_v(N, -1), removed(N);
    
    auto Find_Centroid = [&] (int u, int parent, int sz) -> int {
        int last = u;
        while (true) {
            for (int id : graph[u]) {
                int v = H[id][0] ^ H[id][1] ^ u;

                if (v == parent or removed[v]) continue;

                if (sub_sz[v] * 2 > sz) {
                    last = v; break;
                }
            }

            if (last == u) break;
            parent = u; u = last; 
        }
        return last;
    };
    auto get_size = [&] (auto self, int u, int parent) -> int {
        sub_sz[u] = 1;
        for (int id : graph[u]) {
            int v = H[id][0] ^ H[id][1] ^ u;

            if (v == parent or removed[v]) continue;

            sub_sz[u] += self(self, v, u);
        }
        return sub_sz[u];
    };
    vector <int> pending;
    pending.push_back(0);

    memset(mn, 0x3f, sizeof mn);
    int ans = inf;

    vector <int> pref_w(N), depth(N);
    vector <int> cache;

    auto DFS = [&] (auto self, int u, int parent) -> void {
        if (pref_w[u] > K) return;

        minimise(ans, depth[u] + mn[K - pref_w[u]]);

        cache.push_back(u);
        for (int id : graph[u]) {
            int v = H[id][0] ^ H[id][1] ^ u;
            if (v == parent or removed[v]) continue;

            depth[v] = depth[u] + 1;
            pref_w[v] = pref_w[u] + L[id];

            self(self, v, u);
        }
    };
    auto Build_Centroid = [&] (int u, int parent) -> void {
        int cen_node = Find_Centroid(u, parent, get_size(get_size, u, parent));

        int l = 0;
        removed[cen_node] = true;
        depth[cen_node] = 0; pref_w[cen_node] = 0;
        mn[0] = 0;

        for (int id : graph[cen_node]) {
            int v = H[id][0] ^ H[id][1] ^ cen_node;
            if (removed[v]) continue;

            depth[v] = 1;
            pref_w[v] = L[id];

            DFS(DFS, v, cen_node);
            for (; l < (int) cache.size(); l += 1) {
                int& node = cache[l];

                minimise(mn[pref_w[node]], depth[node]);
            }
        }

        for (int x : cache) {
            mn[pref_w[x]] = inf;
        }
        cache.clear();
        
        for (int id : graph[cen_node]) {
            int v = H[id][0] ^ H[id][1] ^ cen_node;
            if (removed[v]) continue;

            last_v[v] = cen_node;
            pending.push_back(v);
        }
    };
    while (not pending.empty()) {
        int u = pending.back(); pending.pop_back();
        Build_Centroid(u, last_v[u]);
    }

    return ans == inf ? -1 : ans;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4552 KB Output is correct
4 Correct 2 ms 4412 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 3 ms 4444 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 2 ms 4504 KB Output is correct
15 Correct 2 ms 4440 KB Output is correct
16 Correct 2 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4552 KB Output is correct
4 Correct 2 ms 4412 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 3 ms 4444 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 2 ms 4504 KB Output is correct
15 Correct 2 ms 4440 KB Output is correct
16 Correct 2 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 2 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 2 ms 4444 KB Output is correct
23 Correct 2 ms 4444 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 2 ms 4444 KB Output is correct
27 Correct 2 ms 4564 KB Output is correct
28 Correct 2 ms 4444 KB Output is correct
29 Correct 3 ms 4444 KB Output is correct
30 Correct 4 ms 4544 KB Output is correct
31 Correct 3 ms 4444 KB Output is correct
32 Correct 3 ms 4444 KB Output is correct
33 Correct 3 ms 4444 KB Output is correct
34 Correct 4 ms 4444 KB Output is correct
35 Correct 3 ms 4544 KB Output is correct
36 Correct 2 ms 4444 KB Output is correct
37 Correct 3 ms 4444 KB Output is correct
38 Correct 2 ms 4448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4552 KB Output is correct
4 Correct 2 ms 4412 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 3 ms 4444 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 2 ms 4504 KB Output is correct
15 Correct 2 ms 4440 KB Output is correct
16 Correct 2 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 108 ms 13440 KB Output is correct
20 Correct 115 ms 14576 KB Output is correct
21 Correct 116 ms 14312 KB Output is correct
22 Correct 104 ms 15140 KB Output is correct
23 Correct 58 ms 14428 KB Output is correct
24 Correct 45 ms 14684 KB Output is correct
25 Correct 133 ms 16976 KB Output is correct
26 Correct 83 ms 21460 KB Output is correct
27 Correct 140 ms 23892 KB Output is correct
28 Correct 196 ms 33940 KB Output is correct
29 Correct 188 ms 33224 KB Output is correct
30 Correct 122 ms 24408 KB Output is correct
31 Correct 120 ms 24400 KB Output is correct
32 Correct 151 ms 24424 KB Output is correct
33 Correct 168 ms 23888 KB Output is correct
34 Correct 139 ms 24144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4552 KB Output is correct
4 Correct 2 ms 4412 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 3 ms 4444 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 2 ms 4504 KB Output is correct
15 Correct 2 ms 4440 KB Output is correct
16 Correct 2 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 2 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 2 ms 4444 KB Output is correct
23 Correct 2 ms 4444 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 2 ms 4444 KB Output is correct
27 Correct 2 ms 4564 KB Output is correct
28 Correct 2 ms 4444 KB Output is correct
29 Correct 3 ms 4444 KB Output is correct
30 Correct 4 ms 4544 KB Output is correct
31 Correct 3 ms 4444 KB Output is correct
32 Correct 3 ms 4444 KB Output is correct
33 Correct 3 ms 4444 KB Output is correct
34 Correct 4 ms 4444 KB Output is correct
35 Correct 3 ms 4544 KB Output is correct
36 Correct 2 ms 4444 KB Output is correct
37 Correct 3 ms 4444 KB Output is correct
38 Correct 2 ms 4448 KB Output is correct
39 Correct 108 ms 13440 KB Output is correct
40 Correct 115 ms 14576 KB Output is correct
41 Correct 116 ms 14312 KB Output is correct
42 Correct 104 ms 15140 KB Output is correct
43 Correct 58 ms 14428 KB Output is correct
44 Correct 45 ms 14684 KB Output is correct
45 Correct 133 ms 16976 KB Output is correct
46 Correct 83 ms 21460 KB Output is correct
47 Correct 140 ms 23892 KB Output is correct
48 Correct 196 ms 33940 KB Output is correct
49 Correct 188 ms 33224 KB Output is correct
50 Correct 122 ms 24408 KB Output is correct
51 Correct 120 ms 24400 KB Output is correct
52 Correct 151 ms 24424 KB Output is correct
53 Correct 168 ms 23888 KB Output is correct
54 Correct 139 ms 24144 KB Output is correct
55 Correct 9 ms 5468 KB Output is correct
56 Correct 9 ms 5468 KB Output is correct
57 Correct 64 ms 13828 KB Output is correct
58 Correct 27 ms 15692 KB Output is correct
59 Correct 83 ms 21652 KB Output is correct
60 Correct 318 ms 38856 KB Output is correct
61 Correct 130 ms 24148 KB Output is correct
62 Correct 141 ms 25632 KB Output is correct
63 Correct 185 ms 25592 KB Output is correct
64 Correct 331 ms 24012 KB Output is correct
65 Correct 156 ms 25168 KB Output is correct
66 Correct 285 ms 29624 KB Output is correct
67 Correct 70 ms 28232 KB Output is correct
68 Correct 177 ms 25040 KB Output is correct
69 Correct 188 ms 23172 KB Output is correct
70 Correct 167 ms 24808 KB Output is correct