Submission #572567

# Submission time Handle Problem Language Result Execution time Memory
572567 2022-06-04T17:13:14 Z vladislav11 Race (IOI11_race) C++17
100 / 100
473 ms 56540 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int n, ans;
ll k;
vector< vector< pair<int,int> > > grp;
vector<int> ssize, h;
vector<ll> pref;

int dfs_prec ( int v, int p )
{
    for ( auto& [to,w] : grp[v] )
    if ( to != p )
    {
        h[to] = h[v] + 1;
        pref[to] = pref[v] + w;

        ssize[v] += dfs_prec( to, v );
    }

    return ssize[v];
}

vector< vector<int>* > ver;
map<ll,int> mini;

void upd ( int in, int v )
{
    if ( pref[in] + k == pref[v] )
        ans = min( ans, h[v] - h[in] );

    else if ( pref[v] - pref[in] < k && mini.count( pref[in] + k - (pref[v] - pref[in]) ) )
        ans = min( ans, ( h[v] - h[in] ) + ( mini[ pref[in] + k - (pref[v] - pref[in]) ] - h[in] ) );
}

void add ( int v )
{
    if ( mini.count( pref[v] ) )
        mini[ pref[v] ] = min( mini[ pref[v] ], h[v] );

    else
        mini[ pref[v] ] = h[v];
}

void dfs ( int v, int p, bool keep )
{
    //cout << "dfs: " << v << ' ';

    int bigs = -1, bigv = -1;

    for ( auto& [to,w] : grp[v] )
    if ( to != p && ssize[to] > bigs )
    {
        bigs = ssize[to];
        bigv = to;
    }

    //cout << bigs << ' ' << bigv << '\n';

    for ( auto& [to,w] : grp[v] )
    if ( to != p && to != bigv )
        dfs( to, v, false );

    if ( bigv == -1 )
        ver[v] = new vector<int>();
    else
    {
        dfs( bigv, v, true );
        ver[v] = ver[bigv];
    }

    ver[v]->push_back( v );

    for ( auto& [to,w] : grp[v] )
    if ( to != p && to != bigv )
    {
        for ( auto& el : *ver[to] )
            upd( v, el );

        for ( auto& el : *ver[to] )
        {
            ver[v]->push_back( el );
            add( el );
        }
    }

    if ( mini.count( pref[v] + k ) )
        ans = min( ans, mini[ pref[v] + k ] - h[v] );

    mini[ pref[v] ] = h[v];

    /*cout << "mini " << v << ": ";
    for ( auto& [x,y] : mini )
        cout << "(" << x << ' ' << y << ") ";
    cout << '\n';*/

    if ( !keep )
        mini.clear();
}

int best_path ( int n_, int k_, int(*h_)[2], int* l_ )
{
    n = n_;
    k = k_;

    grp.assign( n, vector< pair<int,int> >() );

    for ( int i=0; i<n-1; i++ )
    {
        grp[ h_[i][0] ].push_back({ h_[i][1], l_[i] });
        grp[ h_[i][1] ].push_back({ h_[i][0], l_[i] });
    }

    ssize.assign( n, 1 );
    h.assign( n, 1 );
    pref.assign( n, 0 );

    dfs_prec( 0, -1 );

    ver.assign( n, nullptr );
    mini.clear();
    ans = 2e9;

    dfs( 0, -1, false );

    return ans == 2e9 ? -1 : ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 308 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 304 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 308 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 304 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 2 ms 452 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 2 ms 436 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 308 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 304 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 136 ms 16456 KB Output is correct
20 Correct 136 ms 16360 KB Output is correct
21 Correct 146 ms 16540 KB Output is correct
22 Correct 131 ms 16712 KB Output is correct
23 Correct 210 ms 17548 KB Output is correct
24 Correct 131 ms 19056 KB Output is correct
25 Correct 79 ms 17568 KB Output is correct
26 Correct 45 ms 22160 KB Output is correct
27 Correct 194 ms 31108 KB Output is correct
28 Correct 214 ms 56540 KB Output is correct
29 Correct 209 ms 54928 KB Output is correct
30 Correct 193 ms 31016 KB Output is correct
31 Correct 207 ms 31048 KB Output is correct
32 Correct 234 ms 31264 KB Output is correct
33 Correct 225 ms 25632 KB Output is correct
34 Correct 330 ms 38040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 308 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 304 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 2 ms 452 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 2 ms 436 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 440 KB Output is correct
39 Correct 136 ms 16456 KB Output is correct
40 Correct 136 ms 16360 KB Output is correct
41 Correct 146 ms 16540 KB Output is correct
42 Correct 131 ms 16712 KB Output is correct
43 Correct 210 ms 17548 KB Output is correct
44 Correct 131 ms 19056 KB Output is correct
45 Correct 79 ms 17568 KB Output is correct
46 Correct 45 ms 22160 KB Output is correct
47 Correct 194 ms 31108 KB Output is correct
48 Correct 214 ms 56540 KB Output is correct
49 Correct 209 ms 54928 KB Output is correct
50 Correct 193 ms 31016 KB Output is correct
51 Correct 207 ms 31048 KB Output is correct
52 Correct 234 ms 31264 KB Output is correct
53 Correct 225 ms 25632 KB Output is correct
54 Correct 330 ms 38040 KB Output is correct
55 Correct 13 ms 2260 KB Output is correct
56 Correct 10 ms 1808 KB Output is correct
57 Correct 91 ms 17556 KB Output is correct
58 Correct 52 ms 18324 KB Output is correct
59 Correct 68 ms 28344 KB Output is correct
60 Correct 223 ms 55348 KB Output is correct
61 Correct 220 ms 32616 KB Output is correct
62 Correct 198 ms 31044 KB Output is correct
63 Correct 239 ms 31292 KB Output is correct
64 Correct 473 ms 39428 KB Output is correct
65 Correct 456 ms 46148 KB Output is correct
66 Correct 220 ms 51584 KB Output is correct
67 Correct 182 ms 37276 KB Output is correct
68 Correct 350 ms 43704 KB Output is correct
69 Correct 337 ms 44412 KB Output is correct
70 Correct 329 ms 41932 KB Output is correct