Submission #541602

# Submission time Handle Problem Language Result Execution time Memory
541602 2022-03-23T20:21:52 Z Evang Race (IOI11_race) C++17
100 / 100
581 ms 32548 KB
/*
 *  Written by
 *       ______  _   _  ______  ____  ____  ____  ____
 *      |_    _|| |_| ||_    _||_   || __ || __ |/  _/
 *        |  |  |  _  |  |  |    / / ||  ||||  ||| |__
 *        |  |  | | | |  |  |   | |_ ||__||||__||\__  \
 *        |__|  |_| |_|  |__|   |___||____||____|/____/
 */

#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 5 ms 8916 KB Output is correct
2 Correct 4 ms 8916 KB Output is correct
3 Correct 4 ms 8852 KB Output is correct
4 Correct 5 ms 8948 KB Output is correct
5 Correct 5 ms 8916 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 4 ms 8916 KB Output is correct
8 Correct 5 ms 8916 KB Output is correct
9 Correct 4 ms 8916 KB Output is correct
10 Correct 4 ms 8916 KB Output is correct
11 Correct 5 ms 8856 KB Output is correct
12 Correct 5 ms 8916 KB Output is correct
13 Correct 5 ms 8916 KB Output is correct
14 Correct 6 ms 8916 KB Output is correct
15 Correct 4 ms 8916 KB Output is correct
16 Correct 4 ms 8852 KB Output is correct
17 Correct 4 ms 8864 KB Output is correct
18 Correct 5 ms 8916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8916 KB Output is correct
2 Correct 4 ms 8916 KB Output is correct
3 Correct 4 ms 8852 KB Output is correct
4 Correct 5 ms 8948 KB Output is correct
5 Correct 5 ms 8916 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 4 ms 8916 KB Output is correct
8 Correct 5 ms 8916 KB Output is correct
9 Correct 4 ms 8916 KB Output is correct
10 Correct 4 ms 8916 KB Output is correct
11 Correct 5 ms 8856 KB Output is correct
12 Correct 5 ms 8916 KB Output is correct
13 Correct 5 ms 8916 KB Output is correct
14 Correct 6 ms 8916 KB Output is correct
15 Correct 4 ms 8916 KB Output is correct
16 Correct 4 ms 8852 KB Output is correct
17 Correct 4 ms 8864 KB Output is correct
18 Correct 5 ms 8916 KB Output is correct
19 Correct 5 ms 8916 KB Output is correct
20 Correct 4 ms 8916 KB Output is correct
21 Correct 5 ms 8996 KB Output is correct
22 Correct 7 ms 8916 KB Output is correct
23 Correct 4 ms 8916 KB Output is correct
24 Correct 5 ms 8916 KB Output is correct
25 Correct 5 ms 8916 KB Output is correct
26 Correct 5 ms 8916 KB Output is correct
27 Correct 5 ms 8992 KB Output is correct
28 Correct 5 ms 8916 KB Output is correct
29 Correct 5 ms 8916 KB Output is correct
30 Correct 6 ms 8916 KB Output is correct
31 Correct 5 ms 8996 KB Output is correct
32 Correct 5 ms 8916 KB Output is correct
33 Correct 6 ms 8992 KB Output is correct
34 Correct 5 ms 8916 KB Output is correct
35 Correct 5 ms 8992 KB Output is correct
36 Correct 5 ms 8916 KB Output is correct
37 Correct 5 ms 8916 KB Output is correct
38 Correct 5 ms 8916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8916 KB Output is correct
2 Correct 4 ms 8916 KB Output is correct
3 Correct 4 ms 8852 KB Output is correct
4 Correct 5 ms 8948 KB Output is correct
5 Correct 5 ms 8916 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 4 ms 8916 KB Output is correct
8 Correct 5 ms 8916 KB Output is correct
9 Correct 4 ms 8916 KB Output is correct
10 Correct 4 ms 8916 KB Output is correct
11 Correct 5 ms 8856 KB Output is correct
12 Correct 5 ms 8916 KB Output is correct
13 Correct 5 ms 8916 KB Output is correct
14 Correct 6 ms 8916 KB Output is correct
15 Correct 4 ms 8916 KB Output is correct
16 Correct 4 ms 8852 KB Output is correct
17 Correct 4 ms 8864 KB Output is correct
18 Correct 5 ms 8916 KB Output is correct
19 Correct 150 ms 15780 KB Output is correct
20 Correct 124 ms 15568 KB Output is correct
21 Correct 147 ms 15708 KB Output is correct
22 Correct 146 ms 15768 KB Output is correct
23 Correct 75 ms 16008 KB Output is correct
24 Correct 59 ms 15732 KB Output is correct
25 Correct 119 ms 18456 KB Output is correct
26 Correct 103 ms 21324 KB Output is correct
27 Correct 191 ms 21448 KB Output is correct
28 Correct 266 ms 32548 KB Output is correct
29 Correct 259 ms 31640 KB Output is correct
30 Correct 159 ms 21324 KB Output is correct
31 Correct 172 ms 21336 KB Output is correct
32 Correct 204 ms 21460 KB Output is correct
33 Correct 208 ms 20228 KB Output is correct
34 Correct 185 ms 20140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8916 KB Output is correct
2 Correct 4 ms 8916 KB Output is correct
3 Correct 4 ms 8852 KB Output is correct
4 Correct 5 ms 8948 KB Output is correct
5 Correct 5 ms 8916 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 4 ms 8916 KB Output is correct
8 Correct 5 ms 8916 KB Output is correct
9 Correct 4 ms 8916 KB Output is correct
10 Correct 4 ms 8916 KB Output is correct
11 Correct 5 ms 8856 KB Output is correct
12 Correct 5 ms 8916 KB Output is correct
13 Correct 5 ms 8916 KB Output is correct
14 Correct 6 ms 8916 KB Output is correct
15 Correct 4 ms 8916 KB Output is correct
16 Correct 4 ms 8852 KB Output is correct
17 Correct 4 ms 8864 KB Output is correct
18 Correct 5 ms 8916 KB Output is correct
19 Correct 5 ms 8916 KB Output is correct
20 Correct 4 ms 8916 KB Output is correct
21 Correct 5 ms 8996 KB Output is correct
22 Correct 7 ms 8916 KB Output is correct
23 Correct 4 ms 8916 KB Output is correct
24 Correct 5 ms 8916 KB Output is correct
25 Correct 5 ms 8916 KB Output is correct
26 Correct 5 ms 8916 KB Output is correct
27 Correct 5 ms 8992 KB Output is correct
28 Correct 5 ms 8916 KB Output is correct
29 Correct 5 ms 8916 KB Output is correct
30 Correct 6 ms 8916 KB Output is correct
31 Correct 5 ms 8996 KB Output is correct
32 Correct 5 ms 8916 KB Output is correct
33 Correct 6 ms 8992 KB Output is correct
34 Correct 5 ms 8916 KB Output is correct
35 Correct 5 ms 8992 KB Output is correct
36 Correct 5 ms 8916 KB Output is correct
37 Correct 5 ms 8916 KB Output is correct
38 Correct 5 ms 8916 KB Output is correct
39 Correct 150 ms 15780 KB Output is correct
40 Correct 124 ms 15568 KB Output is correct
41 Correct 147 ms 15708 KB Output is correct
42 Correct 146 ms 15768 KB Output is correct
43 Correct 75 ms 16008 KB Output is correct
44 Correct 59 ms 15732 KB Output is correct
45 Correct 119 ms 18456 KB Output is correct
46 Correct 103 ms 21324 KB Output is correct
47 Correct 191 ms 21448 KB Output is correct
48 Correct 266 ms 32548 KB Output is correct
49 Correct 259 ms 31640 KB Output is correct
50 Correct 159 ms 21324 KB Output is correct
51 Correct 172 ms 21336 KB Output is correct
52 Correct 204 ms 21460 KB Output is correct
53 Correct 208 ms 20228 KB Output is correct
54 Correct 185 ms 20140 KB Output is correct
55 Correct 13 ms 9592 KB Output is correct
56 Correct 14 ms 9584 KB Output is correct
57 Correct 85 ms 15900 KB Output is correct
58 Correct 43 ms 15944 KB Output is correct
59 Correct 99 ms 21328 KB Output is correct
60 Correct 482 ms 31984 KB Output is correct
61 Correct 183 ms 21356 KB Output is correct
62 Correct 214 ms 21264 KB Output is correct
63 Correct 271 ms 21456 KB Output is correct
64 Correct 581 ms 21216 KB Output is correct
65 Correct 223 ms 21364 KB Output is correct
66 Correct 410 ms 29668 KB Output is correct
67 Correct 108 ms 21696 KB Output is correct
68 Correct 315 ms 21556 KB Output is correct
69 Correct 252 ms 21672 KB Output is correct
70 Correct 258 ms 21108 KB Output is correct