Submission #1088969

# Submission time Handle Problem Language Result Execution time Memory
1088969 2024-09-15T16:33:00 Z Tymond Race (IOI11_race) C++17
100 / 100
259 ms 43316 KB
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

const int MAXN = 2e5 + 7;
vector<pii> g[MAXN];
bool blocked[MAXN];
int sz[MAXN];
int parent[MAXN];
int n, k;
ll ans;
ll m[1000010];
vector<pll> akt;

int dfs(int v, int p){
    sz[v] = 1;
    parent[v] = p;
    for(auto ele : g[v]){
        if(ele.fi == p || blocked[ele.fi]){
            continue;
        }
        sz[v] += dfs(ele.fi, v);
    }
    return sz[v];
}

void dfs2(int v, int p, pll s){
    if(s.fi > k){
        return;
    }
    akt.pb(s);
    for(auto ele : g[v]){
        if(blocked[ele.fi] || p == ele.fi || s.fi + ele.se > k){
            continue;
        }
        dfs2(ele.fi, v, {s.fi + ele.se, s.se + 1});
    }
}

void centroid_decomposition(int centroid){
    int roz = dfs(centroid, centroid);

    while(true){
        bool f = 0;
        for(auto ele : g[centroid]){
            if(ele.fi == parent[centroid] || blocked[ele.fi]){
                continue;
            }
            if(sz[ele.fi] * 2 >= roz){
                centroid = ele.fi;
                f = 1;
                break;
            }
        }
        if(!f){
            break;
        }
    }

    blocked[centroid] = 1;
    vector<pll> every;
    for(auto ele : g[centroid]){
        if(blocked[ele.fi]){
            continue;
        }
        akt.clear();
        dfs2(ele.fi, ele.fi, {ele.se, 1});
        for(auto ele2 : akt){
            every.pb(ele2);
            if(ele2.fi == k){
                ans = min(ans, ele2.se);
            }else if(ele2.fi < k){
                if(m[k - ele2.fi] != 0){
                    ans = min(ans, ele2.se + m[k - ele2.fi]);
                }
            }
        }

        for(auto ele2 : akt){
            if(m[ele2.fi] == 0){
                m[ele2.fi] = ele2.se;
            }else{
                m[ele2.fi] = min(m[ele2.fi], ele2.se);
            }
        }
    }
    for(auto ele : every){
        m[ele.fi] = 0;
    }

    for(auto ele : g[centroid]){
        if(blocked[ele.fi]){
            continue;
        }
        centroid_decomposition(ele.fi);
    }
}

int best_path(int N, int K, int H[][2], int L[]){
    ans = 1e9;
    n = N;
    k = K;
    //cout << n << ' ' << k << '\n';
    for(int i = 0; i < n - 1; i++){
        //cout << H[i][0] << ' ' << H[i][1] << ' ' << L[i] << '\n';
        g[H[i][0]].pb({H[i][1], L[i]});
        g[H[i][1]].pb({H[i][0], L[i]});
    }

    centroid_decomposition(0);
    if(ans == (ll)1e9){
        return -1;
    }
    return (int)ans;
}

