Submission #237253

# Submission time Handle Problem Language Result Execution time Memory
237253 2020-06-05T13:07:14 Z DodgeBallMan Fireworks (APIO16_fireworks) C++14
100 / 100
367 ms 82680 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second

using namespace std;

const int N = 3e5 + 10;
long long high[N], slope[N], par[N];
int sz[N], hv[N], n, m;
priority_queue<long long> F;
vector<pii> g[N];


void dfs( int u, int p ) {
    pii hh( 0, -1 );
    sz[u] = 1;
    for( pii v : g[u] ) if( v.x != p ) {
        dfs( v.x, u );
        par[v.x] = v.y;
        sz[u] += sz[v.x];
        hh = max( hh, pii( sz[v.x], v.x ) );
    }
    hv[u] = hh.y;
    return ;
}

void sack( int u, int p, priority_queue<long long> &Q ) { 
    //printf("U : %d P : %d\n",u,p);
    if( sz[u] == 1 ) {
        high[u] = par[u], slope[u] = -1LL;
        Q.emplace( par[u] ), Q.emplace( par[u] );
        return ;
    }
    sack( hv[u], u, Q );
    high[u] = high[hv[u]], slope[u] = slope[hv[u]];
    priority_queue<long long> S;
    for( pii v : g[u] ) if( v.x != p && v.x != hv[u] ) {
        sack( v.x, u, S );
        high[u] += high[v.x], slope[u] += slope[v.x];
        while( !S.empty() ) {
            Q.emplace( S.top() );
            S.pop();
        }
    }
    if( !p ) {
        //printf("%d %d\n",u,p);
        vector<long long> v;
        while( !Q.empty() ) {
            v.emplace_back( Q.top() );
            Q.pop();
        }
        reverse( v.begin(), v.end() );
        long long pv = 0, no = 0;
        for( int i = 0 ; slope[u] < 0 ; slope[u]++, i++ ) {
            no += ( v[i] - pv ) * slope[u];
            pv = v[i];
        }
        printf("%lld\n",high[u]+no);
    }
    else {
        long long a, b;

        while( slope[u] + (int)Q.size() >= 0 ) {
            swap( a, b );
            a = Q.top();
            //printf("%lld %lld %d\n",a,b,(int)Q.size()); 
            Q.pop();
        }
        Q.emplace( a + par[u] ), Q.emplace( b + par[u] );
        /*printf("%d %d\nTEMP : ",u,p);
        priority_queue<long long> temp = Q;
        while( !temp.empty() ) {
            printf("%d ",temp.top());
            temp.pop();
        }
        printf("\n");*/
        high[u] += par[u];
    }
}

int main()
{
    scanf("%d %d",&n,&m); n += m;
    for( int i = 2, a, b ; i <= n ; i++ ) {
        scanf("%d %d",&a,&b);
        g[i].emplace_back( pii( a, b ) );
        g[a].emplace_back( pii( i, b ) );
    }
    dfs( 1, 0 );
    sack( 1, 0, F );
    return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&n,&m); n += m;
     ~~~~~^~~~~~~~~~~~~~~
fireworks.cpp:86:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&a,&b);
         ~~~~~^~~~~~~~~~~~~~~
fireworks.cpp: In function 'void sack(int, int, std::priority_queue<long long int>&)':
fireworks.cpp:70:47: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
         Q.emplace( a + par[u] ), Q.emplace( b + par[u] );
                                             ~~^~~~~~~~
fireworks.cpp:70:22: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
         Q.emplace( a + par[u] ), Q.emplace( b + par[u] );
                    ~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 8 ms 7424 KB Output is correct
