Submission #1024311

# Submission time Handle Problem Language Result Execution time Memory
1024311 2024-07-16T00:10:47 Z eysbutno Race (IOI11_race) C++17
100 / 100
362 ms 96172 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = array<int, 2>;
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()

int best_path(int n, int k, int h[][2], int l[]) {
    vector<vector<pii>> adj(n);
    for (int i = 0; i < n - 1; i++) {
        adj[h[i][0]].push_back({h[i][1], l[i]});
        adj[h[i][1]].push_back({h[i][0], l[i]});
    }
    vector<ll> f(n);
    vector<int> dep(n);
    const auto build = [&](int u, int p, auto &&self) -> void {
        for (auto [v, w] : adj[u]) {
            if (v == p) { continue; }
            f[v] = f[u] + w;
            dep[v] = dep[u] + 1;
            self(v, u, self); 
        }
    };
    build(0, -1, build);
    vector<map<ll, int>> mp(n);
    const int INF = 1e9;
    int res = INF;
    const auto merge = [&](int i, int j) -> void {
        if (mp[j].count(f[i] + k)) {
            res = min(res, mp[j][f[i] + k] - dep[i]);
        }
        ll need = k + f[i] * 2;
        int sub = 2 * dep[i];
        if (sz(mp[i]) < sz(mp[j])) {
            swap(mp[i], mp[j]);
        }
        for (auto [len, dx] : mp[j]) {
            if (mp[i].count(need - len)) {
                res = min(res, dx + mp[i][need - len] - sub);
            }
        }
        for (auto [len, dx] : mp[j]) {
            if (mp[i].count(len)) {
                mp[i][len] = min(mp[i][len], dx);
            } else {
                mp[i][len] = dx;
            }
        }
    };
    const auto dfs = [&](int u, int p, auto &&self) -> void {
        for (auto [v, w] : adj[u]) {
            if (v == p) { continue; }
            self(v, u, self);
            merge(u, v);
        }
        mp[u][f[u]] = dep[u];
    };
    dfs(0, -1, dfs);
    return res < INF ? res : -1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 1 ms 2704 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
25 Correct 1 ms 2756 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2736 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 2 ms 2648 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 73 ms 27828 KB Output is correct
20 Correct 80 ms 28820 KB Output is correct
21 Correct 80 ms 28524 KB Output is correct
22 Correct 73 ms 29268 KB Output is correct
23 Correct 115 ms 42356 KB Output is correct
24 Correct 87 ms 35192 KB Output is correct
25 Correct 47 ms 19792 KB Output is correct
26 Correct 29 ms 20760 KB Output is correct
27 Correct 121 ms 42956 KB Output is correct
28 Correct 140 ms 49492 KB Output is correct
29 Correct 143 ms 50004 KB Output is correct
30 Correct 124 ms 43348 KB Output is correct
31 Correct 125 ms 43420 KB Output is correct
32 Correct 140 ms 43344 KB Output is correct
33 Correct 139 ms 42320 KB Output is correct
34 Correct 228 ms 74320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 1 ms 2704 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
25 Correct 1 ms 2756 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2736 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 2 ms 2648 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2648 KB Output is correct
39 Correct 73 ms 27828 KB Output is correct
40 Correct 80 ms 28820 KB Output is correct
41 Correct 80 ms 28524 KB Output is correct
42 Correct 73 ms 29268 KB Output is correct
43 Correct 115 ms 42356 KB Output is correct
44 Correct 87 ms 35192 KB Output is correct
45 Correct 47 ms 19792 KB Output is correct
46 Correct 29 ms 20760 KB Output is correct
47 Correct 121 ms 42956 KB Output is correct
48 Correct 140 ms 49492 KB Output is correct
49 Correct 143 ms 50004 KB Output is correct
50 Correct 124 ms 43348 KB Output is correct
51 Correct 125 ms 43420 KB Output is correct
52 Correct 140 ms 43344 KB Output is correct
53 Correct 139 ms 42320 KB Output is correct
54 Correct 228 ms 74320 KB Output is correct
55 Correct 11 ms 5976 KB Output is correct
56 Correct 5 ms 4444 KB Output is correct
57 Correct 42 ms 26068 KB Output is correct
58 Correct 35 ms 23964 KB Output is correct
59 Correct 49 ms 26964 KB Output is correct
60 Correct 133 ms 49708 KB Output is correct
61 Correct 149 ms 46164 KB Output is correct
62 Correct 120 ms 43344 KB Output is correct
63 Correct 143 ms 43344 KB Output is correct
64 Correct 358 ms 95036 KB Output is correct
65 Correct 362 ms 96172 KB Output is correct
66 Correct 154 ms 48868 KB Output is correct
67 Correct 107 ms 44852 KB Output is correct
68 Correct 251 ms 62036 KB Output is correct
69 Correct 272 ms 65012 KB Output is correct
70 Correct 238 ms 60020 KB Output is correct