Submission #435777

# Submission time Handle Problem Language Result Execution time Memory
435777 2021-06-23T17:42:13 Z alextodoran Race (IOI11_race) C++17
100 / 100
968 ms 38204 KB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>
#include "race.h"

using namespace std;

typedef long long ll;

const int K_MAX = 1000000;

struct Edge
{
    int to;
    int len;
};

int aux[K_MAX + 2];
int best_path (int n, int k, int edges[][2], int edgeLen[])
{
    vector <Edge> adj[n];
    for(int i = 0; i < n - 1; i++)
    {
        int u = edges[i][0], v = edges[i][1];
        adj[u].push_back(Edge{v, edgeLen[i]});
        adj[v].push_back(Edge{u, edgeLen[i]});
    }

    bool deleted[n];
    for(int u = 0; u < n; u++)
        deleted[u] = false;

    int subSize[n];
    int parent[n];

    vector <int> visited;
    function <void (int)> dfs = [&] (int u)
    {
        visited.push_back(u);

        subSize[u] = 1;
        for(Edge e : adj[u])
            if(e.to != parent[u] && deleted[e.to] == false)
            {
                parent[e.to] = u;
                dfs(e.to);
                subSize[u] += subSize[e.to];
            }
    };

    function <int (int)> find_centroid = [&] (int root)
    {
        parent[root] = -1;
        dfs(root);

        for(int u : visited)
        {
            bool isCentroid = true;
            for(Edge e : adj[u])
                if(e.to != parent[u] && deleted[e.to] == false)
                    isCentroid &= (subSize[e.to] <= subSize[root] / 2);
            if(parent[u] != -1)
                isCentroid &= ((subSize[root] - subSize[u]) <= subSize[root] / 2);
            if(isCentroid == true)
                return u;
        }

        visited.clear();
        return -1;
    };

    vector <pair <int, int>> minEdges;
    function <void (int, int, int)> get_lens = [&] (int u, int depth, int currLen)
    {
        if(currLen > k)
            return;
        minEdges.push_back(make_pair(currLen, depth));
        for(Edge e : adj[u])
            if(e.to != parent[u] && deleted[e.to] == false)
                get_lens(e.to, depth + 1, currLen + e.len);
    };
    for(int i = 1; i <= k; i++)
        aux[i] = INT_MAX;
    aux[0] = 0;

    int answer = INT_MAX;
    function <void (int)> solve = [&] (int root)
    {
        root = find_centroid(root);

        parent[root] = -1;
        dfs(root);
        visited.clear();

        vector <int> changed;
        for(Edge e : adj[root])
            if(deleted[e.to] == false)
            {
                get_lens(e.to, 1, e.len);
                for(pair <int, int> p : minEdges)
                {
                    if(aux[k - p.first] != INT_MAX)
                        answer = min(answer, aux[k - p.first] + p.second);
                }
                for(pair <int, int> p : minEdges)
                {
                    aux[p.first] = min(aux[p.first], p.second);
                    changed.push_back(p.first);
                }
                minEdges.clear();
            }
        for(int i : changed)
            aux[i] = INT_MAX;
        aux[0] = 0;

        deleted[root] = true;
        for(Edge e : adj[root])
            if(deleted[e.to] == false)
                solve(e.to);
    };

    solve(0);

    if(answer == INT_MAX)
        answer = -1;
    return answer;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 408 KB Output is correct
22 Correct 4 ms 3916 KB Output is correct
23 Correct 3 ms 3276 KB Output is correct
24 Correct 4 ms 3788 KB Output is correct
25 Correct 3 ms 3660 KB Output is correct
26 Correct 2 ms 1612 KB Output is correct
27 Correct 4 ms 3532 KB Output is correct
28 Correct 2 ms 1100 KB Output is correct
29 Correct 3 ms 1612 KB Output is correct
30 Correct 3 ms 1740 KB Output is correct
31 Correct 3 ms 2892 KB Output is correct
32 Correct 3 ms 3148 KB Output is correct
33 Correct 4 ms 3404 KB Output is correct
34 Correct 3 ms 2636 KB Output is correct
35 Correct 4 ms 3532 KB Output is correct
36 Correct 4 ms 4044 KB Output is correct
37 Correct 3 ms 3532 KB Output is correct
38 Correct 3 ms 2380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 273 ms 9416 KB Output is correct
20 Correct 231 ms 9440 KB Output is correct
21 Correct 335 ms 9752 KB Output is correct
22 Correct 249 ms 10296 KB Output is correct
23 Correct 179 ms 9752 KB Output is correct
24 Correct 138 ms 9620 KB Output is correct
25 Correct 238 ms 12208 KB Output is correct
26 Correct 181 ms 16192 KB Output is correct
27 Correct 288 ms 18848 KB Output is correct
28 Correct 650 ms 29904 KB Output is correct
29 Correct 689 ms 29144 KB Output is correct
30 Correct 306 ms 18888 KB Output is correct
31 Correct 285 ms 18780 KB Output is correct
32 Correct 408 ms 18776 KB Output is correct
33 Correct 494 ms 17592 KB Output is correct
34 Correct 477 ms 17500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 408 KB Output is correct
22 Correct 4 ms 3916 KB Output is correct
23 Correct 3 ms 3276 KB Output is correct
24 Correct 4 ms 3788 KB Output is correct
25 Correct 3 ms 3660 KB Output is correct
26 Correct 2 ms 1612 KB Output is correct
27 Correct 4 ms 3532 KB Output is correct
28 Correct 2 ms 1100 KB Output is correct
29 Correct 3 ms 1612 KB Output is correct
30 Correct 3 ms 1740 KB Output is correct
31 Correct 3 ms 2892 KB Output is correct
32 Correct 3 ms 3148 KB Output is correct
33 Correct 4 ms 3404 KB Output is correct
34 Correct 3 ms 2636 KB Output is correct
35 Correct 4 ms 3532 KB Output is correct
36 Correct 4 ms 4044 KB Output is correct
37 Correct 3 ms 3532 KB Output is correct
38 Correct 3 ms 2380 KB Output is correct
39 Correct 273 ms 9416 KB Output is correct
40 Correct 231 ms 9440 KB Output is correct
41 Correct 335 ms 9752 KB Output is correct
42 Correct 249 ms 10296 KB Output is correct
43 Correct 179 ms 9752 KB Output is correct
44 Correct 138 ms 9620 KB Output is correct
45 Correct 238 ms 12208 KB Output is correct
46 Correct 181 ms 16192 KB Output is correct
47 Correct 288 ms 18848 KB Output is correct
48 Correct 650 ms 29904 KB Output is correct
49 Correct 689 ms 29144 KB Output is correct
50 Correct 306 ms 18888 KB Output is correct
51 Correct 285 ms 18780 KB Output is correct
52 Correct 408 ms 18776 KB Output is correct
53 Correct 494 ms 17592 KB Output is correct
54 Correct 477 ms 17500 KB Output is correct
55 Correct 14 ms 1276 KB Output is correct
56 Correct 16 ms 1356 KB Output is correct
57 Correct 160 ms 10588 KB Output is correct
58 Correct 50 ms 10300 KB Output is correct
59 Correct 184 ms 16828 KB Output is correct
60 Correct 891 ms 38204 KB Output is correct
61 Correct 301 ms 21824 KB Output is correct
62 Correct 355 ms 26912 KB Output is correct
63 Correct 460 ms 26820 KB Output is correct
64 Correct 968 ms 26032 KB Output is correct
65 Correct 567 ms 22712 KB Output is correct
66 Correct 777 ms 34200 KB Output is correct
67 Correct 227 ms 27584 KB Output is correct
68 Correct 455 ms 27128 KB Output is correct
69 Correct 472 ms 27308 KB Output is correct
70 Correct 448 ms 25872 KB Output is correct