3 Correct 9 ms 7424 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 8 ms 7424 KB Output is correct
7 Correct 8 ms 7424 KB Output is correct
8 Correct 9 ms 7424 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 8 ms 7424 KB Output is correct
3 Correct 8 ms 7424 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 8 ms 7424 KB Output is correct
6 Correct 9 ms 7424 KB Output is correct
7 Correct 9 ms 7424 KB Output is correct
8 Correct 9 ms 7424 KB Output is correct
9 Correct 8 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 9 ms 7424 KB Output is correct
12 Correct 9 ms 7424 KB Output is correct
13 Correct 9 ms 7424 KB Output is correct
14 Correct 9 ms 7424 KB Output is correct
15 Correct 9 ms 7424 KB Output is correct
16 Correct 9 ms 7424 KB Output is correct
17 Correct 9 ms 7424 KB Output is correct
18 Correct 9 ms 7424 KB Output is correct
19 Correct 9 ms 7552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 8 ms 7424 KB Output is correct
3 Correct 9 ms 7424 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 8 ms 7424 KB Output is correct
7 Correct 8 ms 7424 KB Output is correct
8 Correct 9 ms 7424 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 9 ms 7424 KB Output is correct
12 Correct 8 ms 7424 KB Output is correct
13 Correct 8 ms 7424 KB Output is correct
14 Correct 9 ms 7424 KB Output is correct
15 Correct 8 ms 7424 KB Output is correct
16 Correct 9 ms 7424 KB Output is correct
17 Correct 9 ms 7424 KB Output is correct
18 Correct 9 ms 7424 KB Output is correct
19 Correct 8 ms 7424 KB Output is correct
20 Correct 9 ms 7424 KB Output is correct
21 Correct 9 ms 7424 KB Output is correct
22 Correct 9 ms 7424 KB Output is correct
23 Correct 9 ms 7424 KB Output is correct
24 Correct 9 ms 7424 KB Output is correct
25 Correct 9 ms 7424 KB Output is correct
26 Correct 9 ms 7424 KB Output is correct
27 Correct 9 ms 7424 KB Output is correct
28 Correct 9 ms 7424 KB Output is correct
29 Correct 9 ms 7552 KB Output is correct
30 Correct 9 ms 7424 KB Output is correct
31 Correct 12 ms 7424 KB Output is correct
32 Correct 10 ms 7552 KB Output is correct
33 Correct 10 ms 7552 KB Output is correct
34 Correct 11 ms 7680 KB Output is correct
35 Correct 11 ms 7680 KB Output is correct
36 Correct 11 ms 7680 KB Output is correct
37 Correct 13 ms 7808 KB Output is correct
38 Correct 12 ms 7808 KB Output is correct
39 Correct 12 ms 7808 KB Output is correct
40 Correct 11 ms 8576 KB Output is correct
41 Correct 12 ms 8576 KB Output is correct
42 Correct 11 ms 7680 KB Output is correct
43 Correct 12 ms 8320 KB Output is correct
44 Correct 12 ms 8064 KB Output is correct
45 Correct 12 ms 8192 KB Output is correct
46 Correct 12 ms 7936 KB Output is correct
47 Correct 12 ms 7936 KB Output is correct
48 Correct 12 ms 7936 KB Output is correct
49 Correct 13 ms 7936 KB Output is correct
50 Correct 12 ms 7808 KB Output is correct
51 Correct 11 ms 7808 KB Output is correct
52 Correct 11 ms 7936 KB Output is correct
53 Correct 12 ms 7936 KB Output is correct
54 Correct 12 ms 8064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 8 ms 7424 KB Output is correct
3 Correct 9 ms 7424 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 8 ms 7424 KB Output is correct
7 Correct 8 ms 7424 KB Output is correct
8 Correct 9 ms 7424 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 9 ms 7424 KB Output is correct
12 Correct 8 ms 7424 KB Output is correct
13 Correct 8 ms 7424 KB Output is correct
14 Correct 9 ms 7424 KB Output is correct
15 Correct 8 ms 7424 KB Output is correct
16 Correct 9 ms 7424 KB Output is correct
17 Correct 9 ms 7424 KB Output is correct
18 Correct 9 ms 7424 KB Output is correct
19 Correct 8 ms 7424 KB Output is correct
20 Correct 9 ms 7424 KB Output is correct
21 Correct 9 ms 7424 KB Output is correct
22 Correct 9 ms 7424 KB Output is correct
23 Correct 9 ms 7424 KB Output is correct
24 Correct 9 ms 7424 KB Output is correct
25 Correct 9 ms 7424 KB Output is correct
26 Correct 9 ms 7424 KB Output is correct
27 Correct 9 ms 7424 KB Output is correct
28 Correct 9 ms 7424 KB Output is correct
29 Correct 9 ms 7552 KB Output is correct
30 Correct 9 ms 7424 KB Output is correct
31 Correct 12 ms 7424 KB Output is correct
32 Correct 10 ms 7552 KB Output is correct
33 Correct 10 ms 7552 KB Output is correct
34 Correct 11 ms 7680 KB Output is correct
35 Correct 11 ms 7680 KB Output is correct
36 Correct 11 ms 7680 KB Output is correct
37 Correct 13 ms 7808 KB Output is correct
38 Correct 12 ms 7808 KB Output is correct
39 Correct 12 ms 7808 KB Output is correct
40 Correct 11 ms 8576 KB Output is correct
41 Correct 12 ms 8576 KB Output is correct
42 Correct 11 ms 7680 KB Output is correct
43 Correct 12 ms 8320 KB Output is correct
44 Correct 12 ms 8064 KB Output is correct
45 Correct 12 ms 8192 KB Output is correct
46 Correct 12 ms 7936 KB Output is correct
47 Correct 12 ms 7936 KB Output is correct
48 Correct 12 ms 7936 KB Output is correct
49 Correct 13 ms 7936 KB Output is correct
50 Correct 12 ms 7808 KB Output is correct
51 Correct 11 ms 7808 KB Output is correct
52 Correct 11 ms 7936 KB Output is correct
53 Correct 12 ms 7936 KB Output is correct
54 Correct 12 ms 8064 KB Output is correct
55 Correct 19 ms 8448 KB Output is correct
56 Correct 52 ms 11448 KB Output is correct
57 Correct 97 ms 13948 KB Output is correct
58 Correct 127 ms 15844 KB Output is correct
59 Correct 162 ms 20660 KB Output is correct
60 Correct 218 ms 23728 KB Output is correct
61 Correct 247 ms 26544 KB Output is correct
62 Correct 279 ms 28372 KB Output is correct
63 Correct 344 ms 31980 KB Output is correct
64 Correct 367 ms 33360 KB Output is correct
65 Correct 192 ms 82680 KB Output is correct
66 Correct 182 ms 82656 KB Output is correct
67 Correct 196 ms 31224 KB Output is correct
68 Correct 244 ms 63340 KB Output is correct
69 Correct 288 ms 58732 KB Output is correct
70 Correct 283 ms 58732 KB Output is correct
71 Correct 259 ms 42972 KB Output is correct
72 Correct 254 ms 42920 KB Output is correct
73 Correct 231 ms 41996 KB Output is correct
74 Correct 233 ms 41904 KB Output is correct
75 Correct 236 ms 41572 KB Output is correct
76 Correct 234 ms 41780 KB Output is correct
77 Correct 241 ms 40672 KB Output is correct
78 Correct 238 ms 41336 KB Output is correct
79 Correct 241 ms 49052 KB Output is correct