Submission #1003573

# Submission time Handle Problem Language Result Execution time Memory
1003573 2024-06-20T13:07:51 Z ilef Race (IOI11_race) C++17
100 / 100
289 ms 33372 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 9048 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 4 ms 9052 KB Output is correct
6 Correct 3 ms 9052 KB Output is correct
7 Correct 4 ms 9028 KB Output is correct
8 Correct 3 ms 9052 KB Output is correct
9 Correct 3 ms 9052 KB Output is correct
10 Correct 4 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 4 ms 9052 KB Output is correct
13 Correct 3 ms 8924 KB Output is correct
14 Correct 4 ms 9036 KB Output is correct
15 Correct 4 ms 8984 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 9048 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 4 ms 9052 KB Output is correct
6 Correct 3 ms 9052 KB Output is correct
7 Correct 4 ms 9028 KB Output is correct
8 Correct 3 ms 9052 KB Output is correct
9 Correct 3 ms 9052 KB Output is correct
10 Correct 4 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 4 ms 9052 KB Output is correct
13 Correct 3 ms 8924 KB Output is correct
14 Correct 4 ms 9036 KB Output is correct
15 Correct 4 ms 8984 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 8868 KB Output is correct
20 Correct 3 ms 9052 KB Output is correct
21 Correct 4 ms 9052 KB Output is correct
22 Correct 4 ms 9052 KB Output is correct
23 Correct 4 ms 9052 KB Output is correct
24 Correct 4 ms 9052 KB Output is correct
25 Correct 4 ms 9024 KB Output is correct
26 Correct 4 ms 9052 KB Output is correct
27 Correct 4 ms 9052 KB Output is correct
28 Correct 4 ms 9052 KB Output is correct
29 Correct 3 ms 9052 KB Output is correct
30 Correct 4 ms 9052 KB Output is correct
31 Correct 4 ms 9052 KB Output is correct
32 Correct 6 ms 9048 KB Output is correct
33 Correct 4 ms 9052 KB Output is correct
34 Correct 3 ms 9052 KB Output is correct
35 Correct 3 ms 9052 KB Output is correct
36 Correct 4 ms 8916 KB Output is correct
37 Correct 4 ms 9052 KB Output is correct
38 Correct 4 ms 8900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9048 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 4 ms 9052 KB Output is correct
6 Correct 3 ms 9052 KB Output is correct
7 Correct 4 ms 9028 KB Output is correct
8 Correct 3 ms 9052 KB Output is correct
9 Correct 3 ms 9052 KB Output is correct
10 Correct 4 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 4 ms 9052 KB Output is correct
13 Correct 3 ms 8924 KB Output is correct
14 Correct 4 ms 9036 KB Output is correct
15 Correct 4 ms 8984 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 93 ms 14608 KB Output is correct
20 Correct 88 ms 15700 KB Output is correct
21 Correct 91 ms 15296 KB Output is correct
22 Correct 89 ms 15956 KB Output is correct
23 Correct 47 ms 16224 KB Output is correct
24 Correct 37 ms 15952 KB Output is correct
25 Correct 90 ms 18520 KB Output is correct
26 Correct 89 ms 21072 KB Output is correct
27 Correct 91 ms 21840 KB Output is correct
28 Correct 147 ms 33372 KB Output is correct
29 Correct 151 ms 32596 KB Output is correct
30 Correct 109 ms 22608 KB Output is correct
31 Correct 104 ms 22608 KB Output is correct
32 Correct 102 ms 22352 KB Output is correct
33 Correct 138 ms 21584 KB Output is correct
34 Correct 107 ms 21912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9048 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 4 ms 9052 KB Output is correct
6 Correct 3 ms 9052 KB Output is correct
7 Correct 4 ms 9028 KB Output is correct
8 Correct 3 ms 9052 KB Output is correct
9 Correct 3 ms 9052 KB Output is correct
10 Correct 4 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 4 ms 9052 KB Output is correct
13 Correct 3 ms 8924 KB Output is correct
14 Correct 4 ms 9036 KB Output is correct
15 Correct 4 ms 8984 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 8868 KB Output is correct
20 Correct 3 ms 9052 KB Output is correct
21 Correct 4 ms 9052 KB Output is correct
22 Correct 4 ms 9052 KB Output is correct
23 Correct 4 ms 9052 KB Output is correct
24 Correct 4 ms 9052 KB Output is correct
25 Correct 4 ms 9024 KB Output is correct
26 Correct 4 ms 9052 KB Output is correct
27 Correct 4 ms 9052 KB Output is correct
28 Correct 4 ms 9052 KB Output is correct
29 Correct 3 ms 9052 KB Output is correct
30 Correct 4 ms 9052 KB Output is correct
31 Correct 4 ms 9052 KB Output is correct
32 Correct 6 ms 9048 KB Output is correct
33 Correct 4 ms 9052 KB Output is correct
34 Correct 3 ms 9052 KB Output is correct
35 Correct 3 ms 9052 KB Output is correct
36 Correct 4 ms 8916 KB Output is correct
37 Correct 4 ms 9052 KB Output is correct
38 Correct 4 ms 8900 KB Output is correct
39 Correct 93 ms 14608 KB Output is correct
40 Correct 88 ms 15700 KB Output is correct
41 Correct 91 ms 15296 KB Output is correct
42 Correct 89 ms 15956 KB Output is correct
43 Correct 47 ms 16224 KB Output is correct
44 Correct 37 ms 15952 KB Output is correct
45 Correct 90 ms 18520 KB Output is correct
46 Correct 89 ms 21072 KB Output is correct
47 Correct 91 ms 21840 KB Output is correct
48 Correct 147 ms 33372 KB Output is correct
49 Correct 151 ms 32596 KB Output is correct
50 Correct 109 ms 22608 KB Output is correct
51 Correct 104 ms 22608 KB Output is correct
52 Correct 102 ms 22352 KB Output is correct
53 Correct 138 ms 21584 KB Output is correct
54 Correct 107 ms 21912 KB Output is correct
55 Correct 9 ms 9560 KB Output is correct
56 Correct 10 ms 9564 KB Output is correct
57 Correct 51 ms 14984 KB Output is correct
58 Correct 25 ms 15840 KB Output is correct
59 Correct 68 ms 21248 KB Output is correct
60 Correct 289 ms 32852 KB Output is correct
61 Correct 96 ms 21860 KB Output is correct
62 Correct 111 ms 22608 KB Output is correct
63 Correct 137 ms 22404 KB Output is correct
64 Correct 250 ms 21168 KB Output is correct
65 Correct 96 ms 23632 KB Output is correct
66 Correct 224 ms 28688 KB Output is correct
67 Correct 64 ms 24104 KB Output is correct
68 Correct 131 ms 22864 KB Output is correct
69 Correct 123 ms 20860 KB Output is correct
70 Correct 115 ms 22864 KB Output is correct