Submission #998190

# Submission time Handle Problem Language Result Execution time Memory
998190 2024-06-13T11:41:16 Z The_Samurai Race (IOI11_race) C++17
100 / 100
244 ms 63316 KB
#include "bits/stdc++.h"
#include "race.h"
using namespace std;
using ll = long long;
const int inf = 1e9, N = 2e5 + 5;
int ans = inf, k, z = 0;

vector<vector<pair<int, int>>> g(N);
vector<int> par(N, -1), heavy(N), height2(N);
vector<ll> height(N);
vector<set<pair<ll, int>>> st;

/*
*/

int dfs(int u) {
    int size = 1, max_child_size = 0;
    for (auto [v, w]: g[u]) {
        if (par[u] == v) continue;
        height[v] = height[u] + w;
        height2[v] = height2[u] + 1;
        par[v] = u;
        int child_size = dfs(v);
        if (child_size > max_child_size) {
            heavy[u] = heavy[v];
            max_child_size = child_size;
        }
        size += child_size;
    }
    if (size == 1) {
        heavy[u] = z++;
        st.emplace_back();
    }
    return size;
}

void dfs2(int u) {
    for (auto [v, w]: g[u]) {
        if (par[u] == v) continue;
        dfs2(v);
    }
    for (auto [v, w]: g[u]) {
        if (par[u] == v or heavy[u] == heavy[v]) continue;
        for (auto it: st[heavy[v]]) {
            ll need = k - (it.first - height[u]) + height[u];
            auto it2 = st[heavy[u]].lower_bound(make_pair(need, 0));
            if (it2 != st[heavy[u]].end() and it2->first == need) {
                ans = min(ans, it.second + it2->second - 2 * height2[u]);
            }
        }
        for (auto it: st[heavy[v]]) {
            st[heavy[u]].insert(it);
        }
        st[heavy[v]].clear();
    }
    ll need = k + height[u];
    auto it = st[heavy[u]].lower_bound(make_pair(need, 0));
    if (it != st[heavy[u]].end() and it->first == need) {
        ans = min(ans, it->second - height2[u]);
    }
    st[heavy[u]].emplace(height[u], height2[u]);
    // vector<pair<ll, int>> del;
    // pair<ll, int> last = {-1, -1};
    // for (auto it: st[heavy[u]]) {
    //     if (it.first == last.first) {
    //         del.emplace_back(it);
    //     }
    //     last = it;
    // }
    // for (auto it: del) st[heavy[u]].erase(it);
}

int best_path(int n, int K, int H[][2], int L[]) {
    k = K;
    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]);
    }
    // change it
    dfs(0);
    dfs2(0);
    return ans == inf ? -1 : ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11096 KB Output is correct
