Submission #417922

# Submission time Handle Problem Language Result Execution time Memory
417922 2021-06-04T14:26:53 Z OttoTheDino Race (IOI11_race) C++17
100 / 100
1195 ms 243612 KB
#include <bits/stdc++.h>
using namespace std;

#define rep(i,s,e)                          for (ll i = s; i <= e; ++i)
#define rrep(i,s,e)                         for (ll i = s; i >= e; --i)
#define pb                                  push_back
#define fi                                  first
#define se                                  second
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;

const ll mxn = 3e5;
ll ans = LLONG_MAX, dep[mxn], hdep[mxn], sz[mxn], bot[mxn], k;
vii neibs[mxn];
map<ll, ll> deep[mxn];
map<ll, bool> been[mxn];

void get_depth (ll u, ll prev) {
    //cout << u << endl;
    sz[u] = 1;
    for (ii vd : neibs[u]) {
        ll v = vd.fi, d = vd.se;
        if (v == prev) continue;
        dep[v] = dep[u]+d;
        hdep[v] = hdep[u]+1;
        get_depth (v, u);
        sz[u] += sz[v];
    }
}

void dfs (ll u, ll prev) {
    //cout << u << endl;
    ll heavy = -1, ma = 0;
    for (ii vd : neibs[u]) {
        ll v = vd.fi;  
        if (v == prev) continue;
        dfs (v, u);
        if (sz[v] > ma) {
            ma = sz[v];
            heavy = v;
        }
    }

    if (heavy==-1) {
        bot[u] = u;
        deep[u][dep[u]] = hdep[u]; 
        been[u][dep[u]] = 1;
        return;
    }

    bot[u] = bot[heavy];

    for (ii vd : neibs[u]) {
        ll v = vd.fi;
        if (v == prev || v == heavy) continue;

        for (ii deps : deep[bot[v]]) {
            ll real_dep = deps.fi, high_dep = deps.se;
            ll need_dep = k+2*dep[u]-real_dep;
            //cout << ans << endl;
            if (been[bot[u]][need_dep]) ans = min(ans, high_dep+deep[bot[u]][need_dep]-2*hdep[u]);
            //cout << ans << " " << u << "\n";
        }

        for (ii deps : deep[bot[v]]) {
            ll real_dep = deps.fi, high_dep = deps.se;
            if (!been[bot[u]][real_dep] || high_dep < deep[bot[u]][real_dep]) {
                deep[bot[u]][real_dep] = high_dep;
                been[bot[u]][real_dep] = 1;
            }
        }
    }

    ll need_dep = k+dep[u];
    if (been[bot[u]][need_dep]) ans = min(ans, deep[bot[u]][need_dep]-hdep[u]);
    if (!been[bot[u]][dep[u]] || hdep[u] < deep[bot[u]][dep[u]]) {
        deep[bot[u]][dep[u]] = hdep[u];
        been[bot[u]][dep[u]] = 1;
    }
}

int best_path (int n, int ok, int h[][2], int l[]) {
    k = ok;
    rep (i,0,n-2) {
        ll a = h[i][0], b = h[i][1], c = l[i];
        neibs[a].pb({b,c}), neibs[b].pb({a,c});
    }

    get_depth(0,-1);

    //cout << "got depth!" << endl;

    dfs (0, -1);

    return (ans==LLONG_MAX?-1:ans);
}

//int main() {
//    ios::sync_with_stdio(0);
//    cin.tie(0);

  //first test case

//   int a1[3][2] = {{0,1},{1,2},{1,3}};
//   int b1[3] = {1,2,4};
//
//   cout << best_path (4, 3, a1, b1) << "\n";
//   cout << "first done" << endl;
//
//  //second test case
//  
//    int a2[2][2] = {{0,1},{1,2}};
//    int b2[2] = {1,1};
//
//    cout << best_path (3, 3, a2, b2) << "\n";
//    cout << "second done" << endl;
//
//  //third test case
//
//    int a3[10][2] = {{0,1},{0,2},{2,3},{3,4},{4,5},{0,6},{6,7},{6,8},{8,9},{8,10}};
//    int b3[10] = {3,4,5,4,6,3,2,5,6,7};
//
//    cout << best_path (11, 12, a3, b3) << "\n";
//    cout << "thrird done" << endl;
//    

    //custom one test case
