Submission #794446

# Submission time Handle Problem Language Result Execution time Memory
794446 2023-07-26T14:28:37 Z WLZ Fireworks (APIO16_fireworks) C++17
100 / 100
211 ms 80324 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "templates/debug.h"
#else
#define debug(...) 0
#endif

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define eb emplace_back
#define pb push_back
#define MP make_pair
#define MT make_tuple
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int) x.size()

using ll = long long;
using ull = unsigned ll;
using lll = __int128_t;
using ulll = __uint128_t;
using ld = long double;
using ii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<ii>;
template<typename T> using max_pq = priority_queue<T>;
template<typename T> using min_pq = priority_queue<T, vector<T>, greater<T>>;

template<typename T> void cmax(T &a, const T &b) { a = max(a, b); }
template<typename T> void cmin(T &a, const T &b) { a = min(a, b); }

const int INF = 0x3f3f3f3f;
const ll LINF = (ll) 1e18;
const double DINF = 1.0 / 0.0;
const double pi = acos(-1);
const double EPS = 1e-9;

void solve(int current_case);

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  //cin >> t;
  for (int q = 1; q <= t; q++) solve(q);
  return 0;
}

int n, m;
vi p, c;
vll a, b;
vector< max_pq<ll> > pq;
vector<vi> g;

int dfs(int u) {
  if (g[u].empty()) {
    pq[u].push(c[u]); pq[u].push(c[u]);
    a[u] = 1; b[u] = -c[u];
    debug(u, a[u], b[u], pq[u]);
    return u;
  }
  int rep = u;
  a[u] = b[u] = 0;
  for (auto &v : g[u]) {
    int other = dfs(v);
    a[u] += a[v]; b[u] += b[v];
    if (SZ(pq[other]) > SZ(pq[rep])) swap(rep, other);
    while (!pq[other].empty()) {
      pq[rep].push(pq[other].top());
      pq[other].pop();
    }
  }
  while (a[u] > 1) {
    b[u] += pq[rep].top(); pq[rep].pop();
    a[u]--;
  }
  ll lx = pq[rep].top(); pq[rep].pop(); // 0 -> 1
  ll ly = pq[rep].top(); pq[rep].pop(); // -1 -> 0
  pq[rep].push(lx + c[u]); pq[rep].push(ly + c[u]);
  b[u] -= c[u];
  debug(u, a[u], b[u], pq[rep]);
  return rep;
}

void solve(int current_case) {
  cin >> n >> m;
  g.resize(n + m + 1);
  p.resize(n + m + 1); c.resize(n + m + 1);
  rep(i, 2, n + m + 1) {
    cin >> p[i] >> c[i];
    g[p[i]].pb(i);
  }
  a.resize(n + m + 1); b.resize(n + m + 1);
  pq.assign(n + m + 1, max_pq<ll>());
  c[1] = 0;
  int rep = dfs(1);
  while (a[1] > 0) {
    b[1] += pq[rep].top(); pq[rep].pop();
    a[1]--;
  }
  cout << b[1] << '\n';
}

Compilation message

fireworks.cpp: In function 'int dfs(int)':
fireworks.cpp:7:20: warning: statement has no effect [-Wunused-value]
    7 | #define debug(...) 0
      |                    ^
fireworks.cpp:60:5: note: in expansion of macro 'debug'
   60 |     debug(u, a[u], b[u], pq[u]);
      |     ^~~~~
fireworks.cpp:7:20: warning: statement has no effect [-Wunused-value]
    7 | #define debug(...) 0
      |                    ^
fireworks.cpp:82:3: note: in expansion of macro 'debug'
   82 |   debug(u, a[u], b[u], pq[rep]);
      |   ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 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 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 320 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 592 KB Output is correct
34 Correct 2 ms 724 KB Output is correct
35 Correct 2 ms 724 KB Output is correct
36 Correct 2 ms 848 KB Output is correct
37 Correct 3 ms 992 KB Output is correct
38 Correct 2 ms 980 KB Output is correct
39 Correct 4 ms 928 KB Output is correct
40 Correct 2 ms 1620 KB Output is correct
41 Correct 2 ms 1620 KB Output is correct
42 Correct 2 ms 980 KB Output is correct
43 Correct 2 ms 1264 KB Output is correct
44 Correct 2 ms 1108 KB Output is correct
45 Correct 2 ms 1108 KB Output is correct
46 Correct 2 ms 1108 KB Output is correct
47 Correct 3 ms 1100 KB Output is correct
48 Correct 3 ms 980 KB Output is correct
49 Correct 2 ms 980 KB Output is correct
50 Correct 2 ms 852 KB Output is correct
51 Correct 2 ms 896 KB Output is correct
52 Correct 2 ms 980 KB Output is correct
53 Correct 2 ms 980 KB Output is correct
54 Correct 2 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 320 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 592 KB Output is correct
34 Correct 2 ms 724 KB Output is correct
35 Correct 2 ms 724 KB Output is correct
36 Correct 2 ms 848 KB Output is correct
37 Correct 3 ms 992 KB Output is correct
38 Correct 2 ms 980 KB Output is correct
39 Correct 4 ms 928 KB Output is correct
40 Correct 2 ms 1620 KB Output is correct
41 Correct 2 ms 1620 KB Output is correct
42 Correct 2 ms 980 KB Output is correct
43 Correct 2 ms 1264 KB Output is correct
44 Correct 2 ms 1108 KB Output is correct
45 Correct 2 ms 1108 KB Output is correct
46 Correct 2 ms 1108 KB Output is correct
47 Correct 3 ms 1100 KB Output is correct
48 Correct 3 ms 980 KB Output is correct
49 Correct 2 ms 980 KB Output is correct
50 Correct 2 ms 852 KB Output is correct
51 Correct 2 ms 896 KB Output is correct
52 Correct 2 ms 980 KB Output is correct
53 Correct 2 ms 980 KB Output is correct
54 Correct 2 ms 972 KB Output is correct
55 Correct 7 ms 2132 KB Output is correct
56 Correct 25 ms 7176 KB Output is correct
57 Correct 36 ms 12108 KB Output is correct
58 Correct 64 ms 15452 KB Output is correct
59 Correct 86 ms 20436 KB Output is correct
60 Correct 111 ms 25412 KB Output is correct
61 Correct 136 ms 29180 KB Output is correct
62 Correct 144 ms 32124 KB Output is correct
63 Correct 187 ms 38044 KB Output is correct
64 Correct 211 ms 40020 KB Output is correct
65 Correct 92 ms 80324 KB Output is correct
66 Correct 91 ms 80308 KB Output is correct
67 Correct 118 ms 38184 KB Output is correct
68 Correct 122 ms 60512 KB Output is correct
69 Correct 143 ms 56856 KB Output is correct
70 Correct 152 ms 56860 KB Output is correct
71 Correct 149 ms 50424 KB Output is correct
72 Correct 160 ms 50800 KB Output is correct
73 Correct 126 ms 47036 KB Output is correct
74 Correct 125 ms 47128 KB Output is correct
75 Correct 142 ms 45988 KB Output is correct
76 Correct 130 ms 46020 KB Output is correct
77 Correct 135 ms 45228 KB Output is correct
78 Correct 133 ms 43288 KB Output is correct
79 Correct 124 ms 44816 KB Output is correct