Submission #419855

# Submission time Handle Problem Language Result Execution time Memory
419855 2021-06-07T14:15:06 Z dxz05 Race (IOI11_race) C++14
100 / 100
640 ms 34908 KB
#pragma GCC optimize("Ofast")
#include "race.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2e5 + 3e2;

vector<pair<int, int>> g[MAXN];

int k;
int D[MAXN];

int get_centroid(int v, int p, int n, int &centroid){
    int size = 1;
    for (auto edge : g[v]){
        int u = edge.first, w = edge.second;
        if (D[u] == -1 && u != p){
            size += get_centroid(u, v, n, centroid);
        }
    }

    if (size >= (n + 1) / 2 && centroid == -1) centroid = v;

    return size;
}

void dfs(int v, int p, int dist, int cnt, vector<pair<int, int>> &dists){
    dists.emplace_back(dist, cnt);
    for (auto edge : g[v]){
        int u = edge.first, w = edge.second;
        if (u != p && D[u] == -1){
            dfs(u, v, dist + w, cnt + 1, dists);
        }
    }
}

int ans = 1e9;

int d[2000000];

void build(int v, int dep, int n){
    int centroid = -1;
    get_centroid(v, -1, n, centroid);
    if (centroid == -1) centroid = v;

    D[centroid] = dep;

    vector<int> changed;

    for (auto edge : g[centroid]){
        int u = edge.first, w = edge.second;
        if (D[u] != -1) continue;
        vector<pair<int, int>> dists;
        dfs(u, -1, w, 1, dists);

        for (auto now : dists){
            int x = now.first, cnt = now.second;
            if (x > k) continue;
            if (d[k - x] != -1){
                ans = min(ans, cnt + d[k - x]);
            }
        }

        for (auto now : dists){
            int x = now.first, cnt = now.second;
            if (x > k) continue;
            if (d[x] == -1 || d[x] > cnt){
                d[x] = cnt;
                changed.push_back(x);
            }
        }

    }

    for (int x : changed){
        d[x] = -1;
    }
    d[0] = 0;

    for (auto edge : g[centroid]){
        int u = edge.first;
        if (D[u] == -1) build(u, dep + 1, (n + 1) / 2);
    }

}

int best_path(int N, int K, int H[][2], int L[]){
    k = K;
    fill(D, D + MAXN, -1);
    fill(d, d + 2000000, -1);
    d[0] = 0;

    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]);
    }

    build(0, 1, N);

    if (ans == 1e9) ans = -1;

    return ans;
}

/*
4 3
0 1 1
1 2 2
1 3 4
2

3 3
0 1 1
1 2 1
-1

11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
2



*/

Compilation message

race.cpp: In function 'int get_centroid(int, int, int, int&)':
race.cpp:18:29: warning: unused variable 'w' [-Wunused-variable]
   18 |         int u = edge.first, w = edge.second;
      |                             ^
