Submission #337091

# Submission time Handle Problem Language Result Execution time Memory
337091 2020-12-18T09:21:35 Z Joshc Race (IOI11_race) C++11
100 / 100
542 ms 35596 KB
#include "race.h"
#include <vector>
using namespace std;

#define f first
#define s second
#define pii pair<int, int>

const int MAX_N = 200001;
vector<pii> edges[MAX_N], lengths;
vector<int> cur;
int k, num[MAX_N], best[1000001], ans = 1e9;
bool removed[MAX_N];

int dfs(int v, int p) {
    num[v] = 1;
    for (pii u : edges[v]) {
        if (u.f != p && !removed[u.f]) num[v] += dfs(u.f, v);
    }
    return num[v];
}

int centroid(int v, int p, int n) {
    for (pii u : edges[v]) {
        if (u.f != p && !removed[u.f] && num[u.f]>n/2) return centroid(u.f, v, n);
    }
    return v;
}

void getlengths(int v, int p, int c, int d) {
    if (c > k) return;
    lengths.emplace_back(c, d);
    for (pii u : edges[v]) {
        if (u.f != p && !removed[u.f]) getlengths(u.f, v, c+u.s, d+1);
    }
}

void solve(int v) {
    removed[v] = true;
    for (pii u : edges[v]) {
        if (!removed[u.f]) {
            lengths.clear();
            getlengths(u.f, v, u.s, 1);
            for (pii length : lengths) {
                if (length.f == k) ans = min(ans, length.s);
                ans = min(ans, length.s+best[k-length.f]);
                cur.push_back(length.f);
            }
            for (pii length : lengths) best[length.f] = min(best[length.f], length.s);
        }
    }
    for (int i : cur) best[i] = 1e9;
    cur.clear();
    for (pii u : edges[v]) {
        if (!removed[u.f]) solve(centroid(u.f, u.f, dfs(u.f, u.f)));
    }
}

