Submission #639845

# Submission time Handle Problem Language Result Execution time Memory
639845 2022-09-12T09:00:51 Z classic Race (IOI11_race) C++14
100 / 100
782 ms 36544 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5;

vector<pair<int, int>> adj[N];
int sz[N];
bool vis[N];
int res;
map<int, int> mn;
vector<pair<int, int>> cand;

int dfsSz(int u, int p) {
    sz[u] = 1;
    for (pair<int, int> v : adj[u]) {
        if (v.first != p && !vis[v.first]) {
            sz[u] += dfsSz(v.first, u);
        }
    }
    return sz[u];
}

int centroid(int u, int p, int siz) {
    for (pair<int, int> v : adj[u]) {
        if (v.first != p && !vis[v.first] && sz[v.first] > siz / 2) {
            return centroid(v.first, u, siz);
        }
    }
    return u;
}

void dfs(int u, int p, int len, int d) {
    cand.emplace_back(len, d);
    for (pair<int, int> v : adj[u]) {
        if (v.first != p && !vis[v.first]) {
            dfs(v.first, u, len + v.second, d + 1);
        }
    }
}

void build(int u, int k) {
    dfsSz(u, -1);
    int c = centroid(u, -1, sz[u]);
    mn[0] = 0;
    for (pair<int, int> v : adj[c]) {
        if (!vis[v.first]) {
            dfs(v.first, c, v.second, 1);
            for (pair<int, int> x : cand) {
                int len = x.first;
                int d = x.second;
                if (mn.find(k - len) != mn.end()) {
                    res = min(res, mn[k - len] + d);
                }
            }
            for (pair<int, int> x : cand) {
                if (mn.find(x.first) == mn.end()) {
                    mn[x.first] = x.second;
                } else {
                    mn[x.first] = min(mn[x.first], x.second);
                }
            }
            cand.clear();
        }
    }
    mn.clear();
    vis[c] = true;
    for (pair<int, int> v : adj[c]) {
        if (!vis[v.first]) {
            build(v.first, k);
        }
    }
}

int best_path(int n, int k, int h[][2], int l[]) {
    for (int i = 0; i < n - 1; i++) {
        adj[h[i][0]].emplace_back(h[i][1], l[i]);
        adj[h[i][1]].emplace_back(h[i][0], l[i]);
    }
    res = 1e9;
    build(1, k);
    return (res == 1e9 ? -1 : 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 4996 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 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 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 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4996 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 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 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 4 ms 4948 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 5 ms 5076 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 4 ms 5020 KB Output is correct
25 Correct 4 ms 5024 KB Output is correct
26 Correct 4 ms 5028 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 4 ms 5024 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 5136 KB Output is correct
32 Correct 4 ms 5028 KB Output is correct
33 Correct 5 ms 5024 KB Output is correct
34 Correct 5 ms 5076 KB Output is correct
35 Correct 5 ms 5064 KB Output is correct
36 Correct 5 ms 5076 KB Output is correct
37 Correct 5 ms 5076 KB Output is correct
38 Correct 5 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 4996 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 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 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 203 ms 10768 KB Output is correct
20 Correct 205 ms 11180 KB Output is correct
21 Correct 196 ms 11084 KB Output is correct
22 Correct 187 ms 11112 KB Output is correct
23 Correct 262 ms 11344 KB Output is correct
24 Correct 158 ms 10956 KB Output is correct
25 Correct 176 ms 16248 KB Output is correct
26 Correct 95 ms 16788 KB Output is correct
27 Correct 284 ms 16200 KB Output is correct
28 Correct 776 ms 36544 KB Output is correct
29 Correct 781 ms 32532 KB Output is correct
30 Correct 304 ms 16488 KB Output is correct
31 Correct 283 ms 16384 KB Output is correct
32 Correct 314 ms 16584 KB Output is correct
33 Correct 444 ms 16064 KB Output is correct
34 Correct 718 ms 25024 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 4996 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 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 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 4 ms 4948 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 5 ms 5076 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 4 ms 5020 KB Output is correct
25 Correct 4 ms 5024 KB Output is correct
26 Correct 4 ms 5028 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 4 ms 5024 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 5136 KB Output is correct
32 Correct 4 ms 5028 KB Output is correct
33 Correct 5 ms 5024 KB Output is correct
34 Correct 5 ms 5076 KB Output is correct
35 Correct 5 ms 5064 KB Output is correct
36 Correct 5 ms 5076 KB Output is correct
37 Correct 5 ms 5076 KB Output is correct
38 Correct 5 ms 5076 KB Output is correct
39 Correct 203 ms 10768 KB Output is correct
40 Correct 205 ms 11180 KB Output is correct
41 Correct 196 ms 11084 KB Output is correct
42 Correct 187 ms 11112 KB Output is correct
43 Correct 262 ms 11344 KB Output is correct
44 Correct 158 ms 10956 KB Output is correct
45 Correct 176 ms 16248 KB Output is correct
46 Correct 95 ms 16788 KB Output is correct
47 Correct 284 ms 16200 KB Output is correct
48 Correct 776 ms 36544 KB Output is correct
49 Correct 781 ms 32532 KB Output is correct
50 Correct 304 ms 16488 KB Output is correct
51 Correct 283 ms 16384 KB Output is correct
52 Correct 314 ms 16584 KB Output is correct
53 Correct 444 ms 16064 KB Output is correct
54 Correct 718 ms 25024 KB Output is correct
55 Correct 24 ms 5888 KB Output is correct
56 Correct 13 ms 5716 KB Output is correct
57 Correct 125 ms 11600 KB Output is correct
58 Correct 43 ms 10960 KB Output is correct
59 Correct 270 ms 20532 KB Output is correct
60 Correct 782 ms 30892 KB Output is correct
61 Correct 306 ms 17740 KB Output is correct
62 Correct 269 ms 16588 KB Output is correct
63 Correct 358 ms 16588 KB Output is correct
64 Correct 779 ms 21336 KB Output is correct
65 Correct 733 ms 26292 KB Output is correct
66 Correct 782 ms 35644 KB Output is correct
67 Correct 126 ms 16696 KB Output is correct
68 Correct 430 ms 25044 KB Output is correct
69 Correct 458 ms 25364 KB Output is correct
70 Correct 400 ms 24204 KB Output is correct