/*int main(){
    int tab[3][2] = {{0, 1}, {1, 2}, {1, 3}};
    int l[3] = {1, 2, 4};
    cout << best_path(4, 3, tab, l) << '\n';
    return 0;
}*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 4 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 3 ms 4956 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 4 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 3 ms 4956 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 3 ms 5208 KB Output is correct
20 Correct 3 ms 4956 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 4 ms 8540 KB Output is correct
23 Correct 4 ms 8024 KB Output is correct
24 Correct 4 ms 9308 KB Output is correct
25 Correct 4 ms 8284 KB Output is correct
26 Correct 5 ms 7512 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 4 ms 6748 KB Output is correct
29 Correct 4 ms 7700 KB Output is correct
30 Correct 4 ms 7788 KB Output is correct
31 Correct 4 ms 8540 KB Output is correct
32 Correct 5 ms 8540 KB Output is correct
33 Correct 4 ms 10036 KB Output is correct
34 Correct 5 ms 9060 KB Output is correct
35 Correct 5 ms 10080 KB Output is correct
36 Correct 6 ms 10568 KB Output is correct
37 Correct 4 ms 8508 KB Output is correct
38 Correct 4 ms 7256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 4 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 3 ms 4956 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 73 ms 11568 KB Output is correct
20 Correct 76 ms 11368 KB Output is correct
21 Correct 74 ms 12692 KB Output is correct
22 Correct 92 ms 14372 KB Output is correct
23 Correct 52 ms 11096 KB Output is correct
24 Correct 35 ms 10844 KB Output is correct
25 Correct 89 ms 13956 KB Output is correct
26 Correct 90 ms 22112 KB Output is correct
27 Correct 103 ms 19792 KB Output is correct
28 Correct 181 ms 30928 KB Output is correct
29 Correct 162 ms 30036 KB Output is correct
30 Correct 100 ms 19868 KB Output is correct
31 Correct 123 ms 19792 KB Output is correct
32 Correct 152 ms 19796 KB Output is correct
33 Correct 133 ms 18516 KB Output is correct
34 Correct 125 ms 19284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 4 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 3 ms 4956 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 3 ms 5208 KB Output is correct
20 Correct 3 ms 4956 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 4 ms 8540 KB Output is correct
23 Correct 4 ms 8024 KB Output is correct
24 Correct 4 ms 9308 KB Output is correct
25 Correct 4 ms 8284 KB Output is correct
26 Correct 5 ms 7512 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 4 ms 6748 KB Output is correct
29 Correct 4 ms 7700 KB Output is correct
30 Correct 4 ms 7788 KB Output is correct
31 Correct 4 ms 8540 KB Output is correct
32 Correct 5 ms 8540 KB Output is correct
33 Correct 4 ms 10036 KB Output is correct
34 Correct 5 ms 9060 KB Output is correct
35 Correct 5 ms 10080 KB Output is correct
36 Correct 6 ms 10568 KB Output is correct
37 Correct 4 ms 8508 KB Output is correct
38 Correct 4 ms 7256 KB Output is correct
39 Correct 73 ms 11568 KB Output is correct
40 Correct 76 ms 11368 KB Output is correct
41 Correct 74 ms 12692 KB Output is correct
42 Correct 92 ms 14372 KB Output is correct
43 Correct 52 ms 11096 KB Output is correct
44 Correct 35 ms 10844 KB Output is correct
45 Correct 89 ms 13956 KB Output is correct
46 Correct 90 ms 22112 KB Output is correct
47 Correct 103 ms 19792 KB Output is correct
48 Correct 181 ms 30928 KB Output is correct
49 Correct 162 ms 30036 KB Output is correct
50 Correct 100 ms 19868 KB Output is correct
51 Correct 123 ms 19792 KB Output is correct
52 Correct 152 ms 19796 KB Output is correct
53 Correct 133 ms 18516 KB Output is correct
54 Correct 125 ms 19284 KB Output is correct
55 Correct 11 ms 6232 KB Output is correct
56 Correct 10 ms 6284 KB Output is correct
57 Correct 75 ms 15944 KB Output is correct
58 Correct 25 ms 14284 KB Output is correct
59 Correct 69 ms 22868 KB Output is correct
60 Correct 259 ms 43316 KB Output is correct
61 Correct 134 ms 20680 KB Output is correct
62 Correct 134 ms 23836 KB Output is correct
63 Correct 157 ms 23768 KB Output is correct
64 Correct 226 ms 30208 KB Output is correct
65 Correct 144 ms 20684 KB Output is correct
66 Correct 255 ms 36616 KB Output is correct
67 Correct 66 ms 24384 KB Output is correct
68 Correct 143 ms 30144 KB Output is correct
69 Correct 154 ms 30468 KB Output is correct
70 Correct 177 ms 29376 KB Output is correct