int best_path(int n, int K, int H[][2], int L[]) {
    k = K;
    for (int i=0; i<n-1; i++) {
        edges[H[i][0]].emplace_back(H[i][1], L[i]);
        edges[H[i][1]].emplace_back(H[i][0], L[i]);
    }
    for (int i=0; i<1000001; i++) best[i] = 1e9;
    dfs(0, 0);
    solve(centroid(0, 0, n));
    return ans==1e9 ? -1 : ans;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8940 KB Output is correct
2 Correct 6 ms 8940 KB Output is correct
3 Correct 7 ms 8940 KB Output is correct
4 Correct 6 ms 8940 KB Output is correct
5 Correct 6 ms 8940 KB Output is correct
6 Correct 6 ms 8940 KB Output is correct
7 Correct 6 ms 8940 KB Output is correct
8 Correct 7 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 6 ms 8940 KB Output is correct
11 Correct 6 ms 8940 KB Output is correct
12 Correct 7 ms 8940 KB Output is correct
13 Correct 6 ms 8940 KB Output is correct
14 Correct 6 ms 8940 KB Output is correct
15 Correct 6 ms 8940 KB Output is correct
16 Correct 6 ms 8940 KB Output is correct
17 Correct 6 ms 8940 KB Output is correct
18 Correct 6 ms 8940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8940 KB Output is correct
2 Correct 6 ms 8940 KB Output is correct
3 Correct 7 ms 8940 KB Output is correct
4 Correct 6 ms 8940 KB Output is correct
5 Correct 6 ms 8940 KB Output is correct
6 Correct 6 ms 8940 KB Output is correct
7 Correct 6 ms 8940 KB Output is correct
8 Correct 7 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 6 ms 8940 KB Output is correct
11 Correct 6 ms 8940 KB Output is correct
12 Correct 7 ms 8940 KB Output is correct
13 Correct 6 ms 8940 KB Output is correct
14 Correct 6 ms 8940 KB Output is correct
15 Correct 6 ms 8940 KB Output is correct
16 Correct 6 ms 8940 KB Output is correct
17 Correct 6 ms 8940 KB Output is correct
18 Correct 6 ms 8940 KB Output is correct
19 Correct 6 ms 8940 KB Output is correct
20 Correct 7 ms 8940 KB Output is correct
21 Correct 7 ms 9068 KB Output is correct
22 Correct 7 ms 9068 KB Output is correct
23 Correct 7 ms 9068 KB Output is correct
24 Correct 9 ms 9068 KB Output is correct
25 Correct 7 ms 9068 KB Output is correct
26 Correct 7 ms 9068 KB Output is correct
27 Correct 7 ms 9068 KB Output is correct
28 Correct 7 ms 9068 KB Output is correct
29 Correct 7 ms 9084 KB Output is correct
30 Correct 7 ms 9068 KB Output is correct
31 Correct 7 ms 9068 KB Output is correct
32 Correct 7 ms 9068 KB Output is correct
33 Correct 7 ms 9068 KB Output is correct
34 Correct 7 ms 9068 KB Output is correct
35 Correct 7 ms 9068 KB Output is correct
36 Correct 7 ms 9068 KB Output is correct
37 Correct 7 ms 9068 KB Output is correct
38 Correct 7 ms 9068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8940 KB Output is correct
2 Correct 6 ms 8940 KB Output is correct
3 Correct 7 ms 8940 KB Output is correct
4 Correct 6 ms 8940 KB Output is correct
5 Correct 6 ms 8940 KB Output is correct
6 Correct 6 ms 8940 KB Output is correct
7 Correct 6 ms 8940 KB Output is correct
8 Correct 7 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 6 ms 8940 KB Output is correct
11 Correct 6 ms 8940 KB Output is correct
12 Correct 7 ms 8940 KB Output is correct
13 Correct 6 ms 8940 KB Output is correct
14 Correct 6 ms 8940 KB Output is correct
15 Correct 6 ms 8940 KB Output is correct
16 Correct 6 ms 8940 KB Output is correct
17 Correct 6 ms 8940 KB Output is correct
18 Correct 6 ms 8940 KB Output is correct
19 Correct 147 ms 15980 KB Output is correct
20 Correct 144 ms 15788 KB Output is correct
21 Correct 167 ms 16108 KB Output is correct
22 Correct 149 ms 16500 KB Output is correct
23 Correct 94 ms 15980 KB Output is correct
24 Correct 68 ms 15852 KB Output is correct
25 Correct 138 ms 18412 KB Output is correct
26 Correct 101 ms 22380 KB Output is correct
27 Correct 205 ms 22764 KB Output is correct
28 Correct 323 ms 34028 KB Output is correct
29 Correct 322 ms 33132 KB Output is correct
30 Correct 201 ms 22764 KB Output is correct
31 Correct 201 ms 22764 KB Output is correct
32 Correct 248 ms 23020 KB Output is correct
33 Correct 262 ms 21612 KB Output is correct
34 Correct 221 ms 22508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8940 KB Output is correct
2 Correct 6 ms 8940 KB Output is correct
3 Correct 7 ms 8940 KB Output is correct
4 Correct 6 ms 8940 KB Output is correct
5 Correct 6 ms 8940 KB Output is correct
6 Correct 6 ms 8940 KB Output is correct
7 Correct 6 ms 8940 KB Output is correct
8 Correct 7 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 6 ms 8940 KB Output is correct
11 Correct 6 ms 8940 KB Output is correct
12 Correct 7 ms 8940 KB Output is correct
13 Correct 6 ms 8940 KB Output is correct
14 Correct 6 ms 8940 KB Output is correct
15 Correct 6 ms 8940 KB Output is correct
16 Correct 6 ms 8940 KB Output is correct
17 Correct 6 ms 8940 KB Output is correct
18 Correct 6 ms 8940 KB Output is correct
19 Correct 6 ms 8940 KB Output is correct
20 Correct 7 ms 8940 KB Output is correct
21 Correct 7 ms 9068 KB Output is correct
22 Correct 7 ms 9068 KB Output is correct
23 Correct 7 ms 9068 KB Output is correct
24 Correct 9 ms 9068 KB Output is correct
25 Correct 7 ms 9068 KB Output is correct
26 Correct 7 ms 9068 KB Output is correct
27 Correct 7 ms 9068 KB Output is correct
28 Correct 7 ms 9068 KB Output is correct
29 Correct 7 ms 9084 KB Output is correct
30 Correct 7 ms 9068 KB Output is correct
31 Correct 7 ms 9068 KB Output is correct
32 Correct 7 ms 9068 KB Output is correct
33 Correct 7 ms 9068 KB Output is correct
34 Correct 7 ms 9068 KB Output is correct
35 Correct 7 ms 9068 KB Output is correct
36 Correct 7 ms 9068 KB Output is correct
37 Correct 7 ms 9068 KB Output is correct
38 Correct 7 ms 9068 KB Output is correct
39 Correct 147 ms 15980 KB Output is correct
40 Correct 144 ms 15788 KB Output is correct
41 Correct 167 ms 16108 KB Output is correct
42 Correct 149 ms 16500 KB Output is correct
43 Correct 94 ms 15980 KB Output is correct
44 Correct 68 ms 15852 KB Output is correct
45 Correct 138 ms 18412 KB Output is correct
46 Correct 101 ms 22380 KB Output is correct
47 Correct 205 ms 22764 KB Output is correct
48 Correct 323 ms 34028 KB Output is correct
49 Correct 322 ms 33132 KB Output is correct
50 Correct 201 ms 22764 KB Output is correct
51 Correct 201 ms 22764 KB Output is correct
52 Correct 248 ms 23020 KB Output is correct
53 Correct 262 ms 21612 KB Output is correct
54 Correct 221 ms 22508 KB Output is correct
55 Correct 15 ms 9708 KB Output is correct
56 Correct 16 ms 9708 KB Output is correct
57 Correct 91 ms 16744 KB Output is correct
58 Correct 47 ms 16480 KB Output is correct
59 Correct 103 ms 22380 KB Output is correct
60 Correct 494 ms 35596 KB Output is correct
61 Correct 221 ms 23152 KB Output is correct
62 Correct 237 ms 23952 KB Output is correct
63 Correct 309 ms 23792 KB Output is correct
64 Correct 542 ms 25184 KB Output is correct
65 Correct 285 ms 23660 KB Output is correct
66 Correct 437 ms 31468 KB Output is correct
67 Correct 146 ms 25308 KB Output is correct
68 Correct 302 ms 24040 KB Output is correct
69 Correct 305 ms 24244 KB Output is correct
70 Correct 281 ms 23400 KB Output is correct