Submission #753765

# Submission time Handle Problem Language Result Execution time Memory
753765 2023-06-06T01:05:18 Z acceptify Race (IOI11_race) C++17
100 / 100
882 ms 42340 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

const int mxn = 2e5 + 5;

vector<pair<int, int>> adj[mxn];
int del[mxn], sz[mxn];
map<int, int> mp;
vector<pair<int, int>> tmp;
int res = 2e9;

void dfs(int x, int par = -1) {
    sz[x] = 1;
    for (auto [w, p] : adj[x]) {
        if (p == par || del[p]) continue;
        dfs(p, x);
        sz[x] += sz[p];
    }
}

int centroid(int x, int n, int par = -1) {
    for (auto [w, p] : adj[x]) {
        if (p == par || del[p]) continue;
        if (sz[p] * 2 > n) return centroid(p, n, x);
    }
    return x;
}

void dfs2(int k, int x, int par, int dis, int cnt) {
    if (dis > k) return;
    tmp.push_back({dis, cnt});
    if (mp.find(k - dis) != mp.end()) res = min(res, mp[k - dis] + cnt);
    for (auto [w, p] : adj[x]) {
        if (p == par || del[p]) continue;
        dfs2(k, p, x, dis + w, cnt + 1);
    }
}

void solve(int k, int x) {
    dfs(x);
    int c = centroid(x, sz[x]);
    mp.clear(), tmp.clear();
    mp[0] = 0;
    for (auto [w, i] : adj[c]) {
        if (!del[i]) {
            dfs2(k, i, c, w, 1);
            for (auto [d, c] : tmp) {
                if (mp.find(d) == mp.end()) mp[d] = c;
                else mp[d] = min(mp[d], c);
            }
            tmp.clear();
        }
    }
    del[c] = 1;
    for (auto [w, i] : adj[c]) {
        if (!del[i]) solve(k, i);
    }
}

int best_path(int N, int K, int H[][2], int L[]) {
    for (int i = 0; i < N - 1; i++) {
        adj[H[i][0]].push_back({L[i], H[i][1]});
        adj[H[i][1]].push_back({L[i], H[i][0]});
    }
    solve(K, 0);
    if (res == 2e9) return -1;
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5016 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 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5012 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 5016 KB Output is correct
17 Correct 4 ms 5012 KB Output is correct
18 Correct 3 ms 5020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5016 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 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5012 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 5016 KB Output is correct
17 Correct 4 ms 5012 KB Output is correct
18 Correct 3 ms 5020 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 4 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 4 ms 5028 KB Output is correct
28 Correct 4 ms 5028 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 4 ms 5076 KB Output is correct
33 Correct 5 ms 5032 KB Output is correct
34 Correct 4 ms 5076 KB Output is correct
35 Correct 5 ms 5024 KB Output is correct
36 Correct 4 ms 5024 KB Output is correct
37 Correct 4 ms 5024 KB Output is correct
38 Correct 4 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5016 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 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5012 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 5016 KB Output is correct
17 Correct 4 ms 5012 KB Output is correct
18 Correct 3 ms 5020 KB Output is correct
19 Correct 189 ms 12092 KB Output is correct
20 Correct 180 ms 12020 KB Output is correct
21 Correct 195 ms 12308 KB Output is correct
22 Correct 187 ms 12468 KB Output is correct
23 Correct 95 ms 12316 KB Output is correct
24 Correct 76 ms 12184 KB Output is correct
25 Correct 145 ms 15576 KB Output is correct
26 Correct 105 ms 19892 KB Output is correct
27 Correct 210 ms 19276 KB Output is correct
28 Correct 318 ms 33868 KB Output is correct
29 Correct 317 ms 32668 KB Output is correct
30 Correct 205 ms 19368 KB Output is correct
31 Correct 212 ms 19400 KB Output is correct
32 Correct 237 ms 19416 KB Output is correct
33 Correct 297 ms 18288 KB Output is correct
34 Correct 194 ms 19160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5016 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 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5012 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 5016 KB Output is correct
17 Correct 4 ms 5012 KB Output is correct
18 Correct 3 ms 5020 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 4 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 4 ms 5028 KB Output is correct
28 Correct 4 ms 5028 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 4 ms 5076 KB Output is correct
33 Correct 5 ms 5032 KB Output is correct
34 Correct 4 ms 5076 KB Output is correct
35 Correct 5 ms 5024 KB Output is correct
36 Correct 4 ms 5024 KB Output is correct
37 Correct 4 ms 5024 KB Output is correct
38 Correct 4 ms 5076 KB Output is correct
39 Correct 189 ms 12092 KB Output is correct
40 Correct 180 ms 12020 KB Output is correct
41 Correct 195 ms 12308 KB Output is correct
42 Correct 187 ms 12468 KB Output is correct
43 Correct 95 ms 12316 KB Output is correct
44 Correct 76 ms 12184 KB Output is correct
45 Correct 145 ms 15576 KB Output is correct
46 Correct 105 ms 19892 KB Output is correct
47 Correct 210 ms 19276 KB Output is correct
48 Correct 318 ms 33868 KB Output is correct
49 Correct 317 ms 32668 KB Output is correct
50 Correct 205 ms 19368 KB Output is correct
51 Correct 212 ms 19400 KB Output is correct
52 Correct 237 ms 19416 KB Output is correct
53 Correct 297 ms 18288 KB Output is correct
54 Correct 194 ms 19160 KB Output is correct
55 Correct 18 ms 5844 KB Output is correct
56 Correct 15 ms 5768 KB Output is correct
57 Correct 131 ms 12532 KB Output is correct
58 Correct 48 ms 11944 KB Output is correct
59 Correct 296 ms 23256 KB Output is correct
60 Correct 816 ms 42340 KB Output is correct
61 Correct 261 ms 19736 KB Output is correct
62 Correct 277 ms 19732 KB Output is correct
63 Correct 346 ms 19648 KB Output is correct
64 Correct 882 ms 24980 KB Output is correct
65 Correct 190 ms 20172 KB Output is correct
66 Correct 548 ms 30600 KB Output is correct
67 Correct 129 ms 20072 KB Output is correct
68 Correct 366 ms 23804 KB Output is correct
69 Correct 365 ms 23860 KB Output is correct
70 Correct 333 ms 23060 KB Output is correct