//    int ac1[3][2] = {{0,1},{1,2},{2,3}};
//    int bc1[3] = {5,5,5};
//
//    cout << best_path (3,15,ac1,bc1) << "\n";
//    
//    return 0;
//}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 35532 KB Output is correct
2 Correct 28 ms 35552 KB Output is correct
3 Correct 19 ms 35532 KB Output is correct
4 Correct 20 ms 35464 KB Output is correct
5 Correct 28 ms 35504 KB Output is correct
6 Correct 20 ms 35540 KB Output is correct
7 Correct 23 ms 35532 KB Output is correct
8 Correct 19 ms 35576 KB Output is correct
9 Correct 20 ms 35484 KB Output is correct
10 Correct 20 ms 35532 KB Output is correct
11 Correct 19 ms 35516 KB Output is correct
12 Correct 30 ms 35584 KB Output is correct
13 Correct 20 ms 35532 KB Output is correct
14 Correct 20 ms 35596 KB Output is correct
15 Correct 18 ms 35548 KB Output is correct
16 Correct 19 ms 35496 KB Output is correct
17 Correct 20 ms 35524 KB Output is correct
18 Correct 20 ms 35568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 35532 KB Output is correct
2 Correct 28 ms 35552 KB Output is correct
3 Correct 19 ms 35532 KB Output is correct
4 Correct 20 ms 35464 KB Output is correct
5 Correct 28 ms 35504 KB Output is correct
6 Correct 20 ms 35540 KB Output is correct
7 Correct 23 ms 35532 KB Output is correct
8 Correct 19 ms 35576 KB Output is correct
9 Correct 20 ms 35484 KB Output is correct
10 Correct 20 ms 35532 KB Output is correct
11 Correct 19 ms 35516 KB Output is correct
12 Correct 30 ms 35584 KB Output is correct
13 Correct 20 ms 35532 KB Output is correct
14 Correct 20 ms 35596 KB Output is correct
15 Correct 18 ms 35548 KB Output is correct
16 Correct 19 ms 35496 KB Output is correct
17 Correct 20 ms 35524 KB Output is correct
18 Correct 20 ms 35568 KB Output is correct
19 Correct 21 ms 35532 KB Output is correct
20 Correct 19 ms 35584 KB Output is correct
21 Correct 23 ms 36044 KB Output is correct
22 Correct 22 ms 36180 KB Output is correct
23 Correct 22 ms 36172 KB Output is correct
24 Correct 22 ms 36180 KB Output is correct
25 Correct 34 ms 36180 KB Output is correct
26 Correct 31 ms 36164 KB Output is correct
27 Correct 21 ms 35776 KB Output is correct
28 Correct 22 ms 36188 KB Output is correct
29 Correct 23 ms 36148 KB Output is correct
30 Correct 28 ms 36156 KB Output is correct
31 Correct 26 ms 36172 KB Output is correct
32 Correct 22 ms 36172 KB Output is correct
33 Correct 22 ms 36196 KB Output is correct
34 Correct 21 ms 36008 KB Output is correct
35 Correct 20 ms 35924 KB Output is correct
36 Correct 21 ms 35912 KB Output is correct
37 Correct 20 ms 35920 KB Output is correct
38 Correct 21 ms 35920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 35532 KB Output is correct
2 Correct 28 ms 35552 KB Output is correct
3 Correct 19 ms 35532 KB Output is correct
4 Correct 20 ms 35464 KB Output is correct
5 Correct 28 ms 35504 KB Output is correct
6 Correct 20 ms 35540 KB Output is correct
7 Correct 23 ms 35532 KB Output is correct
8 Correct 19 ms 35576 KB Output is correct
9 Correct 20 ms 35484 KB Output is correct
10 Correct 20 ms 35532 KB Output is correct
11 Correct 19 ms 35516 KB Output is correct
12 Correct 30 ms 35584 KB Output is correct
13 Correct 20 ms 35532 KB Output is correct
14 Correct 20 ms 35596 KB Output is correct
15 Correct 18 ms 35548 KB Output is correct
16 Correct 19 ms 35496 KB Output is correct
17 Correct 20 ms 35524 KB Output is correct
18 Correct 20 ms 35568 KB Output is correct
19 Correct 281 ms 75728 KB Output is correct
20 Correct 287 ms 76896 KB Output is correct
21 Correct 269 ms 77128 KB Output is correct
22 Correct 270 ms 76892 KB Output is correct
23 Correct 406 ms 117776 KB Output is correct
24 Correct 326 ms 91408 KB Output is correct
25 Correct 163 ms 57552 KB Output is correct
26 Correct 98 ms 64684 KB Output is correct
27 Correct 377 ms 88944 KB Output is correct
28 Correct 443 ms 131648 KB Output is correct
29 Correct 446 ms 131584 KB Output is correct
30 Correct 362 ms 88772 KB Output is correct
31 Correct 384 ms 89024 KB Output is correct
32 Correct 514 ms 90080 KB Output is correct
33 Correct 397 ms 87316 KB Output is correct
34 Correct 855 ms 191208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 35532 KB Output is correct
2 Correct 28 ms 35552 KB Output is correct
3 Correct 19 ms 35532 KB Output is correct
4 Correct 20 ms 35464 KB Output is correct
5 Correct 28 ms 35504 KB Output is correct
6 Correct 20 ms 35540 KB Output is correct
7 Correct 23 ms 35532 KB Output is correct
8 Correct 19 ms 35576 KB Output is correct
9 Correct 20 ms 35484 KB Output is correct
10 Correct 20 ms 35532 KB Output is correct
11 Correct 19 ms 35516 KB Output is correct
12 Correct 30 ms 35584 KB Output is correct
13 Correct 20 ms 35532 KB Output is correct
14 Correct 20 ms 35596 KB Output is correct
15 Correct 18 ms 35548 KB Output is correct
16 Correct 19 ms 35496 KB Output is correct
17 Correct 20 ms 35524 KB Output is correct
18 Correct 20 ms 35568 KB Output is correct
19 Correct 21 ms 35532 KB Output is correct
20 Correct 19 ms 35584 KB Output is correct
21 Correct 23 ms 36044 KB Output is correct
22 Correct 22 ms 36180 KB Output is correct
23 Correct 22 ms 36172 KB Output is correct
24 Correct 22 ms 36180 KB Output is correct
25 Correct 34 ms 36180 KB Output is correct
26 Correct 31 ms 36164 KB Output is correct
27 Correct 21 ms 35776 KB Output is correct
28 Correct 22 ms 36188 KB Output is correct
29 Correct 23 ms 36148 KB Output is correct
30 Correct 28 ms 36156 KB Output is correct
31 Correct 26 ms 36172 KB Output is correct
32 Correct 22 ms 36172 KB Output is correct
33 Correct 22 ms 36196 KB Output is correct
34 Correct 21 ms 36008 KB Output is correct
35 Correct 20 ms 35924 KB Output is correct
36 Correct 21 ms 35912 KB Output is correct
37 Correct 20 ms 35920 KB Output is correct
38 Correct 21 ms 35920 KB Output is correct
39 Correct 281 ms 75728 KB Output is correct
40 Correct 287 ms 76896 KB Output is correct
41 Correct 269 ms 77128 KB Output is correct
42 Correct 270 ms 76892 KB Output is correct
43 Correct 406 ms 117776 KB Output is correct
44 Correct 326 ms 91408 KB Output is correct
45 Correct 163 ms 57552 KB Output is correct
46 Correct 98 ms 64684 KB Output is correct
47 Correct 377 ms 88944 KB Output is correct
48 Correct 443 ms 131648 KB Output is correct
49 Correct 446 ms 131584 KB Output is correct
50 Correct 362 ms 88772 KB Output is correct
51 Correct 384 ms 89024 KB Output is correct
52 Correct 514 ms 90080 KB Output is correct
53 Correct 397 ms 87316 KB Output is correct
54 Correct 855 ms 191208 KB Output is correct
55 Correct 56 ms 42436 KB Output is correct
56 Correct 38 ms 38932 KB Output is correct
57 Correct 167 ms 75344 KB Output is correct
58 Correct 90 ms 58152 KB Output is correct
59 Correct 190 ms 83236 KB Output is correct
60 Correct 530 ms 131036 KB Output is correct
61 Correct 488 ms 99844 KB Output is correct
62 Correct 397 ms 90052 KB Output is correct
63 Correct 474 ms 90048 KB Output is correct
64 Correct 1142 ms 243612 KB Output is correct
65 Correct 1195 ms 241752 KB Output is correct
66 Correct 509 ms 131848 KB Output is correct
67 Correct 455 ms 82476 KB Output is correct
68 Correct 1004 ms 141816 KB Output is correct
69 Correct 1008 ms 142912 KB Output is correct
70 Correct 920 ms 137064 KB Output is correct