답안 #106302

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
106302 2019-04-17T20:09:32 Z alexpetrescu Fireworks (APIO16_fireworks) C++14
100 / 100
455 ms 73416 KB
#include <cstdio>
#include <queue>
#include <algorithm>
#include <vector>

//FILE *fin = fopen("a.in", "r"), *fout = fopen("a.out", "w");
#define fin stdin
#define fout stdout

#define ll long long

#define MAXN 300000

std::vector < int > g[MAXN + 1];
ll d[MAXN + 1];
int c[MAXN + 1], k, u[MAXN + 1];
std::priority_queue < ll > q[MAXN + 1];

void dfs(int x) {
    for (auto &y : g[x]) {
        dfs(y);
        d[x] += d[y];
        if (q[u[y]].size() > q[u[x]].size())
            u[x] = u[y];
    }
    for (auto &y : g[x]) {
        if (u[x] != u[y]) {
            while (!q[u[y]].empty()) {
                q[u[x]].push(q[u[y]].top());
                q[u[y]].pop();
            }
        }
    }
    for (int i = g[x].size(); i > bool(c[x]); i--) {
        d[x] += q[u[x]].top();
        q[u[x]].pop();
    }
    if (g[x].empty()) {
        u[x] = ++k;
        q[u[x]].push(c[x]);
        q[u[x]].push(c[x]);
        d[x] = -c[x];
    } else if (c[x]) {
        ll a = q[u[x]].top();
        q[u[x]].pop();
        ll b = q[u[x]].top();
        q[u[x]].pop();
        q[u[x]].push(a + c[x]);
        q[u[x]].push(b + c[x]);
        d[x] -= c[x];
    }
}

