Submission #716651

# Submission time Handle Problem Language Result Execution time Memory
716651 2023-03-30T16:53:11 Z sheldon Race (IOI11_race) C++14
100 / 100
1586 ms 54148 KB
#include <bits/stdc++.h>

using namespace std;

const int nax = 2e5 + 5;

vector<pair<int, int>> edges[nax];
int sz[nax];
bool dead[nax];

int cent (int u, int p, int comp) {
    for (auto v : edges[u]) {
        if (p != v.first && !dead[v.first] && sz[v.first] * 2 > comp) {
            return cent (v.first, u, comp);
        }
    }
    return u;
}

void dfs (int u, int p) {
    sz[u] = 1;
    for (auto v : edges[u]) {
        if (v.first != p && !dead[v.first]) {
            dfs (v.first, u);
            sz[u] += sz[v.first];
        }
    }
}
map<long long, int> mp;

void dfs2 (int u, int p, long long x, int depth) {
    if (mp[x] == 0 && x != 0) {
        mp[x] = depth;
    }
    mp[x] = min(mp[x], depth);
    for (auto v : edges[u]) {
        if (v.first != p && !dead[v.first]) {
            dfs2 (v.first, u, x + v.second, depth + 1);
        }
    }
}
int ans = 1e9;
int K;
void dfs3 (int u, int p, long long x, int depth) {
    if ((K - x > 0 && mp[K - x] > 0) || (K - x == 0))
        ans = min(ans, depth + mp[K - x]);
    for (auto v : edges[u]) {
        if (v.first != p && !dead[v.first]) {
            dfs3 (v.first, u, x + v.second, depth + 1);
        }
    }
}   

void decomp (int u) {
    dfs (u, - 1);
    int C = cent(u, -1, sz[u]);
    for (auto v : edges[C]) {
        if (!dead[v.first]) {
            dfs3(v.first, C, v.second, 1);
            dfs2 (v.first, C, v.second, 1);
        }
    }
    dead[C] = 1;
    mp.clear();
    for (auto v : edges[C]) {
        if (!dead[v.first])
            decomp (v.first);
    }
}

int best_path(int n, int k, int H[][2], int l[]) {
    K = k;
    for (int i = 0; i < n - 1; ++i) {
        int u = H[i][0];
        int v = H[i][1];
        int x = l[i];
        ++u, ++v;
        edges[u].push_back({v, x});
        edges[v].push_back({u, x});
    }
    decomp (1);
    return (ans == 1e9 ? -1 : ans);
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5020 KB Output is correct
5 Correct 4 ms 5016 KB Output is correct
6 Correct 3 ms 5004 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5012 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5020 KB Output is correct
5 Correct 4 ms 5016 KB Output is correct
6 Correct 3 ms 5004 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5012 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 6 ms 5152 KB Output is correct
23 Correct 5 ms 5076 KB Output is correct
24 Correct 5 ms 5076 KB Output is correct
25 Correct 5 ms 5204 KB Output is correct
26 Correct 4 ms 5152 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 6 ms 5076 KB Output is correct
29 Correct 5 ms 5204 KB Output is correct
30 Correct 5 ms 5204 KB Output is correct
31 Correct 5 ms 5204 KB Output is correct
32 Correct 5 ms 5148 KB Output is correct
33 Correct 6 ms 5204 KB Output is correct
34 Correct 5 ms 5076 KB Output is correct
35 Correct 5 ms 5144 KB Output is correct
36 Correct 5 ms 5076 KB Output is correct
37 Correct 6 ms 5148 KB Output is correct
38 Correct 6 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5020 KB Output is correct
5 Correct 4 ms 5016 KB Output is correct
6 Correct 3 ms 5004 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5012 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 270 ms 11796 KB Output is correct
20 Correct 270 ms 11692 KB Output is correct
21 Correct 252 ms 11596 KB Output is correct
22 Correct 225 ms 11800 KB Output is correct
23 Correct 317 ms 12004 KB Output is correct
24 Correct 144 ms 11928 KB Output is correct
25 Correct 202 ms 16168 KB Output is correct
26 Correct 109 ms 18964 KB Output is correct
27 Correct 337 ms 19012 KB Output is correct
28 Correct 1066 ms 45676 KB Output is correct
29 Correct 1056 ms 44664 KB Output is correct
30 Correct 340 ms 19056 KB Output is correct
31 Correct 340 ms 18916 KB Output is correct
32 Correct 385 ms 19108 KB Output is correct
33 Correct 535 ms 17800 KB Output is correct
34 Correct 988 ms 30976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5020 KB Output is correct
5 Correct 4 ms 5016 KB Output is correct
6 Correct 3 ms 5004 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5012 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 6 ms 5152 KB Output is correct
23 Correct 5 ms 5076 KB Output is correct
24 Correct 5 ms 5076 KB Output is correct
25 Correct 5 ms 5204 KB Output is correct
26 Correct 4 ms 5152 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 6 ms 5076 KB Output is correct
29 Correct 5 ms 5204 KB Output is correct
30 Correct 5 ms 5204 KB Output is correct
31 Correct 5 ms 5204 KB Output is correct
32 Correct 5 ms 5148 KB Output is correct
33 Correct 6 ms 5204 KB Output is correct
34 Correct 5 ms 5076 KB Output is correct
35 Correct 5 ms 5144 KB Output is correct
36 Correct 5 ms 5076 KB Output is correct
37 Correct 6 ms 5148 KB Output is correct
38 Correct 6 ms 5076 KB Output is correct
39 Correct 270 ms 11796 KB Output is correct
40 Correct 270 ms 11692 KB Output is correct
41 Correct 252 ms 11596 KB Output is correct
42 Correct 225 ms 11800 KB Output is correct
43 Correct 317 ms 12004 KB Output is correct
44 Correct 144 ms 11928 KB Output is correct
45 Correct 202 ms 16168 KB Output is correct
46 Correct 109 ms 18964 KB Output is correct
47 Correct 337 ms 19012 KB Output is correct
48 Correct 1066 ms 45676 KB Output is correct
49 Correct 1056 ms 44664 KB Output is correct
50 Correct 340 ms 19056 KB Output is correct
51 Correct 340 ms 18916 KB Output is correct
52 Correct 385 ms 19108 KB Output is correct
53 Correct 535 ms 17800 KB Output is correct
54 Correct 988 ms 30976 KB Output is correct
55 Correct 32 ms 6112 KB Output is correct
56 Correct 20 ms 5588 KB Output is correct
57 Correct 184 ms 11796 KB Output is correct
58 Correct 42 ms 11676 KB Output is correct
59 Correct 496 ms 27824 KB Output is correct
60 Correct 1528 ms 54148 KB Output is correct
61 Correct 477 ms 20940 KB Output is correct
62 Correct 414 ms 19348 KB Output is correct
63 Correct 452 ms 19388 KB Output is correct
64 Correct 1586 ms 31336 KB Output is correct
65 Correct 978 ms 32080 KB Output is correct
66 Correct 1315 ms 42316 KB Output is correct
67 Correct 129 ms 19772 KB Output is correct
68 Correct 720 ms 34964 KB Output is correct
69 Correct 764 ms 35156 KB Output is correct
70 Correct 706 ms 33632 KB Output is correct