Submission #1003574

# Submission time Handle Problem Language Result Execution time Memory
1003574 2024-06-20T13:08:23 Z ilef Race (IOI11_race) C++17
100 / 100
290 ms 32280 KB
#include <bits/stdc++.h>
using namespace std;

#define nmax 200020
#define ii pair<int, int>
#define ll long long

int n, k, ans, cnt[1000100], sz[nmax], r[nmax];
vector<ii> adj[nmax];

inline void minimize(int &a, int b)
{
    if(a == -1 || a > b)
        a = b;
}

int dfs(int u, int p = -1)
{
    sz[u] = 1;
    for(auto [C, v]: adj[u]) {
        if(v != p && !r[v]) sz[u] += dfs(v, u);
    }
    return sz[u];
}

int get_centroid(int m, int u, int p = -1)
{
    for(auto [C, v]: adj[u]) {
        if(v != p && !r[v] && 2 * sz[v] > m) return get_centroid(m, v, u);
    }
    return u;
}

void get_cnt(int sum, bool filling, int u, int p = -1, int depth = 1)
{
    if(sum > k) return;
    if(filling)
        minimize(cnt[sum], depth);
    else {
        if(cnt[k - sum] != -1)
            minimize(ans, depth + cnt[k - sum]);
    }
    for(auto [C, v]: adj[u]) {
        if(v != p && !r[v]) get_cnt(sum + C, filling, v, u, depth + 1);
    }
}

void del(int sum, int u, int p = -1)
{
    if(sum > k) return;
    cnt[sum] = -1;
    for(auto [C, v]: adj[u]) {
        if(v != p && !r[v])
            del(sum + C, v, u);
    }
}

void centroid(int u = 0)
{
    u = get_centroid(dfs(u), u);
    r[u] = 1;
    cnt[0] = 0;
    for(auto [C, v]: adj[u]) {
        if(!r[v]) {
            get_cnt(C, false, v);
            get_cnt(C, true, v);
        }
    }
    for(auto [C, v]: adj[u]) {
        if(!r[v]) {
            del(C, v);
        }
    }
    for(auto [C, v]: adj[u]) {
        if(!r[v]) centroid(v);
    }
}

