Submission #655057

# Submission time Handle Problem Language Result Execution time Memory
655057 2022-11-02T22:44:43 Z benjaminkleyn Race (IOI11_race) C++17
100 / 100
523 ms 32640 KB
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;


const int max_n = 200000;

vector<pair<int,int>> g[max_n];
bool done[max_n];
int sz[max_n], par[max_n];

struct node
{
    int dist, len, subtree;
};

bool operator<(const node &a, const node &b)
{
    if (a.dist < b.dist)
        return true;
    else if (a.dist == b.dist)
        return make_pair(a.len, a.subtree) < make_pair(b.len, b.subtree);
    else
        return false;
}

vector<node> nodes;
int k, ans = INT_MAX;
void dfs(int u, int p, int dist, int len, int subtree)
{
    if (dist > k || len > ans)
        return;

    for (auto [v, l] : g[u])
        if (v != p && !done[v])
            dfs(v, u, dist + l, len + 1, subtree);

    nodes.push_back({dist, len, subtree});
}

int find_centroid(int u, int n, int p = -1)
{
    for (auto [v, l] : g[u])
        if (v != p && !done[v] && sz[v] > n / 2)
            return find_centroid(v, n, u);
    return u;
}

int find_size(int u, int p = -1)
{
    if (done[u]) return 0;
    sz[u] = 1;

    for (auto [v, l] : g[u])
        if (v != p)
            sz[u] += find_size(v, u);

    return sz[u];
}

void centroid_decomposition(int u = 0, int p = -1)
{
    find_size(u);
    int c = find_centroid(u, sz[u]);
    par[c] = p;

    nodes.resize(0);

    int subtree = 0;
    for (auto [v, l] : g[c])
    {
        if (!done[v])
            dfs(v, c, l, 1, subtree);
        nodes.push_back({0, 0, subtree});
        subtree++;
    }

    sort(nodes.begin(), nodes.end());
    int lo = 0, hi = nodes.size() - 1;

    while (lo < hi)
        if (nodes[lo].dist + nodes[hi].dist > k)
            hi--;
        else if (nodes[lo].dist + nodes[hi].dist < k)
            lo++;
        else if (nodes[lo].subtree != nodes[hi].subtree)
            ans = min(ans, nodes[lo].len + nodes[hi].len), hi--;
        else if (nodes[hi].dist == nodes[hi-1].dist)
            hi--;
        else
            lo++;

    done[c] = true;
    for (auto [v, l] : g[c])
        if (!done[v])
            centroid_decomposition(v, c);
}

int best_path(int N, int K, int H[][2], int L[])
{
    for (int i = 0; i < N - 1; i++)
    {
        g[H[i][0]].push_back({H[i][1], L[i]});
        g[H[i][1]].push_back({H[i][0], L[i]});
    }

    k = K;
    centroid_decomposition();

    return (ans == INT_MAX ? -1 : ans);
}
# 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 4952 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5012 KB Output is correct
8 Correct 3 ms 5008 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 4 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 4 ms 5008 KB Output is correct
18 Correct 3 ms 5008 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 4952 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5012 KB Output is correct
8 Correct 3 ms 5008 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 4 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 4 ms 5008 KB Output is correct
18 Correct 3 ms 5008 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 5028 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5020 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5024 KB Output is correct
27 Correct 3 ms 5076 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 3 ms 5024 KB Output is correct
32 Correct 4 ms 5024 KB Output is correct
33 Correct 5 ms 5076 KB Output is correct
34 Correct 4 ms 5076 KB Output is correct
35 Correct 3 ms 5020 KB Output is correct
36 Correct 4 ms 5076 KB Output is correct
37 Correct 4 ms 5080 KB Output is correct
38 Correct 3 ms 5080 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 4952 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5012 KB Output is correct
8 Correct 3 ms 5008 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 4 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 4 ms 5008 KB Output is correct
18 Correct 3 ms 5008 KB Output is correct
19 Correct 111 ms 11144 KB Output is correct
20 Correct 114 ms 11284 KB Output is correct
21 Correct 153 ms 11712 KB Output is correct
22 Correct 142 ms 12488 KB Output is correct
23 Correct 67 ms 11468 KB Output is correct
24 Correct 55 ms 11392 KB Output is correct
25 Correct 168 ms 14048 KB Output is correct
26 Correct 145 ms 19108 KB Output is correct
27 Correct 162 ms 19716 KB Output is correct
28 Correct 262 ms 30928 KB Output is correct
29 Correct 270 ms 30028 KB Output is correct
30 Correct 159 ms 19660 KB Output is correct
31 Correct 174 ms 19660 KB Output is correct
32 Correct 208 ms 19812 KB Output is correct
33 Correct 231 ms 18348 KB Output is correct
34 Correct 172 ms 19220 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 4952 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5012 KB Output is correct
8 Correct 3 ms 5008 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 4 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 4 ms 5008 KB Output is correct
18 Correct 3 ms 5008 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 5028 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5020 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5024 KB Output is correct
27 Correct 3 ms 5076 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 3 ms 5024 KB Output is correct
32 Correct 4 ms 5024 KB Output is correct
33 Correct 5 ms 5076 KB Output is correct
34 Correct 4 ms 5076 KB Output is correct
35 Correct 3 ms 5020 KB Output is correct
36 Correct 4 ms 5076 KB Output is correct
37 Correct 4 ms 5080 KB Output is correct
38 Correct 3 ms 5080 KB Output is correct
39 Correct 111 ms 11144 KB Output is correct
40 Correct 114 ms 11284 KB Output is correct
41 Correct 153 ms 11712 KB Output is correct
42 Correct 142 ms 12488 KB Output is correct
43 Correct 67 ms 11468 KB Output is correct
44 Correct 55 ms 11392 KB Output is correct
45 Correct 168 ms 14048 KB Output is correct
46 Correct 145 ms 19108 KB Output is correct
47 Correct 162 ms 19716 KB Output is correct
48 Correct 262 ms 30928 KB Output is correct
49 Correct 270 ms 30028 KB Output is correct
50 Correct 159 ms 19660 KB Output is correct
51 Correct 174 ms 19660 KB Output is correct
52 Correct 208 ms 19812 KB Output is correct
53 Correct 231 ms 18348 KB Output is correct
54 Correct 172 ms 19220 KB Output is correct
55 Correct 11 ms 5844 KB Output is correct
56 Correct 13 ms 5908 KB Output is correct
57 Correct 133 ms 13512 KB Output is correct
58 Correct 64 ms 15312 KB Output is correct
59 Correct 158 ms 19032 KB Output is correct
60 Correct 523 ms 32640 KB Output is correct
61 Correct 176 ms 20304 KB Output is correct
62 Correct 223 ms 22140 KB Output is correct
63 Correct 288 ms 22072 KB Output is correct
64 Correct 489 ms 22308 KB Output is correct
65 Correct 198 ms 20388 KB Output is correct
66 Correct 343 ms 28200 KB Output is correct
67 Correct 153 ms 27000 KB Output is correct
68 Correct 229 ms 21556 KB Output is correct
69 Correct 202 ms 21696 KB Output is correct
70 Correct 216 ms 20940 KB Output is correct