# Verdict Execution time Memory Grader output
1 Correct 9 ms 13516 KB Output is correct
2 Correct 9 ms 13516 KB Output is correct
3 Correct 9 ms 13516 KB Output is correct
4 Correct 9 ms 13596 KB Output is correct
5 Correct 8 ms 13516 KB Output is correct
6 Correct 8 ms 13516 KB Output is correct
7 Correct 9 ms 13596 KB Output is correct
8 Correct 8 ms 13516 KB Output is correct
9 Correct 8 ms 13576 KB Output is correct
10 Correct 8 ms 13516 KB Output is correct
11 Correct 8 ms 13596 KB Output is correct
12 Correct 8 ms 13516 KB Output is correct
13 Correct 8 ms 13516 KB Output is correct
14 Correct 9 ms 13516 KB Output is correct
15 Correct 9 ms 13620 KB Output is correct
16 Correct 8 ms 13516 KB Output is correct
17 Correct 9 ms 13588 KB Output is correct
18 Correct 10 ms 13516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 13516 KB Output is correct
2 Correct 9 ms 13516 KB Output is correct
3 Correct 9 ms 13516 KB Output is correct
4 Correct 9 ms 13596 KB Output is correct
5 Correct 8 ms 13516 KB Output is correct
6 Correct 8 ms 13516 KB Output is correct
7 Correct 9 ms 13596 KB Output is correct
8 Correct 8 ms 13516 KB Output is correct
9 Correct 8 ms 13576 KB Output is correct
10 Correct 8 ms 13516 KB Output is correct
11 Correct 8 ms 13596 KB Output is correct
12 Correct 8 ms 13516 KB Output is correct
13 Correct 8 ms 13516 KB Output is correct
14 Correct 9 ms 13516 KB Output is correct
15 Correct 9 ms 13620 KB Output is correct
16 Correct 8 ms 13516 KB Output is correct
17 Correct 9 ms 13588 KB Output is correct
18 Correct 10 ms 13516 KB Output is correct
19 Correct 9 ms 13552 KB Output is correct
20 Correct 8 ms 13520 KB Output is correct
21 Correct 9 ms 13644 KB Output is correct
22 Correct 9 ms 13644 KB Output is correct
23 Correct 9 ms 13644 KB Output is correct
24 Correct 9 ms 13644 KB Output is correct
25 Correct 9 ms 13644 KB Output is correct
26 Correct 9 ms 13644 KB Output is correct
27 Correct 11 ms 13644 KB Output is correct
28 Correct 10 ms 13644 KB Output is correct
29 Correct 10 ms 13684 KB Output is correct
30 Correct 10 ms 13644 KB Output is correct
31 Correct 11 ms 13644 KB Output is correct
32 Correct 10 ms 13644 KB Output is correct
33 Correct 9 ms 13644 KB Output is correct
34 Correct 10 ms 13644 KB Output is correct
35 Correct 9 ms 13644 KB Output is correct
36 Correct 9 ms 13644 KB Output is correct
37 Correct 10 ms 13644 KB Output is correct
38 Correct 9 ms 13644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 13516 KB Output is correct
2 Correct 9 ms 13516 KB Output is correct
3 Correct 9 ms 13516 KB Output is correct
4 Correct 9 ms 13596 KB Output is correct
5 Correct 8 ms 13516 KB Output is correct
6 Correct 8 ms 13516 KB Output is correct
7 Correct 9 ms 13596 KB Output is correct
8 Correct 8 ms 13516 KB Output is correct
9 Correct 8 ms 13576 KB Output is correct
10 Correct 8 ms 13516 KB Output is correct
11 Correct 8 ms 13596 KB Output is correct
12 Correct 8 ms 13516 KB Output is correct
13 Correct 8 ms 13516 KB Output is correct
14 Correct 9 ms 13516 KB Output is correct
15 Correct 9 ms 13620 KB Output is correct
16 Correct 8 ms 13516 KB Output is correct
17 Correct 9 ms 13588 KB Output is correct
18 Correct 10 ms 13516 KB Output is correct
19 Correct 277 ms 19208 KB Output is correct
20 Correct 200 ms 19152 KB Output is correct
21 Correct 186 ms 19288 KB Output is correct
22 Correct 160 ms 19080 KB Output is correct
23 Correct 123 ms 19308 KB Output is correct
24 Correct 84 ms 19020 KB Output is correct
25 Correct 140 ms 23512 KB Output is correct
26 Correct 114 ms 23396 KB Output is correct
27 Correct 290 ms 23472 KB Output is correct
28 Correct 423 ms 33428 KB Output is correct
29 Correct 448 ms 33320 KB Output is correct
30 Correct 321 ms 23460 KB Output is correct
31 Correct 310 ms 23468 KB Output is correct
32 Correct 479 ms 23512 KB Output is correct
33 Correct 368 ms 23924 KB Output is correct
34 Correct 327 ms 23464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 13516 KB Output is correct
2 Correct 9 ms 13516 KB Output is correct
3 Correct 9 ms 13516 KB Output is correct
4 Correct 9 ms 13596 KB Output is correct
5 Correct 8 ms 13516 KB Output is correct
6 Correct 8 ms 13516 KB Output is correct
7 Correct 9 ms 13596 KB Output is correct
8 Correct 8 ms 13516 KB Output is correct
9 Correct 8 ms 13576 KB Output is correct
10 Correct 8 ms 13516 KB Output is correct
11 Correct 8 ms 13596 KB Output is correct
12 Correct 8 ms 13516 KB Output is correct
13 Correct 8 ms 13516 KB Output is correct
14 Correct 9 ms 13516 KB Output is correct
15 Correct 9 ms 13620 KB Output is correct
16 Correct 8 ms 13516 KB Output is correct
17 Correct 9 ms 13588 KB Output is correct
18 Correct 10 ms 13516 KB Output is correct
19 Correct 9 ms 13552 KB Output is correct
20 Correct 8 ms 13520 KB Output is correct
21 Correct 9 ms 13644 KB Output is correct
22 Correct 9 ms 13644 KB Output is correct
23 Correct 9 ms 13644 KB Output is correct
24 Correct 9 ms 13644 KB Output is correct
25 Correct 9 ms 13644 KB Output is correct
26 Correct 9 ms 13644 KB Output is correct
27 Correct 11 ms 13644 KB Output is correct
28 Correct 10 ms 13644 KB Output is correct
29 Correct 10 ms 13684 KB Output is correct
30 Correct 10 ms 13644 KB Output is correct
31 Correct 11 ms 13644 KB Output is correct
32 Correct 10 ms 13644 KB Output is correct
33 Correct 9 ms 13644 KB Output is correct
34 Correct 10 ms 13644 KB Output is correct
35 Correct 9 ms 13644 KB Output is correct
36 Correct 9 ms 13644 KB Output is correct
37 Correct 10 ms 13644 KB Output is correct
38 Correct 9 ms 13644 KB Output is correct
39 Correct 277 ms 19208 KB Output is correct
40 Correct 200 ms 19152 KB Output is correct
41 Correct 186 ms 19288 KB Output is correct
42 Correct 160 ms 19080 KB Output is correct
43 Correct 123 ms 19308 KB Output is correct
44 Correct 84 ms 19020 KB Output is correct
45 Correct 140 ms 23512 KB Output is correct
46 Correct 114 ms 23396 KB Output is correct
47 Correct 290 ms 23472 KB Output is correct
48 Correct 423 ms 33428 KB Output is correct
49 Correct 448 ms 33320 KB Output is correct
50 Correct 321 ms 23460 KB Output is correct
51 Correct 310 ms 23468 KB Output is correct
52 Correct 479 ms 23512 KB Output is correct
53 Correct 368 ms 23924 KB Output is correct
54 Correct 327 ms 23464 KB Output is correct
55 Correct 18 ms 14212 KB Output is correct
56 Correct 22 ms 14156 KB Output is correct
57 Correct 97 ms 19284 KB Output is correct
58 Correct 51 ms 18612 KB Output is correct
59 Correct 127 ms 24124 KB Output is correct
60 Correct 429 ms 34908 KB Output is correct
61 Correct 302 ms 23620 KB Output is correct
62 Correct 298 ms 23592 KB Output is correct
63 Correct 447 ms 23620 KB Output is correct
64 Correct 640 ms 25292 KB Output is correct
65 Correct 503 ms 24532 KB Output is correct
66 Correct 500 ms 33356 KB Output is correct
67 Correct 175 ms 23676 KB Output is correct
68 Correct 535 ms 25072 KB Output is correct
69 Correct 486 ms 25208 KB Output is correct
70 Correct 459 ms 24668 KB Output is correct