Submission #998150

# Submission time Handle Problem Language Result Execution time Memory
998150 2024-06-13T10:39:46 Z alexdumitru Race (IOI11_race) C++14
100 / 100
264 ms 39248 KB
#include "race.h"
#include <iostream>
#include <vector>

using namespace std;

const int NMAX = 2e5;
const int KMAX = 1e6;

vector<pair<int, int>> g[NMAX];
int w[NMAX];
bool used[NMAX];
int minDepth[KMAX + 1];
int Ans;

int get_w(int node, int dad) {
    w[node] = 1;
    for (auto it : g[node]) {
        if (it.first == dad || used[it.first]) continue;
        get_w(it.first, node);
        w[node] += w[it.first];
    }
    return w[node];
}

int get_centroid(int node, int dad, int totalW) {
    for (auto it : g[node]) {
        if (it.first == dad || used[it.first]) continue;
        if (w[it.first] > totalW / 2)
            return get_centroid(it.first, node, totalW);
    }
    return node;
}

void dfs_add(int node, int dad, int k, int length, int depth) {
    if (length > k) return;
    minDepth[length] = min(minDepth[length], depth);

    for (auto it : g[node]) {
        if (it.first == dad || used[it.first]) continue;
        dfs_add(it.first, node, k, length + it.second, depth + 1);
    }
}

void dfs_compute(int node, int dad, int k, int length, int depth) {
    if (length > k) return;
    Ans = min(Ans, depth + minDepth[k - length]);

    for (auto it : g[node]) {
        if (it.first == dad || used[it.first]) continue;
        dfs_compute(it.first, node, k, length + it.second, depth + 1);
    }
}

void dfs_clear(int node, int dad, int k, int length) {
    if (length > k) return;
    minDepth[length] = NMAX;

    for (auto it : g[node]) {
        if (it.first == dad || used[it.first]) continue;
        dfs_clear(it.first, node, k, length + it.second);
    }
}

void go(int node, int k) {
    int totalW = get_w(node, -1);
    int c = get_centroid(node, -1, totalW);
    used[c] = true;

    minDepth[0] = 0;

    for (auto it : g[c]) {
        if (used[it.first]) continue;
        dfs_compute(it.first, -1, k, it.second, 1);
        dfs_add(it.first, -1, k, it.second, 1);
    }

  	dfs_clear(c, -1, k, 0);

    for (auto it : g[c])
        if (!used[it.first])
            go(it.first, k);
}