2 Correct 3 ms 11100 KB Output is correct
3 Correct 3 ms 11100 KB Output is correct
4 Correct 3 ms 11100 KB Output is correct
5 Correct 2 ms 11100 KB Output is correct
6 Correct 3 ms 11100 KB Output is correct
7 Correct 3 ms 11100 KB Output is correct
8 Correct 3 ms 11100 KB Output is correct
9 Correct 3 ms 11100 KB Output is correct
10 Correct 3 ms 11100 KB Output is correct
11 Correct 4 ms 11100 KB Output is correct
12 Correct 4 ms 11100 KB Output is correct
13 Correct 3 ms 11100 KB Output is correct
14 Correct 3 ms 11100 KB Output is correct
15 Correct 4 ms 11012 KB Output is correct
16 Correct 3 ms 11100 KB Output is correct
17 Correct 4 ms 11100 KB Output is correct
18 Correct 2 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11096 KB Output is correct
2 Correct 3 ms 11100 KB Output is correct
3 Correct 3 ms 11100 KB Output is correct
4 Correct 3 ms 11100 KB Output is correct
5 Correct 2 ms 11100 KB Output is correct
6 Correct 3 ms 11100 KB Output is correct
7 Correct 3 ms 11100 KB Output is correct
8 Correct 3 ms 11100 KB Output is correct
9 Correct 3 ms 11100 KB Output is correct
10 Correct 3 ms 11100 KB Output is correct
11 Correct 4 ms 11100 KB Output is correct
12 Correct 4 ms 11100 KB Output is correct
13 Correct 3 ms 11100 KB Output is correct
14 Correct 3 ms 11100 KB Output is correct
15 Correct 4 ms 11012 KB Output is correct
16 Correct 3 ms 11100 KB Output is correct
17 Correct 4 ms 11100 KB Output is correct
18 Correct 2 ms 11100 KB Output is correct
19 Correct 4 ms 11096 KB Output is correct
20 Correct 3 ms 11096 KB Output is correct
21 Correct 4 ms 11100 KB Output is correct
22 Correct 3 ms 11100 KB Output is correct
23 Correct 4 ms 11100 KB Output is correct
24 Correct 4 ms 11100 KB Output is correct
25 Correct 4 ms 11120 KB Output is correct
26 Correct 4 ms 11120 KB Output is correct
27 Correct 3 ms 11100 KB Output is correct
28 Correct 3 ms 11156 KB Output is correct
29 Correct 3 ms 11100 KB Output is correct
30 Correct 4 ms 11124 KB Output is correct
31 Correct 4 ms 11100 KB Output is correct
32 Correct 4 ms 11100 KB Output is correct
33 Correct 4 ms 11100 KB Output is correct
34 Correct 4 ms 11100 KB Output is correct
35 Correct 3 ms 11100 KB Output is correct
36 Correct 3 ms 11100 KB Output is correct
37 Correct 4 ms 11100 KB Output is correct
38 Correct 3 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11096 KB Output is correct
2 Correct 3 ms 11100 KB Output is correct
3 Correct 3 ms 11100 KB Output is correct
4 Correct 3 ms 11100 KB Output is correct
5 Correct 2 ms 11100 KB Output is correct
6 Correct 3 ms 11100 KB Output is correct
7 Correct 3 ms 11100 KB Output is correct
8 Correct 3 ms 11100 KB Output is correct
9 Correct 3 ms 11100 KB Output is correct
10 Correct 3 ms 11100 KB Output is correct
11 Correct 4 ms 11100 KB Output is correct
12 Correct 4 ms 11100 KB Output is correct
13 Correct 3 ms 11100 KB Output is correct
14 Correct 3 ms 11100 KB Output is correct
15 Correct 4 ms 11012 KB Output is correct
16 Correct 3 ms 11100 KB Output is correct
17 Correct 4 ms 11100 KB Output is correct
18 Correct 2 ms 11100 KB Output is correct
19 Correct 77 ms 20688 KB Output is correct
20 Correct 73 ms 20088 KB Output is correct
21 Correct 68 ms 21696 KB Output is correct
22 Correct 69 ms 20180 KB Output is correct
23 Correct 93 ms 20360 KB Output is correct
24 Correct 62 ms 24012 KB Output is correct
25 Correct 78 ms 33876 KB Output is correct
26 Correct 52 ms 38228 KB Output is correct
27 Correct 113 ms 32728 KB Output is correct
28 Correct 159 ms 63316 KB Output is correct
29 Correct 155 ms 61724 KB Output is correct
30 Correct 110 ms 31932 KB Output is correct
31 Correct 115 ms 31928 KB Output is correct
32 Correct 170 ms 33480 KB Output is correct
33 Correct 98 ms 20508 KB Output is correct
34 Correct 173 ms 33104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11096 KB Output is correct
2 Correct 3 ms 11100 KB Output is correct
3 Correct 3 ms 11100 KB Output is correct
4 Correct 3 ms 11100 KB Output is correct
5 Correct 2 ms 11100 KB Output is correct
6 Correct 3 ms 11100 KB Output is correct
7 Correct 3 ms 11100 KB Output is correct
8 Correct 3 ms 11100 KB Output is correct
9 Correct 3 ms 11100 KB Output is correct
10 Correct 3 ms 11100 KB Output is correct
11 Correct 4 ms 11100 KB Output is correct
12 Correct 4 ms 11100 KB Output is correct
13 Correct 3 ms 11100 KB Output is correct
14 Correct 3 ms 11100 KB Output is correct
15 Correct 4 ms 11012 KB Output is correct
16 Correct 3 ms 11100 KB Output is correct
17 Correct 4 ms 11100 KB Output is correct
18 Correct 2 ms 11100 KB Output is correct
19 Correct 4 ms 11096 KB Output is correct
20 Correct 3 ms 11096 KB Output is correct
21 Correct 4 ms 11100 KB Output is correct
22 Correct 3 ms 11100 KB Output is correct
23 Correct 4 ms 11100 KB Output is correct
24 Correct 4 ms 11100 KB Output is correct
25 Correct 4 ms 11120 KB Output is correct
26 Correct 4 ms 11120 KB Output is correct
27 Correct 3 ms 11100 KB Output is correct
28 Correct 3 ms 11156 KB Output is correct
29 Correct 3 ms 11100 KB Output is correct
30 Correct 4 ms 11124 KB Output is correct
31 Correct 4 ms 11100 KB Output is correct
32 Correct 4 ms 11100 KB Output is correct
33 Correct 4 ms 11100 KB Output is correct
34 Correct 4 ms 11100 KB Output is correct
35 Correct 3 ms 11100 KB Output is correct
36 Correct 3 ms 11100 KB Output is correct
37 Correct 4 ms 11100 KB Output is correct
38 Correct 3 ms 11100 KB Output is correct
39 Correct 77 ms 20688 KB Output is correct
40 Correct 73 ms 20088 KB Output is correct
41 Correct 68 ms 21696 KB Output is correct
42 Correct 69 ms 20180 KB Output is correct
43 Correct 93 ms 20360 KB Output is correct
44 Correct 62 ms 24012 KB Output is correct
45 Correct 78 ms 33876 KB Output is correct
46 Correct 52 ms 38228 KB Output is correct
47 Correct 113 ms 32728 KB Output is correct
48 Correct 159 ms 63316 KB Output is correct
49 Correct 155 ms 61724 KB Output is correct
50 Correct 110 ms 31932 KB Output is correct
51 Correct 115 ms 31928 KB Output is correct
52 Correct 170 ms 33480 KB Output is correct
53 Correct 98 ms 20508 KB Output is correct
54 Correct 173 ms 33104 KB Output is correct
55 Correct 12 ms 12376 KB Output is correct
56 Correct 8 ms 11952 KB Output is correct
57 Correct 43 ms 20420 KB Output is correct
58 Correct 37 ms 29112 KB Output is correct
59 Correct 54 ms 38176 KB Output is correct
60 Correct 141 ms 62436 KB Output is correct
61 Correct 127 ms 34488 KB Output is correct
62 Correct 106 ms 31936 KB Output is correct
63 Correct 126 ms 32828 KB Output is correct
64 Correct 244 ms 37888 KB Output is correct
65 Correct 237 ms 38584 KB Output is correct
66 Correct 158 ms 58660 KB Output is correct
67 Correct 87 ms 44672 KB Output is correct
68 Correct 189 ms 39352 KB Output is correct
69 Correct 192 ms 41136 KB Output is correct
70 Correct 177 ms 38328 KB Output is correct