답안 #119002

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
119002 2019-06-20T07:13:22 Z PeppaPig Fireworks (APIO16_fireworks) C++14
26 / 100
12 ms 7448 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 {
            int 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 12 ms 7424 KB Output is correct
2 Correct 8 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 8 ms 7424 KB Output is correct
8 Correct 8 ms 7444 KB Output is correct
9 Correct 8 ms 7424 KB Output is correct
10 Correct 8 ms 7448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 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 8 ms 7420 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 8 ms 7424 KB Output is correct
7 Correct 11 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 8 ms 7424 KB Output is correct
11 Correct 8 ms 7424 KB Output is correct
12 Correct 9 ms 7424 KB Output is correct
13 Correct 8 ms 7424 KB Output is correct
14 Correct 8 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 7424 KB Output is correct
19 Correct 7 ms 7424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 7424 KB Output is correct
2 Correct 8 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 8 ms 7424 KB Output is correct
8 Correct 8 ms 7444 KB Output is correct
9 Correct 8 ms 7424 KB Output is correct
10 Correct 8 ms 7448 KB Output is correct
11 Correct 7 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 8 ms 7420 KB Output is correct
15 Correct 9 ms 7424 KB Output is correct
16 Correct 8 ms 7424 KB Output is correct
17 Correct 11 ms 7424 KB Output is correct
18 Correct 9 ms 7424 KB Output is correct
19 Correct 9 ms 7424 KB Output is correct
20 Correct 8 ms 7424 KB Output is correct
21 Correct 8 ms 7424 KB Output is correct
22 Correct 9 ms 7424 KB Output is correct
23 Correct 8 ms 7424 KB Output is correct
24 Correct 8 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 7424 KB Output is correct
29 Correct 7 ms 7424 KB Output is correct
30 Incorrect 8 ms 7424 KB Output isn't correct
31 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 7424 KB Output is correct
2 Correct 8 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 8 ms 7424 KB Output is correct
8 Correct 8 ms 7444 KB Output is correct
9 Correct 8 ms 7424 KB Output is correct
10 Correct 8 ms 7448 KB Output is correct
11 Correct 7 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 8 ms 7420 KB Output is correct
15 Correct 9 ms 7424 KB Output is correct
16 Correct 8 ms 7424 KB Output is correct
17 Correct 11 ms 7424 KB Output is correct
18 Correct 9 ms 7424 KB Output is correct
19 Correct 9 ms 7424 KB Output is correct
20 Correct 8 ms 7424 KB Output is correct
21 Correct 8 ms 7424 KB Output is correct
22 Correct 9 ms 7424 KB Output is correct
23 Correct 8 ms 7424 KB Output is correct
24 Correct 8 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 7424 KB Output is correct
29 Correct 7 ms 7424 KB Output is correct
30 Incorrect 8 ms 7424 KB Output isn't correct
31 Halted 0 ms 0 KB -