답안 #119003

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
119003 2019-06-20T07:14:16 Z PeppaPig Fireworks (APIO16_fireworks) C++14
100 / 100
407 ms 81532 KB
#include <bits/stdc++.h>

#define long long long
#define pii pair<int, int>
#define x first
#define y second

using namespace std;

const int N = 3e5+5;

int n, m;
vector<pii> g[N];

int sz[N], hv[N], par[N];

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

long high[N], slope[N];

void sack(int u, int p, priority_queue<long> &Q) {
    if(sz[u] == 1) {
        high[u] = par[u], slope[u] = -1;
        Q.emplace(par[u]);
        Q.emplace(par[u]);
    } else {
        sack(hv[u], u, Q);
        high[u] = high[hv[u]], slope[u] = slope[hv[u]];
        priority_queue<long> T;
        for(pii v : g[u]) if(v.x != p && v.x != hv[u]) {
            sack(v.x, u, T);
            high[u] += high[v.x], slope[u] += slope[v.x];
            while(!T.empty()) {
                Q.emplace(T.top());
                T.pop();
            }
        }
        if(u == 1) {
            vector<long> v;
            while(!Q.empty()) {
                v.emplace_back(Q.top());
                Q.pop();
            } 
            reverse(v.begin(), v.end());
            long pv = 0, ans = high[u];
            for(int i = 0; slope[u] < 0; i++) {
                ans += (v[i] - pv) * slope[u]++;
                pv = v[i];
            }

            printf("%lld\n", ans);
        } else {
            long a, b;
            while(slope[u] + (int)Q.size() >= 0) {
                swap(a, b);
                a = Q.top(); Q.pop();
            }
            Q.emplace(a + par[u]), Q.emplace(b + par[u]);
            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(a, b);
        g[a].emplace_back(i, b);
    }
    pre_process(1, 1);
    priority_queue<long> Q;
    sack(1, 1, Q);

    return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:74:27: 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:76: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:67:48: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
             Q.emplace(a + par[u]), Q.emplace(b + par[u]);
                                              ~~^~~~~~~~
fireworks.cpp:67:25: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
             Q.emplace(a + par[u]), Q.emplace(b + par[u]);
                       ~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7424 KB Output is correct
2 Correct 7 ms 7424 KB Output is correct
3 Correct 8 ms 7424 KB Output is correct
4 Correct 7 ms 7424 KB Output is correct
5 Correct 8 ms 7424 KB Output is correct
6 Correct 7 ms 7396 KB Output is correct
7 Correct 7 ms 7424 KB Output is correct
8 Correct 7 ms 7424 KB Output is correct
9 Correct 7 ms 7424 KB Output is correct
10 Correct 7 ms 7424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7424 KB Output is correct
2 Correct 7 ms 7424 KB Output is correct
3 Correct 7 ms 7424 KB Output is correct
4 Correct 7 ms 7424 KB Output is correct
5 Correct 7 ms 7424 KB Output is correct
6 Correct 8 ms 7424 KB Output is correct
7 Correct 5 ms 7396 KB Output is correct
8 Correct 8 ms 7424 KB Output is correct
9 Correct 7 ms 7424 KB Output is correct
10 Correct 8 ms 7424 KB Output is correct
11 Correct 7 ms 7424 KB Output is correct
12 Correct 7 ms 7424 KB Output is correct
13 Correct 7 ms 7424 KB Output is correct
14 Correct 7 ms 7424 KB Output is correct
15 Correct 8 ms 7424 KB Output is correct
16 Correct 8 ms 7424 KB Output is correct
17 Correct 8 ms 7424 KB Output is correct
18 Correct 8 ms 7508 KB Output is correct
19 Correct 8 ms 7424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7424 KB Output is correct
2 Correct 7 ms 7424 KB Output is correct
3 Correct 8 ms 7424 KB Output is correct
4 Correct 7 ms 7424 KB Output is correct
5 Correct 8 ms 7424 KB Output is correct
6 Correct 7 ms 7396 KB Output is correct
7 Correct 7 ms 7424 KB Output is correct
8 Correct 7 ms 7424 KB Output is correct
9 Correct 7 ms 7424 KB Output is correct
10 Correct 7 ms 7424 KB Output is correct
11 Correct 8 ms 7424 KB Output is correct
12 Correct 7 ms 7424 KB Output is correct
13 Correct 7 ms 7424 KB Output is correct
14 Correct 7 ms 7424 KB Output is correct
15 Correct 7 ms 7424 KB Output is correct
16 Correct 8 ms 7424 KB Output is correct
17 Correct 5 ms 7396 KB Output is correct
18 Correct 8 ms 7424 KB Output is correct
19 Correct 7 ms 7424 KB Output is correct
20 Correct 8 ms 7424 KB Output is correct
21 Correct 7 ms 7424 KB Output is correct
22 Correct 7 ms 7424 KB Output is correct
23 Correct 7 ms 7424 KB Output is correct
24 Correct 7 ms 7424 KB Output is correct
25 Correct 8 ms 7424 KB Output is correct
26 Correct 8 ms 7424 KB Output is correct
27 Correct 8 ms 7424 KB Output is correct
28 Correct 8 ms 7508 KB Output is correct
29 Correct 8 ms 7424 KB Output is correct
30 Correct 10 ms 7424 KB Output is correct
31 Correct 8 ms 7488 KB Output is correct
32 Correct 10 ms 7552 KB Output is correct
33 Correct 10 ms 7680 KB Output is correct
34 Correct 11 ms 7680 KB Output is correct
35 Correct 10 ms 7808 KB Output is correct
36 Correct 10 ms 7808 KB Output is correct
37 Correct 11 ms 7808 KB Output is correct
38 Correct 11 ms 7936 KB Output is correct
39 Correct 11 ms 7808 KB Output is correct
40 Correct 11 ms 8576 KB Output is correct
41 Correct 10 ms 8576 KB Output is correct
42 Correct 10 ms 7732 KB Output is correct
43 Correct 11 ms 8320 KB Output is correct
44 Correct 11 ms 8192 KB Output is correct
45 Correct 12 ms 8192 KB Output is correct
46 Correct 11 ms 7936 KB Output is correct
47 Correct 16 ms 7936 KB Output is correct
48 Correct 11 ms 7936 KB Output is correct
49 Correct 13 ms 8064 KB Output is correct
50 Correct 10 ms 7936 KB Output is correct
51 Correct 10 ms 7936 KB Output is correct
52 Correct 10 ms 8064 KB Output is correct
53 Correct 11 ms 8064 KB Output is correct
54 Correct 12 ms 8064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7424 KB Output is correct
2 Correct 7 ms 7424 KB Output is correct
3 Correct 8 ms 7424 KB Output is correct
4 Correct 7 ms 7424 KB Output is correct
5 Correct 8 ms 7424 KB Output is correct
6 Correct 7 ms 7396 KB Output is correct
7 Correct 7 ms 7424 KB Output is correct
8 Correct 7 ms 7424 KB Output is correct
9 Correct 7 ms 7424 KB Output is correct
10 Correct 7 ms 7424 KB Output is correct
11 Correct 8 ms 7424 KB Output is correct
12 Correct 7 ms 7424 KB Output is correct
13 Correct 7 ms 7424 KB Output is correct
14 Correct 7 ms 7424 KB Output is correct
15 Correct 7 ms 7424 KB Output is correct
16 Correct 8 ms 7424 KB Output is correct
17 Correct 5 ms 7396 KB Output is correct
18 Correct 8 ms 7424 KB Output is correct
19 Correct 7 ms 7424 KB Output is correct
20 Correct 8 ms 7424 KB Output is correct
21 Correct 7 ms 7424 KB Output is correct
22 Correct 7 ms 7424 KB Output is correct
23 Correct 7 ms 7424 KB Output is correct
24 Correct 7 ms 7424 KB Output is correct
25 Correct 8 ms 7424 KB Output is correct
26 Correct 8 ms 7424 KB Output is correct
27 Correct 8 ms 7424 KB Output is correct
28 Correct 8 ms 7508 KB Output is correct
29 Correct 8 ms 7424 KB Output is correct
30 Correct 10 ms 7424 KB Output is correct
31 Correct 8 ms 7488 KB Output is correct
32 Correct 10 ms 7552 KB Output is correct
33 Correct 10 ms 7680 KB Output is correct
34 Correct 11 ms 7680 KB Output is correct
35 Correct 10 ms 7808 KB Output is correct
36 Correct 10 ms 7808 KB Output is correct
37 Correct 11 ms 7808 KB Output is correct
38 Correct 11 ms 7936 KB Output is correct
39 Correct 11 ms 7808 KB Output is correct
40 Correct 11 ms 8576 KB Output is correct
41 Correct 10 ms 8576 KB Output is correct
42 Correct 10 ms 7732 KB Output is correct
43 Correct 11 ms 8320 KB Output is correct
44 Correct 11 ms 8192 KB Output is correct
45 Correct 12 ms 8192 KB Output is correct
46 Correct 11 ms 7936 KB Output is correct
47 Correct 16 ms 7936 KB Output is correct
48 Correct 11 ms 7936 KB Output is correct
49 Correct 13 ms 8064 KB Output is correct
50 Correct 10 ms 7936 KB Output is correct
51 Correct 10 ms 7936 KB Output is correct
52 Correct 10 ms 8064 KB Output is correct
53 Correct 11 ms 8064 KB Output is correct
54 Correct 12 ms 8064 KB Output is correct
55 Correct 17 ms 8576 KB Output is correct
56 Correct 55 ms 11896 KB Output is correct
57 Correct 94 ms 14812 KB Output is correct
58 Correct 124 ms 17132 KB Output is correct
59 Correct 184 ms 20076 KB Output is correct
60 Correct 245 ms 22952 KB Output is correct
61 Correct 279 ms 25748 KB Output is correct
62 Correct 331 ms 27500 KB Output is correct
63 Correct 387 ms 31160 KB Output is correct
64 Correct 407 ms 32452 KB Output is correct
65 Correct 178 ms 81532 KB Output is correct
66 Correct 175 ms 81532 KB Output is correct
67 Correct 202 ms 30200 KB Output is correct
68 Correct 232 ms 62184 KB Output is correct
69 Correct 323 ms 57660 KB Output is correct
70 Correct 320 ms 57700 KB Output is correct
71 Correct 240 ms 41824 KB Output is correct
72 Correct 241 ms 41820 KB Output is correct
73 Correct 241 ms 40704 KB Output is correct
74 Correct 221 ms 40892 KB Output is correct
75 Correct 219 ms 40416 KB Output is correct
76 Correct 221 ms 40572 KB Output is correct
77 Correct 225 ms 39868 KB Output is correct
78 Correct 223 ms 40196 KB Output is correct
79 Correct 243 ms 47828 KB Output is correct