Submission #866453

# Submission time Handle Problem Language Result Execution time Memory
866453 2023-10-26T08:01:18 Z Hando Race (IOI11_race) C++17
100 / 100
642 ms 35784 KB
#include "race.h"

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ar array
#define vt vector
#define pq priority_queue
#define pu push
#define pub push_back
#define em emplace
#define emb emplace_back
#define mt make_tuple

#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define allp(x, l, r) x.begin() + l, x.begin() + r
#define len(x) (int)x.size()
#define uniq(x) unique(all(x)), x.end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

struct Graph {
    int n, k;
    int tot = 2e9;
    map <int, int> H;
    vt <ar <int, 2>> paths;
    vt <vt <ar <int, 2>>> adj;

    Graph(int n, int k): n(n), k(k) {
        H.clear();
        paths.clear();
        adj.assign(n, vt <ar <int, 2>>());
    }

    void add_edge(int u, int v, int w) {
        adj[u].pub({v, w});
        adj[v].pub({u, w});
    }

    void dfs(int u, int p, int c, int lvl = 1) {
        if (c > k) {
            return; 
        }     
        paths.pub({c, lvl});
        for (auto& [v, w] : adj[u]) {
            if (v == p || rem[v]) 
                continue;
            dfs(v, u, c + w, lvl + 1);
        }
    }

    //Centroid Decomposition
    vt <bool> rem;
    vt <int> par, sz;

    void initCD() {
        sz.resize(n);
        rem.resize(n);
        par.resize(n);
        build(0, -1);
    }

    void build(int u, int p) {
        int n = dfsC(u, p);
        int centroid = get_centroid(u, p, n);
        
        par[centroid] = p;
        rem[centroid] = true;

        H.clear();
        H[0] = 0;
        for (auto& [v, w] : adj[centroid]) {
            if (rem[v]) {
                continue;
            }

            paths.clear();
            dfs(v, centroid, w);
            for (auto& [path, nr] : paths) {
                if (H.count(k - path))
                  tot = min(tot, H[k - path] + nr);
            }

            for (auto& [path, nr] : paths) {
                if (H.count(path)) H[path] = min(H[path], nr);
                else H[path] = nr;
            }
        }

        for (auto& [v, w] : adj[centroid]) {
            if (rem[v]) {
                continue;
            }
            build(v, centroid);
        }
    }

    int dfsC(int u, int p) {
        sz[u] = 1;
        for (auto& [v, w] : adj[u]) {
            if (v == p || rem[v])
                continue;
            dfsC(v, u);
            sz[u] += sz[v];
        }
        return sz[u];
    }

    int get_centroid(int u, int p, int n) {
        for (auto& [v, w] : adj[u]) {
            if (v == p || rem[v])
                continue;
            if (sz[v] * 2 > n)
                return get_centroid(v, u, n);
        }
        return u;
    }
};

int best_path(int N, int K, int H[][2], int L[]) {
    Graph G(N, K);
    for (int i = 0; i < N - 1; ++i) {
      G.add_edge(H[i][0], H[i][1], L[i]);
    }
    G.initCD();
    return G.tot == 2e9? -1 : G.tot;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2492 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2492 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 1 ms 2392 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2500 KB Output is correct
25 Correct 2 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2392 KB Output is correct
28 Correct 2 ms 2396 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 2 ms 2504 KB Output is correct
31 Correct 2 ms 2396 KB Output is correct
32 Correct 2 ms 2396 KB Output is correct
33 Correct 2 ms 2508 KB Output is correct
34 Correct 2 ms 2396 KB Output is correct
35 Correct 1 ms 2396 KB Output is correct
36 Correct 1 ms 2396 KB Output is correct
37 Correct 2 ms 2396 KB Output is correct
38 Correct 2 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2492 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 135 ms 12868 KB Output is correct
20 Correct 128 ms 12916 KB Output is correct
21 Correct 143 ms 13092 KB Output is correct
22 Correct 146 ms 13272 KB Output is correct
23 Correct 58 ms 13144 KB Output is correct
24 Correct 56 ms 13096 KB Output is correct
25 Correct 98 ms 13872 KB Output is correct
26 Correct 81 ms 16236 KB Output is correct
27 Correct 154 ms 21588 KB Output is correct
28 Correct 167 ms 26908 KB Output is correct
29 Correct 174 ms 26528 KB Output is correct
30 Correct 134 ms 21584 KB Output is correct
31 Correct 144 ms 21412 KB Output is correct
32 Correct 147 ms 21488 KB Output is correct
33 Correct 188 ms 20128 KB Output is correct
34 Correct 156 ms 21076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2492 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 1 ms 2392 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2500 KB Output is correct
25 Correct 2 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2392 KB Output is correct
28 Correct 2 ms 2396 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 2 ms 2504 KB Output is correct
31 Correct 2 ms 2396 KB Output is correct
32 Correct 2 ms 2396 KB Output is correct
33 Correct 2 ms 2508 KB Output is correct
34 Correct 2 ms 2396 KB Output is correct
35 Correct 1 ms 2396 KB Output is correct
36 Correct 1 ms 2396 KB Output is correct
37 Correct 2 ms 2396 KB Output is correct
38 Correct 2 ms 2396 KB Output is correct
39 Correct 135 ms 12868 KB Output is correct
40 Correct 128 ms 12916 KB Output is correct
41 Correct 143 ms 13092 KB Output is correct
42 Correct 146 ms 13272 KB Output is correct
43 Correct 58 ms 13144 KB Output is correct
44 Correct 56 ms 13096 KB Output is correct
45 Correct 98 ms 13872 KB Output is correct
46 Correct 81 ms 16236 KB Output is correct
47 Correct 154 ms 21588 KB Output is correct
48 Correct 167 ms 26908 KB Output is correct
49 Correct 174 ms 26528 KB Output is correct
50 Correct 134 ms 21584 KB Output is correct
51 Correct 144 ms 21412 KB Output is correct
52 Correct 147 ms 21488 KB Output is correct
53 Correct 188 ms 20128 KB Output is correct
54 Correct 156 ms 21076 KB Output is correct
55 Correct 14 ms 3364 KB Output is correct
56 Correct 10 ms 3404 KB Output is correct
57 Correct 104 ms 13516 KB Output is correct
58 Correct 40 ms 12744 KB Output is correct
59 Correct 224 ms 19648 KB Output is correct
60 Correct 619 ms 35784 KB Output is correct
61 Correct 207 ms 21556 KB Output is correct
62 Correct 204 ms 21588 KB Output is correct
63 Correct 220 ms 21640 KB Output is correct
64 Correct 642 ms 27072 KB Output is correct
65 Correct 138 ms 22108 KB Output is correct
66 Correct 391 ms 25680 KB Output is correct
67 Correct 117 ms 22212 KB Output is correct
68 Correct 309 ms 25776 KB Output is correct
69 Correct 335 ms 25964 KB Output is correct
70 Correct 328 ms 24924 KB Output is correct