int best_path(int N, int K, int h[][2], int L[])
{
    n = N;
    k = K;
    for(int i = 0, u, v; i < n - 1; ++i) {
        u = h[i][0];
        v = h[i][1];
        adj[u].push_back(ii(L[i], v));
        adj[v].push_back(ii(L[i], u));
    }
    ans = -1;
    memset(cnt, -1, sizeof(cnt));
    centroid();
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9052 KB Output is correct
2 Correct 3 ms 9052 KB Output is correct
3 Correct 3 ms 9052 KB Output is correct
4 Correct 3 ms 9052 KB Output is correct
5 Correct 3 ms 9052 KB Output is correct
6 Correct 3 ms 9052 KB Output is correct
7 Correct 3 ms 9052 KB Output is correct
8 Correct 4 ms 9052 KB Output is correct
9 Correct 4 ms 8840 KB Output is correct
10 Correct 3 ms 9048 KB Output is correct
11 Correct 3 ms 9080 KB Output is correct
12 Correct 3 ms 9304 KB Output is correct
13 Correct 3 ms 9048 KB Output is correct
14 Correct 4 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 3 ms 9052 KB Output is correct
17 Correct 3 ms 9052 KB Output is correct
18 Correct 3 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9052 KB Output is correct
2 Correct 3 ms 9052 KB Output is correct
3 Correct 3 ms 9052 KB Output is correct
4 Correct 3 ms 9052 KB Output is correct
5 Correct 3 ms 9052 KB Output is correct
6 Correct 3 ms 9052 KB Output is correct
7 Correct 3 ms 9052 KB Output is correct
8 Correct 4 ms 9052 KB Output is correct
9 Correct 4 ms 8840 KB Output is correct
10 Correct 3 ms 9048 KB Output is correct
11 Correct 3 ms 9080 KB Output is correct
12 Correct 3 ms 9304 KB Output is correct
13 Correct 3 ms 9048 KB Output is correct
14 Correct 4 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 3 ms 9052 KB Output is correct
17 Correct 3 ms 9052 KB Output is correct
18 Correct 3 ms 9052 KB Output is correct
19 Correct 3 ms 9048 KB Output is correct
20 Correct 3 ms 9052 KB Output is correct
21 Correct 4 ms 9048 KB Output is correct
22 Correct 4 ms 9048 KB Output is correct
23 Correct 6 ms 9052 KB Output is correct
24 Correct 4 ms 9052 KB Output is correct
25 Correct 4 ms 9052 KB Output is correct
26 Correct 4 ms 9052 KB Output is correct
27 Correct 4 ms 8956 KB Output is correct
28 Correct 4 ms 9048 KB Output is correct
29 Correct 4 ms 8908 KB Output is correct
30 Correct 4 ms 9052 KB Output is correct
31 Correct 4 ms 9052 KB Output is correct
32 Correct 4 ms 9052 KB Output is correct
33 Correct 4 ms 9052 KB Output is correct
34 Correct 4 ms 8940 KB Output is correct
35 Correct 4 ms 8912 KB Output is correct
36 Correct 4 ms 9052 KB Output is correct
37 Correct 4 ms 9052 KB Output is correct
38 Correct 4 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9052 KB Output is correct
2 Correct 3 ms 9052 KB Output is correct
3 Correct 3 ms 9052 KB Output is correct
4 Correct 3 ms 9052 KB Output is correct
5 Correct 3 ms 9052 KB Output is correct
6 Correct 3 ms 9052 KB Output is correct
7 Correct 3 ms 9052 KB Output is correct
8 Correct 4 ms 9052 KB Output is correct
9 Correct 4 ms 8840 KB Output is correct
10 Correct 3 ms 9048 KB Output is correct
11 Correct 3 ms 9080 KB Output is correct
12 Correct 3 ms 9304 KB Output is correct
13 Correct 3 ms 9048 KB Output is correct
14 Correct 4 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 3 ms 9052 KB Output is correct
17 Correct 3 ms 9052 KB Output is correct
18 Correct 3 ms 9052 KB Output is correct
19 Correct 91 ms 14672 KB Output is correct
20 Correct 104 ms 14424 KB Output is correct
21 Correct 97 ms 14540 KB Output is correct
22 Correct 85 ms 14684 KB Output is correct
23 Correct 44 ms 14676 KB Output is correct
24 Correct 42 ms 14932 KB Output is correct
25 Correct 87 ms 17236 KB Output is correct
26 Correct 74 ms 20304 KB Output is correct
27 Correct 97 ms 20352 KB Output is correct
28 Correct 192 ms 31824 KB Output is correct
29 Correct 147 ms 30548 KB Output is correct
30 Correct 93 ms 20304 KB Output is correct
31 Correct 121 ms 20304 KB Output is correct
32 Correct 116 ms 21568 KB Output is correct
33 Correct 160 ms 20416 KB Output is correct
34 Correct 112 ms 20848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9052 KB Output is correct
2 Correct 3 ms 9052 KB Output is correct
3 Correct 3 ms 9052 KB Output is correct
4 Correct 3 ms 9052 KB Output is correct
5 Correct 3 ms 9052 KB Output is correct
6 Correct 3 ms 9052 KB Output is correct
7 Correct 3 ms 9052 KB Output is correct
8 Correct 4 ms 9052 KB Output is correct
9 Correct 4 ms 8840 KB Output is correct
10 Correct 3 ms 9048 KB Output is correct
11 Correct 3 ms 9080 KB Output is correct
12 Correct 3 ms 9304 KB Output is correct
13 Correct 3 ms 9048 KB Output is correct
14 Correct 4 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 3 ms 9052 KB Output is correct
17 Correct 3 ms 9052 KB Output is correct
18 Correct 3 ms 9052 KB Output is correct
19 Correct 3 ms 9048 KB Output is correct
20 Correct 3 ms 9052 KB Output is correct
21 Correct 4 ms 9048 KB Output is correct
22 Correct 4 ms 9048 KB Output is correct
23 Correct 6 ms 9052 KB Output is correct
24 Correct 4 ms 9052 KB Output is correct
25 Correct 4 ms 9052 KB Output is correct
26 Correct 4 ms 9052 KB Output is correct
27 Correct 4 ms 8956 KB Output is correct
28 Correct 4 ms 9048 KB Output is correct
29 Correct 4 ms 8908 KB Output is correct
30 Correct 4 ms 9052 KB Output is correct
31 Correct 4 ms 9052 KB Output is correct
32 Correct 4 ms 9052 KB Output is correct
33 Correct 4 ms 9052 KB Output is correct
34 Correct 4 ms 8940 KB Output is correct
35 Correct 4 ms 8912 KB Output is correct
36 Correct 4 ms 9052 KB Output is correct
37 Correct 4 ms 9052 KB Output is correct
38 Correct 4 ms 9052 KB Output is correct
39 Correct 91 ms 14672 KB Output is correct
40 Correct 104 ms 14424 KB Output is correct
41 Correct 97 ms 14540 KB Output is correct
42 Correct 85 ms 14684 KB Output is correct
43 Correct 44 ms 14676 KB Output is correct
44 Correct 42 ms 14932 KB Output is correct
45 Correct 87 ms 17236 KB Output is correct
46 Correct 74 ms 20304 KB Output is correct
47 Correct 97 ms 20352 KB Output is correct
48 Correct 192 ms 31824 KB Output is correct
49 Correct 147 ms 30548 KB Output is correct
50 Correct 93 ms 20304 KB Output is correct
51 Correct 121 ms 20304 KB Output is correct
52 Correct 116 ms 21568 KB Output is correct
53 Correct 160 ms 20416 KB Output is correct
54 Correct 112 ms 20848 KB Output is correct
55 Correct 9 ms 9560 KB Output is correct
56 Correct 10 ms 9640 KB Output is correct
57 Correct 58 ms 14928 KB Output is correct
58 Correct 24 ms 15568 KB Output is correct
59 Correct 77 ms 21036 KB Output is correct
60 Correct 290 ms 32280 KB Output is correct
61 Correct 110 ms 21332 KB Output is correct
62 Correct 119 ms 21588 KB Output is correct
63 Correct 148 ms 21784 KB Output is correct
64 Correct 255 ms 21032 KB Output is correct
65 Correct 101 ms 22092 KB Output is correct
66 Correct 237 ms 28596 KB Output is correct
67 Correct 60 ms 23608 KB Output is correct
68 Correct 162 ms 22864 KB Output is correct
69 Correct 144 ms 20820 KB Output is correct
70 Correct 125 ms 22356 KB Output is correct