int best_path(int N, int K, int H[][2], int L[]) {
    Ans = N;

    for (int i = 0; i <= KMAX; i++) {
        minDepth[i] = N + 1;
    }

    for (int i = 0; i < N - 1; i++) {
        g[H[i][0]].emplace_back(H[i][1], L[i]);
        g[H[i][1]].emplace_back(H[i][0], L[i]);
    }

    go(0, K);

    return Ans == N ? -1 : Ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 2 ms 13660 KB Output is correct
3 Correct 2 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13656 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 2 ms 13660 KB Output is correct
8 Correct 3 ms 13656 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 2 ms 13660 KB Output is correct
13 Correct 2 ms 13660 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 2 ms 13660 KB Output is correct
16 Correct 2 ms 13660 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 2 ms 13660 KB Output is correct
3 Correct 2 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13656 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 2 ms 13660 KB Output is correct
8 Correct 3 ms 13656 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 2 ms 13660 KB Output is correct
13 Correct 2 ms 13660 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 2 ms 13660 KB Output is correct
16 Correct 2 ms 13660 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 2 ms 13660 KB Output is correct
20 Correct 3 ms 13664 KB Output is correct
21 Correct 3 ms 13660 KB Output is correct
22 Correct 3 ms 13660 KB Output is correct
23 Correct 3 ms 13660 KB Output is correct
24 Correct 4 ms 13660 KB Output is correct
25 Correct 4 ms 13656 KB Output is correct
26 Correct 3 ms 13660 KB Output is correct
27 Correct 3 ms 13500 KB Output is correct
28 Correct 4 ms 13656 KB Output is correct
29 Correct 3 ms 13656 KB Output is correct
30 Correct 4 ms 13656 KB Output is correct
31 Correct 3 ms 13660 KB Output is correct
32 Correct 5 ms 13660 KB Output is correct
33 Correct 3 ms 13660 KB Output is correct
34 Correct 3 ms 13660 KB Output is correct
35 Correct 3 ms 13660 KB Output is correct
36 Correct 3 ms 13660 KB Output is correct
37 Correct 3 ms 13660 KB Output is correct
38 Correct 3 ms 13656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 2 ms 13660 KB Output is correct
3 Correct 2 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13656 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 2 ms 13660 KB Output is correct
8 Correct 3 ms 13656 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 2 ms 13660 KB Output is correct
13 Correct 2 ms 13660 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 2 ms 13660 KB Output is correct
16 Correct 2 ms 13660 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 98 ms 19280 KB Output is correct
20 Correct 77 ms 19284 KB Output is correct
21 Correct 89 ms 19424 KB Output is correct
22 Correct 76 ms 19548 KB Output is correct
23 Correct 44 ms 19792 KB Output is correct
24 Correct 35 ms 19544 KB Output is correct
25 Correct 80 ms 23128 KB Output is correct
26 Correct 71 ms 26716 KB Output is correct
27 Correct 89 ms 23416 KB Output is correct
28 Correct 140 ms 39248 KB Output is correct
29 Correct 143 ms 38520 KB Output is correct
30 Correct 99 ms 25452 KB Output is correct
31 Correct 92 ms 25596 KB Output is correct
32 Correct 101 ms 25424 KB Output is correct
33 Correct 120 ms 24468 KB Output is correct
34 Correct 107 ms 24912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 2 ms 13660 KB Output is correct
3 Correct 2 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13656 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 2 ms 13660 KB Output is correct
8 Correct 3 ms 13656 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 2 ms 13660 KB Output is correct
13 Correct 2 ms 13660 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 2 ms 13660 KB Output is correct
16 Correct 2 ms 13660 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 2 ms 13660 KB Output is correct
20 Correct 3 ms 13664 KB Output is correct
21 Correct 3 ms 13660 KB Output is correct
22 Correct 3 ms 13660 KB Output is correct
23 Correct 3 ms 13660 KB Output is correct
24 Correct 4 ms 13660 KB Output is correct
25 Correct 4 ms 13656 KB Output is correct
26 Correct 3 ms 13660 KB Output is correct
27 Correct 3 ms 13500 KB Output is correct
28 Correct 4 ms 13656 KB Output is correct
29 Correct 3 ms 13656 KB Output is correct
30 Correct 4 ms 13656 KB Output is correct
31 Correct 3 ms 13660 KB Output is correct
32 Correct 5 ms 13660 KB Output is correct
33 Correct 3 ms 13660 KB Output is correct
34 Correct 3 ms 13660 KB Output is correct
35 Correct 3 ms 13660 KB Output is correct
36 Correct 3 ms 13660 KB Output is correct
37 Correct 3 ms 13660 KB Output is correct
38 Correct 3 ms 13656 KB Output is correct
39 Correct 98 ms 19280 KB Output is correct
40 Correct 77 ms 19284 KB Output is correct
41 Correct 89 ms 19424 KB Output is correct
42 Correct 76 ms 19548 KB Output is correct
43 Correct 44 ms 19792 KB Output is correct
44 Correct 35 ms 19544 KB Output is correct
45 Correct 80 ms 23128 KB Output is correct
46 Correct 71 ms 26716 KB Output is correct
47 Correct 89 ms 23416 KB Output is correct
48 Correct 140 ms 39248 KB Output is correct
49 Correct 143 ms 38520 KB Output is correct
50 Correct 99 ms 25452 KB Output is correct
51 Correct 92 ms 25596 KB Output is correct
52 Correct 101 ms 25424 KB Output is correct
53 Correct 120 ms 24468 KB Output is correct
54 Correct 107 ms 24912 KB Output is correct
55 Correct 9 ms 14172 KB Output is correct
56 Correct 12 ms 13916 KB Output is correct
57 Correct 48 ms 19764 KB Output is correct
58 Correct 23 ms 20688 KB Output is correct
59 Correct 69 ms 27524 KB Output is correct
60 Correct 264 ms 38888 KB Output is correct
61 Correct 100 ms 24924 KB Output is correct
62 Correct 114 ms 25424 KB Output is correct
63 Correct 134 ms 25456 KB Output is correct
64 Correct 247 ms 24164 KB Output is correct
65 Correct 102 ms 26448 KB Output is correct
66 Correct 234 ms 34132 KB Output is correct
67 Correct 59 ms 26816 KB Output is correct
68 Correct 137 ms 25964 KB Output is correct
69 Correct 130 ms 23820 KB Output is correct
70 Correct 151 ms 26064 KB Output is correct