Submission #886940

# Submission time Handle Problem Language Result Execution time Memory
886940 2023-12-13T08:00:13 Z fryingduc Race (IOI11_race) C++17
100 / 100
319 ms 140732 KB
#include "bits/stdc++.h"
using namespace std;

//#define int long long

const int maxn = 1e6+5;
const int inf = 1e9+10;
int n, l;
int sz[maxn], tin[maxn], tout[maxn], timer;
long long h[maxn];
int dak[maxn], depth[maxn], et[maxn];
vector<pair<int, int>> g[maxn];
multiset<int> mn[maxn];
vector<long long> vec;
int ans = inf;
void pre_dfs(int u, int prev){
    sz[u] = 1;
    tin[u] = ++timer;
    et[timer] = u;
    for(auto e:g[u]){
        int v = e.first, w = e.second;
        if(v == prev) continue;
        depth[v] = depth[u] + 1;
        h[v] = h[u] + w;
        pre_dfs(v, u);
        sz[u] += sz[v];
    }
    tout[u] = timer;
}
void dfs(int u, int prev, bool keep){
    int bigchild = -1;
    for(auto e:g[u]){
        int v = e.first;
        if(v == prev) continue;
        if(bigchild == -1 || sz[bigchild] < sz[v]) bigchild = v;        
    }
    for(auto e:g[u]){
        int v = e.first;
        if(v == prev || v == bigchild) continue;
        dfs(v, u, 0);
    }
    if(bigchild != -1) dfs(bigchild, u, 1);
    
    int mim = lower_bound(vec.begin(), vec.end(), 1LL * h[u] + l) - vec.begin();
    if(mim < (int)vec.size() and vec[mim] == h[u] + l){
        if(mn[mim].size()) ans = min(ans, *mn[mim].begin() - depth[u]);
    }
    mn[dak[u]].insert(depth[u]);
    
    for(auto e:g[u]){
        int v = e.first;
        if(v == prev || v == bigchild) continue;
        
        for(int i = tin[v]; i <= tout[v]; ++i){
            long long val = 2LL * h[u] + l - h[et[i]];
            int pos = lower_bound(vec.begin(), vec.end(), val) - vec.begin();
            if(pos < (int)vec.size() and vec[pos] == val){
                if(mn[pos].size()) ans = min(ans, *mn[pos].begin() + depth[et[i]] - 2 * depth[u]);
            }
        }
        
        for(int i = tin[v]; i <= tout[v]; ++i){
            mn[dak[et[i]]].insert(depth[et[i]]);
        }
    }
    
    if(keep) return;
    for(int i = tin[u]; i <= tout[u]; ++i){
        mn[dak[et[i]]].erase(depth[et[i]]);
    }
    
}
int best_path(int _n, int _k, int edges[][2], int weights[]){
    n = _n, l = _k;
    for(int i = 0; i < n - 1; ++i){
        int u = edges[i][0], v = edges[i][1], w = weights[i]; 
        ++u, ++v;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    pre_dfs(1, 0);
    
    for(int i = 1; i <= n; ++i){
        vec.push_back(h[i]);
    }
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    for(int i = 1; i <= n; ++i){
        dak[i] = lower_bound(vec.begin(), vec.end(), h[i]) - vec.begin();
    }
    
    dfs(1, 0, 1);
    return (ans > n ? -1 : ans);
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 82520 KB Output is correct
2 Correct 18 ms 82524 KB Output is correct
3 Correct 16 ms 82524 KB Output is correct
4 Correct 15 ms 82524 KB Output is correct
5 Correct 16 ms 82460 KB Output is correct
6 Correct 16 ms 82524 KB Output is correct
7 Correct 16 ms 82524 KB Output is correct
8 Correct 15 ms 82524 KB Output is correct
9 Correct 16 ms 82520 KB Output is correct
10 Correct 17 ms 82568 KB Output is correct
11 Correct 17 ms 82524 KB Output is correct
12 Correct 16 ms 82404 KB Output is correct
13 Correct 16 ms 82568 KB Output is correct
14 Correct 16 ms 82508 KB Output is correct
15 Correct 16 ms 82524 KB Output is correct
16 Correct 17 ms 82520 KB Output is correct
17 Correct 15 ms 82524 KB Output is correct
18 Correct 16 ms 82524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 82520 KB Output is correct
2 Correct 18 ms 82524 KB Output is correct
3 Correct 16 ms 82524 KB Output is correct
4 Correct 15 ms 82524 KB Output is correct
5 Correct 16 ms 82460 KB Output is correct
6 Correct 16 ms 82524 KB Output is correct
7 Correct 16 ms 82524 KB Output is correct
8 Correct 15 ms 82524 KB Output is correct
9 Correct 16 ms 82520 KB Output is correct
10 Correct 17 ms 82568 KB Output is correct
11 Correct 17 ms 82524 KB Output is correct
12 Correct 16 ms 82404 KB Output is correct
13 Correct 16 ms 82568 KB Output is correct
14 Correct 16 ms 82508 KB Output is correct
15 Correct 16 ms 82524 KB Output is correct
16 Correct 17 ms 82520 KB Output is correct
17 Correct 15 ms 82524 KB Output is correct
18 Correct 16 ms 82524 KB Output is correct
19 Correct 16 ms 82520 KB Output is correct
20 Correct 16 ms 82524 KB Output is correct
21 Correct 17 ms 82524 KB Output is correct
22 Correct 17 ms 82432 KB Output is correct
23 Correct 17 ms 82524 KB Output is correct
24 Correct 17 ms 82524 KB Output is correct
25 Correct 17 ms 82524 KB Output is correct
26 Correct 17 ms 82524 KB Output is correct
27 Correct 16 ms 82524 KB Output is correct
28 Correct 18 ms 82520 KB Output is correct
29 Correct 18 ms 82520 KB Output is correct
30 Correct 17 ms 82524 KB Output is correct
31 Correct 17 ms 82528 KB Output is correct
32 Correct 18 ms 82520 KB Output is correct
33 Correct 18 ms 82524 KB Output is correct
34 Correct 17 ms 82520 KB Output is correct
35 Correct 16 ms 82520 KB Output is correct
36 Correct 17 ms 82524 KB Output is correct
37 Correct 16 ms 82524 KB Output is correct
38 Correct 17 ms 82524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 82520 KB Output is correct
2 Correct 18 ms 82524 KB Output is correct
3 Correct 16 ms 82524 KB Output is correct
4 Correct 15 ms 82524 KB Output is correct
5 Correct 16 ms 82460 KB Output is correct
6 Correct 16 ms 82524 KB Output is correct
7 Correct 16 ms 82524 KB Output is correct
8 Correct 15 ms 82524 KB Output is correct
9 Correct 16 ms 82520 KB Output is correct
10 Correct 17 ms 82568 KB Output is correct
11 Correct 17 ms 82524 KB Output is correct
12 Correct 16 ms 82404 KB Output is correct
13 Correct 16 ms 82568 KB Output is correct
14 Correct 16 ms 82508 KB Output is correct
15 Correct 16 ms 82524 KB Output is correct
16 Correct 17 ms 82520 KB Output is correct
17 Correct 15 ms 82524 KB Output is correct
18 Correct 16 ms 82524 KB Output is correct
19 Correct 153 ms 100332 KB Output is correct
20 Correct 151 ms 100384 KB Output is correct
21 Correct 175 ms 100240 KB Output is correct
22 Correct 143 ms 100552 KB Output is correct
23 Correct 185 ms 100728 KB Output is correct
24 Correct 146 ms 100504 KB Output is correct
25 Correct 86 ms 107216 KB Output is correct
26 Correct 67 ms 113868 KB Output is correct
27 Correct 209 ms 113976 KB Output is correct
28 Correct 191 ms 140732 KB Output is correct
29 Correct 184 ms 138648 KB Output is correct
30 Correct 186 ms 113976 KB Output is correct
31 Correct 217 ms 113608 KB Output is correct
32 Correct 230 ms 113680 KB Output is correct
33 Correct 226 ms 112784 KB Output is correct
34 Correct 265 ms 113172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 82520 KB Output is correct
2 Correct 18 ms 82524 KB Output is correct
3 Correct 16 ms 82524 KB Output is correct
4 Correct 15 ms 82524 KB Output is correct
5 Correct 16 ms 82460 KB Output is correct
6 Correct 16 ms 82524 KB Output is correct
7 Correct 16 ms 82524 KB Output is correct
8 Correct 15 ms 82524 KB Output is correct
9 Correct 16 ms 82520 KB Output is correct
10 Correct 17 ms 82568 KB Output is correct
11 Correct 17 ms 82524 KB Output is correct
12 Correct 16 ms 82404 KB Output is correct
13 Correct 16 ms 82568 KB Output is correct
14 Correct 16 ms 82508 KB Output is correct
15 Correct 16 ms 82524 KB Output is correct
16 Correct 17 ms 82520 KB Output is correct
17 Correct 15 ms 82524 KB Output is correct
18 Correct 16 ms 82524 KB Output is correct
19 Correct 16 ms 82520 KB Output is correct
20 Correct 16 ms 82524 KB Output is correct
21 Correct 17 ms 82524 KB Output is correct
22 Correct 17 ms 82432 KB Output is correct
23 Correct 17 ms 82524 KB Output is correct
24 Correct 17 ms 82524 KB Output is correct
25 Correct 17 ms 82524 KB Output is correct
26 Correct 17 ms 82524 KB Output is correct
27 Correct 16 ms 82524 KB Output is correct
28 Correct 18 ms 82520 KB Output is correct
29 Correct 18 ms 82520 KB Output is correct
30 Correct 17 ms 82524 KB Output is correct
31 Correct 17 ms 82528 KB Output is correct
32 Correct 18 ms 82520 KB Output is correct
33 Correct 18 ms 82524 KB Output is correct
34 Correct 17 ms 82520 KB Output is correct
35 Correct 16 ms 82520 KB Output is correct
36 Correct 17 ms 82524 KB Output is correct
37 Correct 16 ms 82524 KB Output is correct
38 Correct 17 ms 82524 KB Output is correct
39 Correct 153 ms 100332 KB Output is correct
40 Correct 151 ms 100384 KB Output is correct
41 Correct 175 ms 100240 KB Output is correct
42 Correct 143 ms 100552 KB Output is correct
43 Correct 185 ms 100728 KB Output is correct
44 Correct 146 ms 100504 KB Output is correct
45 Correct 86 ms 107216 KB Output is correct
46 Correct 67 ms 113868 KB Output is correct
47 Correct 209 ms 113976 KB Output is correct
48 Correct 191 ms 140732 KB Output is correct
49 Correct 184 ms 138648 KB Output is correct
50 Correct 186 ms 113976 KB Output is correct
51 Correct 217 ms 113608 KB Output is correct
52 Correct 230 ms 113680 KB Output is correct
53 Correct 226 ms 112784 KB Output is correct
54 Correct 265 ms 113172 KB Output is correct
55 Correct 25 ms 83576 KB Output is correct
56 Correct 24 ms 83548 KB Output is correct
57 Correct 153 ms 100488 KB Output is correct
58 Correct 91 ms 100208 KB Output is correct
59 Correct 65 ms 113816 KB Output is correct
60 Correct 183 ms 139292 KB Output is correct
61 Correct 200 ms 113564 KB Output is correct
62 Correct 168 ms 113596 KB Output is correct
63 Correct 233 ms 113740 KB Output is correct
64 Correct 268 ms 113880 KB Output is correct
65 Correct 319 ms 114528 KB Output is correct
66 Correct 195 ms 134080 KB Output is correct
67 Correct 170 ms 114548 KB Output is correct
68 Correct 262 ms 114496 KB Output is correct
69 Correct 283 ms 114600 KB Output is correct
70 Correct 272 ms 113444 KB Output is correct