Submission #1000205

# Submission time Handle Problem Language Result Execution time Memory
1000205 2024-06-17T04:01:39 Z Icelast Race (IOI11_race) C++17
100 / 100
398 ms 107092 KB
#include "race.h"
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
struct Tree{
    int n, root;
    vector<int> depth, head, sz, pa;
    vector<int> f;
    Tree(int n, int root, vector<vector<int>> &adj): n(n), root(root){
        depth.resize(n+1, -1);
        head.resize(n+1);
        sz.resize(n+1, 0);
        pa.resize(n+1, -1);
        auto rootTree = [&](auto rootTree, int u, int p) -> void{
            depth[u] = depth[p]+1;
            pa[u] = p;
            sz[u] = 1;
            for(int v : adj[u]){
                if(v == p) continue;
                rootTree(rootTree, v, u);
                sz[u] += sz[v];
            }
        };
        rootTree(rootTree, root, 0);
        auto decompose = [&](auto decompose, int u, int h) -> void{
            head[u] = h;
            int heavy = -1;
            for(int v : adj[u]){
                if(v == pa[u]) continue;
                if(heavy == -1 || sz[heavy] < sz[v]) heavy = v;
            }
            if(heavy != -1) decompose(decompose, heavy, h);
            for(int v : adj[u]){
                if(v == pa[u] || v == heavy) continue;
                decompose(decompose, v, v);
            }
        };
        decompose(decompose, root, root);
        f = [&]{
            vector<array<int, 2>> first(n+1);
            vector<array<int, 2>> second(n+1);
            function<void(int, int)> dfs0 = [&](int u, int p) {
                first[u] = second[u] = {0, -1};
                for (int v : adj[u]) {
                    if (v == p) {
                        continue;
                    }
                    dfs0(v, u);
                    auto fetch = first[v];
                    fetch[0] += 1;
                    fetch[1] = v;
                    if (fetch > first[u]) {
                        swap(fetch, first[u]);
                    }
                    if (fetch > second[u]) {
                        swap(fetch, second[u]);
                    }
                }
            };
            dfs0(1, 0);

            function<void(int, int)> dfs = [&](int u, int p) {
                for (int v : adj[u]) {
                    if (v == p) {
                        continue;
                    }
                    auto fetch = first[u][1] == v ? second[u] : first[u];
                    fetch[0] += 1;
                    fetch[1] = u;
                    if (fetch > first[v]) {
                        swap(fetch, first[v]);
                    }
                    if (fetch > second[v]) {
                        swap(fetch, second[v]);
                    }
                    dfs(v, u);
                }
            };
            dfs(1, 0);
            vector<int> f(n+1);
            for (int u = 1; u <= n; u++) {
                f[u] = first[u][0];
            }
            return f;
        }();
    };
    int lca(int u, int v){
        for(; head[u] != head[v]; v = pa[head[v]]){
            if(depth[head[u]] > depth[head[v]]) swap(u, v);
        }
        if(depth[u] > depth[v]) swap(u, v);
        return u;
    }
    int dist(int u, int v) {
        return depth[u]+depth[v]-2*depth[lca(u, v)];
    }
};
int best_path(int N, int K, int H[][2], int L[])
{
    int n = N;
    vector<vector<pair<ll, ll>>> adj(n+1);
    for(int i = 0; i < n-1; i++){
        int u = H[i][0], v = H[i][1];
        u++; v++;
        adj[u].push_back({v, L[i]});
        adj[v].push_back({u, L[i]});
    }
    vector<int> pa(n+1), depth(n+1, 0);
    depth[0] = -1;
    vector<ll> path(n+1);
    auto rtree = [&](auto rtree, int u, int p, ll len) -> void{
        path[u] = len;
        depth[u] = depth[p]+1;
        pa[u] = p;
        for(auto it : adj[u]){
            int v = it.first, w = it.second;
            if(v == p) continue;
            rtree(rtree, v, u, len+w);
        }
    };
    rtree(rtree, 1, 0, 0);
    vector<map<ll, ll>> mp(n+1);
    ll ans = INF;
    auto dfs = [&](auto dfs, int u, int p) -> void{
        mp[u][path[u]] = depth[u];
        for(auto it : adj[u]){
            int v = it.first;
            if(v == p) continue;
            dfs(dfs, v, u);
            if(mp[u].size() < mp[v].size()) swap(mp[u], mp[v]);
            for(auto it : mp[v]){
                if(mp[u].count(K+2*path[u]-it.first)){
                    ans = min(ans, mp[u][K+2*path[u]-it.first]+it.second-2*depth[u]);
                }
            }
            for(auto it : mp[v]){
                if(mp[u].count(it.first)){
                    mp[u][it.first] = min(mp[u][it.first], it.second);
                }else{
                    mp[u][it.first] = it.second;
                }
            }
        }
        if(mp[u].count(K+path[u])){
            ans = min(ans, mp[u][K+path[u]]-depth[u]);
        }
    };
    dfs(dfs, 1, 0);
    if(ans == INF) ans = -1;
    return ans;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2400 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 2400 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2400 KB Output is correct
17 Correct 0 ms 2400 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2400 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 2400 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2400 KB Output is correct
17 Correct 0 ms 2400 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 1 ms 2392 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 2 ms 2656 KB Output is correct
23 Correct 2 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2648 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 2 ms 2652 KB Output is correct
31 Correct 2 ms 2656 KB Output is correct
32 Correct 1 ms 2660 KB Output is correct
33 Correct 2 ms 2916 KB Output is correct
34 Correct 1 ms 2660 KB Output is correct
35 Correct 1 ms 2760 KB Output is correct
36 Correct 1 ms 2760 KB Output is correct
37 Correct 2 ms 2656 KB Output is correct
38 Correct 1 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2400 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 2400 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2400 KB Output is correct
17 Correct 0 ms 2400 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 94 ms 34180 KB Output is correct
20 Correct 97 ms 33428 KB Output is correct
21 Correct 94 ms 32872 KB Output is correct
22 Correct 92 ms 33108 KB Output is correct
23 Correct 158 ms 45880 KB Output is correct
24 Correct 94 ms 37428 KB Output is correct
25 Correct 79 ms 38484 KB Output is correct
26 Correct 53 ms 48724 KB Output is correct
27 Correct 144 ms 53072 KB Output is correct
28 Correct 255 ms 104784 KB Output is correct
29 Correct 283 ms 102520 KB Output is correct
30 Correct 140 ms 53840 KB Output is correct
31 Correct 173 ms 53840 KB Output is correct
32 Correct 177 ms 53800 KB Output is correct
33 Correct 194 ms 58964 KB Output is correct
34 Correct 331 ms 91192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2400 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 2400 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2400 KB Output is correct
17 Correct 0 ms 2400 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 1 ms 2392 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 2 ms 2656 KB Output is correct
23 Correct 2 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2648 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 2 ms 2652 KB Output is correct
31 Correct 2 ms 2656 KB Output is correct
32 Correct 1 ms 2660 KB Output is correct
33 Correct 2 ms 2916 KB Output is correct
34 Correct 1 ms 2660 KB Output is correct
35 Correct 1 ms 2760 KB Output is correct
36 Correct 1 ms 2760 KB Output is correct
37 Correct 2 ms 2656 KB Output is correct
38 Correct 1 ms 2648 KB Output is correct
39 Correct 94 ms 34180 KB Output is correct
40 Correct 97 ms 33428 KB Output is correct
41 Correct 94 ms 32872 KB Output is correct
42 Correct 92 ms 33108 KB Output is correct
43 Correct 158 ms 45880 KB Output is correct
44 Correct 94 ms 37428 KB Output is correct
45 Correct 79 ms 38484 KB Output is correct
46 Correct 53 ms 48724 KB Output is correct
47 Correct 144 ms 53072 KB Output is correct
48 Correct 255 ms 104784 KB Output is correct
49 Correct 283 ms 102520 KB Output is correct
50 Correct 140 ms 53840 KB Output is correct
51 Correct 173 ms 53840 KB Output is correct
52 Correct 177 ms 53800 KB Output is correct
53 Correct 194 ms 58964 KB Output is correct
54 Correct 331 ms 91192 KB Output is correct
55 Correct 11 ms 6488 KB Output is correct
56 Correct 6 ms 4956 KB Output is correct
57 Correct 48 ms 31500 KB Output is correct
58 Correct 36 ms 25284 KB Output is correct
59 Correct 76 ms 54772 KB Output is correct
60 Correct 249 ms 103304 KB Output is correct
61 Correct 177 ms 56400 KB Output is correct
62 Correct 137 ms 53584 KB Output is correct
63 Correct 201 ms 53840 KB Output is correct
64 Correct 398 ms 107092 KB Output is correct
65 Correct 394 ms 106848 KB Output is correct
66 Correct 259 ms 94804 KB Output is correct
67 Correct 123 ms 48092 KB Output is correct
68 Correct 276 ms 69512 KB Output is correct
69 Correct 296 ms 72204 KB Output is correct
70 Correct 261 ms 67068 KB Output is correct