int main() {
    int n, m;
    fscanf(fin, "%d%d", &n, &m);
    n += m;

    for (int i = 2; i <= n; i++) {
        int x;
        fscanf(fin, "%d%d", &x, &c[i]);
        g[x].push_back(i);
    }

    dfs(1);

    fprintf(fout, "%lld\n", d[1]);

    fclose(fin);
    fclose(fout);
    return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:56:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf(fin, "%d%d", &n, &m);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~
fireworks.cpp:61:15: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf(fin, "%d%d", &x, &c[i]);
         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 16768 KB Output is correct
2 Correct 18 ms 16768 KB Output is correct
3 Correct 17 ms 16768 KB Output is correct
4 Correct 16 ms 16768 KB Output is correct
5 Correct 20 ms 16768 KB Output is correct
6 Correct 19 ms 16768 KB Output is correct
7 Correct 17 ms 16772 KB Output is correct
8 Correct 15 ms 16768 KB Output is correct
9 Correct 16 ms 16768 KB Output is correct
10 Correct 17 ms 16768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16788 KB Output is correct
2 Correct 16 ms 16768 KB Output is correct
3 Correct 17 ms 16768 KB Output is correct
4 Correct 16 ms 16768 KB Output is correct
5 Correct 19 ms 16768 KB Output is correct
6 Correct 17 ms 16768 KB Output is correct
7 Correct 17 ms 16768 KB Output is correct
8 Correct 16 ms 16768 KB Output is correct
9 Correct 17 ms 16768 KB Output is correct
10 Correct 18 ms 16768 KB Output is correct
11 Correct 18 ms 16768 KB Output is correct
12 Correct 17 ms 16768 KB Output is correct
13 Correct 16 ms 16768 KB Output is correct
14 Correct 16 ms 16768 KB Output is correct
15 Correct 17 ms 16740 KB Output is correct
16 Correct 19 ms 16768 KB Output is correct
17 Correct 16 ms 16768 KB Output is correct
18 Correct 17 ms 16768 KB Output is correct
19 Correct 19 ms 16768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 16768 KB Output is correct
2 Correct 18 ms 16768 KB Output is correct
3 Correct 17 ms 16768 KB Output is correct
4 Correct 16 ms 16768 KB Output is correct
5 Correct 20 ms 16768 KB Output is correct
6 Correct 19 ms 16768 KB Output is correct
7 Correct 17 ms 16772 KB Output is correct
8 Correct 15 ms 16768 KB Output is correct
9 Correct 16 ms 16768 KB Output is correct
10 Correct 17 ms 16768 KB Output is correct
11 Correct 17 ms 16788 KB Output is correct
12 Correct 16 ms 16768 KB Output is correct
13 Correct 17 ms 16768 KB Output is correct
14 Correct 16 ms 16768 KB Output is correct
15 Correct 19 ms 16768 KB Output is correct
16 Correct 17 ms 16768 KB Output is correct
17 Correct 17 ms 16768 KB Output is correct
18 Correct 16 ms 16768 KB Output is correct
19 Correct 17 ms 16768 KB Output is correct
20 Correct 18 ms 16768 KB Output is correct
21 Correct 18 ms 16768 KB Output is correct
22 Correct 17 ms 16768 KB Output is correct
23 Correct 16 ms 16768 KB Output is correct
24 Correct 16 ms 16768 KB Output is correct
25 Correct 17 ms 16740 KB Output is correct
26 Correct 19 ms 16768 KB Output is correct
27 Correct 16 ms 16768 KB Output is correct
28 Correct 17 ms 16768 KB Output is correct
29 Correct 19 ms 16768 KB Output is correct
30 Correct 17 ms 16768 KB Output is correct
31 Correct 19 ms 16920 KB Output is correct
32 Correct 17 ms 16896 KB Output is correct
33 Correct 17 ms 16896 KB Output is correct
34 Correct 21 ms 17016 KB Output is correct
35 Correct 22 ms 17016 KB Output is correct
36 Correct 20 ms 17024 KB Output is correct
37 Correct 20 ms 17152 KB Output is correct
38 Correct 22 ms 17152 KB Output is correct
39 Correct 22 ms 17144 KB Output is correct
40 Correct 19 ms 17636 KB Output is correct
41 Correct 21 ms 17664 KB Output is correct
42 Correct 19 ms 17204 KB Output is correct
43 Correct 19 ms 17408 KB Output is correct
44 Correct 25 ms 17280 KB Output is correct
45 Correct 19 ms 17280 KB Output is correct
46 Correct 20 ms 17280 KB Output is correct
47 Correct 19 ms 17280 KB Output is correct
48 Correct 20 ms 17152 KB Output is correct
49 Correct 22 ms 17284 KB Output is correct
50 Correct 23 ms 17144 KB Output is correct
51 Correct 20 ms 17108 KB Output is correct
52 Correct 24 ms 17144 KB Output is correct
53 Correct 21 ms 17152 KB Output is correct
54 Correct 20 ms 17240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 16768 KB Output is correct
2 Correct 18 ms 16768 KB Output is correct
3 Correct 17 ms 16768 KB Output is correct
4 Correct 16 ms 16768 KB Output is correct
5 Correct 20 ms 16768 KB Output is correct
6 Correct 19 ms 16768 KB Output is correct
7 Correct 17 ms 16772 KB Output is correct
8 Correct 15 ms 16768 KB Output is correct
9 Correct 16 ms 16768 KB Output is correct
10 Correct 17 ms 16768 KB Output is correct
11 Correct 17 ms 16788 KB Output is correct
12 Correct 16 ms 16768 KB Output is correct
13 Correct 17 ms 16768 KB Output is correct
14 Correct 16 ms 16768 KB Output is correct
15 Correct 19 ms 16768 KB Output is correct
16 Correct 17 ms 16768 KB Output is correct
17 Correct 17 ms 16768 KB Output is correct
18 Correct 16 ms 16768 KB Output is correct
19 Correct 17 ms 16768 KB Output is correct
20 Correct 18 ms 16768 KB Output is correct
21 Correct 18 ms 16768 KB Output is correct
22 Correct 17 ms 16768 KB Output is correct
23 Correct 16 ms 16768 KB Output is correct
24 Correct 16 ms 16768 KB Output is correct
25 Correct 17 ms 16740 KB Output is correct
26 Correct 19 ms 16768 KB Output is correct
27 Correct 16 ms 16768 KB Output is correct
28 Correct 17 ms 16768 KB Output is correct
29 Correct 19 ms 16768 KB Output is correct
30 Correct 17 ms 16768 KB Output is correct
31 Correct 19 ms 16920 KB Output is correct
32 Correct 17 ms 16896 KB Output is correct
33 Correct 17 ms 16896 KB Output is correct
34 Correct 21 ms 17016 KB Output is correct
35 Correct 22 ms 17016 KB Output is correct
36 Correct 20 ms 17024 KB Output is correct
37 Correct 20 ms 17152 KB Output is correct
38 Correct 22 ms 17152 KB Output is correct
39 Correct 22 ms 17144 KB Output is correct
40 Correct 19 ms 17636 KB Output is correct
41 Correct 21 ms 17664 KB Output is correct
42 Correct 19 ms 17204 KB Output is correct
43 Correct 19 ms 17408 KB Output is correct
44 Correct 25 ms 17280 KB Output is correct
45 Correct 19 ms 17280 KB Output is correct
46 Correct 20 ms 17280 KB Output is correct
47 Correct 19 ms 17280 KB Output is correct
48 Correct 20 ms 17152 KB Output is correct
49 Correct 22 ms 17284 KB Output is correct
50 Correct 23 ms 17144 KB Output is correct
51 Correct 20 ms 17108 KB Output is correct
52 Correct 24 ms 17144 KB Output is correct
53 Correct 21 ms 17152 KB Output is correct
54 Correct 20 ms 17240 KB Output is correct
55 Correct 24 ms 17784 KB Output is correct
56 Correct 54 ms 20600 KB Output is correct
57 Correct 89 ms 23336 KB Output is correct
58 Correct 123 ms 25196 KB Output is correct
59 Correct 159 ms 28008 KB Output is correct
60 Correct 188 ms 30708 KB Output is correct
61 Correct 248 ms 33008 KB Output is correct
62 Correct 253 ms 34696 KB Output is correct
63 Correct 325 ms 38012 KB Output is correct
64 Correct 455 ms 38880 KB Output is correct
65 Correct 165 ms 73364 KB Output is correct
66 Correct 157 ms 73416 KB Output is correct
67 Correct 164 ms 35836 KB Output is correct
68 Correct 194 ms 55744 KB Output is correct
69 Correct 268 ms 52580 KB Output is correct
70 Correct 256 ms 52396 KB Output is correct
71 Correct 224 ms 49764 KB Output is correct
72 Correct 277 ms 49788 KB Output is correct
73 Correct 188 ms 46684 KB Output is correct
74 Correct 217 ms 46696 KB Output is correct
75 Correct 213 ms 45560 KB Output is correct
76 Correct 245 ms 45544 KB Output is correct
77 Correct 196 ms 44388 KB Output is correct
78 Correct 232 ms 42052 KB Output is correct
79 Correct 215 ms 43868 KB Output is correct