답안 #873048

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
873048 2023-11-14T11:06:22 Z NeroZein Magic Tree (CEOI19_magictree) C++17
100 / 100
109 ms 34576 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int N = 1e5 + 5;

vector<int> g[N]; 
pair<int, int> a[N]; 
map<int, long long> dp[N]; 

void merge(map<int, long long>& du, map<int, long long>& dv) {
  if (du.size() > dv.size()) {
    swap(du, dv);
  }
  for (auto it : du) {
    dv[it.first] += it.second; 
  }
}

void dfs(int v) {
  for (int u : g[v]) {
    dfs(u); 
    merge(dp[u], dp[v]); 
  }
  if (!a[v].first) return;
  long long tmp = a[v].second; 
  dp[v][a[v].first] += a[v].second; 
  auto it = dp[v].upper_bound(a[v].first);
  while (tmp && it != dp[v].end()) {
    if (tmp >= it->second) {
      tmp -= it->second;
      it = dp[v].erase(it); 
    } else {
      it->second -= tmp; 
      tmp = 0;
    }
  } 
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m, k;
  cin >> n >> m >> k; 
  for (int i = 2; i <= n; ++i) {
    int p;
    cin >> p;
    g[p].push_back(i); 
  }
  for (int i = 0; i < m; ++i) {
    int v, t, w;
    cin >> v >> t >> w;
    a[v] = {t, w}; 
  }
  dfs(1);
  long long ans = 0; 
  for (auto it : dp[1]) {
    ans += it.second;
  }
  cout << ans << '\n'; 
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 2 ms 8028 KB Output is correct
4 Correct 2 ms 8028 KB Output is correct
5 Correct 2 ms 8028 KB Output is correct
6 Correct 2 ms 8248 KB Output is correct
7 Correct 2 ms 8028 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 2 ms 8072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 17236 KB Output is correct
2 Correct 29 ms 16972 KB Output is correct
3 Correct 100 ms 34576 KB Output is correct
4 Correct 61 ms 17864 KB Output is correct
5 Correct 76 ms 19152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8284 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Correct 36 ms 19080 KB Output is correct
5 Correct 54 ms 23124 KB Output is correct
6 Correct 48 ms 19248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 13344 KB Output is correct
2 Correct 41 ms 14676 KB Output is correct
3 Correct 35 ms 16980 KB Output is correct
4 Correct 28 ms 15840 KB Output is correct
5 Correct 33 ms 21336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 2 ms 8028 KB Output is correct
4 Correct 2 ms 8028 KB Output is correct
5 Correct 2 ms 8028 KB Output is correct
6 Correct 2 ms 8248 KB Output is correct
7 Correct 2 ms 8028 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 2 ms 8072 KB Output is correct
10 Correct 47 ms 16212 KB Output is correct
11 Correct 40 ms 15452 KB Output is correct
12 Correct 33 ms 14920 KB Output is correct
13 Correct 30 ms 14300 KB Output is correct
14 Correct 32 ms 19036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8792 KB Output is correct
2 Correct 20 ms 10628 KB Output is correct
3 Correct 18 ms 10580 KB Output is correct
4 Correct 17 ms 10912 KB Output is correct
5 Correct 7 ms 8924 KB Output is correct
6 Correct 18 ms 11868 KB Output is correct
7 Correct 22 ms 14428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 2 ms 8028 KB Output is correct
4 Correct 2 ms 8028 KB Output is correct
5 Correct 2 ms 8028 KB Output is correct
6 Correct 2 ms 8248 KB Output is correct
7 Correct 2 ms 8028 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 2 ms 8072 KB Output is correct
10 Correct 2 ms 8284 KB Output is correct
11 Correct 2 ms 8284 KB Output is correct
12 Correct 2 ms 8284 KB Output is correct
13 Correct 36 ms 19080 KB Output is correct
14 Correct 54 ms 23124 KB Output is correct
15 Correct 48 ms 19248 KB Output is correct
16 Correct 47 ms 16212 KB Output is correct
17 Correct 40 ms 15452 KB Output is correct
18 Correct 33 ms 14920 KB Output is correct
19 Correct 30 ms 14300 KB Output is correct
20 Correct 32 ms 19036 KB Output is correct
21 Correct 16 ms 11352 KB Output is correct
22 Correct 58 ms 17796 KB Output is correct
23 Correct 74 ms 20756 KB Output is correct
24 Correct 93 ms 28500 KB Output is correct
25 Correct 62 ms 17876 KB Output is correct
26 Correct 65 ms 18516 KB Output is correct
27 Correct 51 ms 16468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 2 ms 8028 KB Output is correct
4 Correct 2 ms 8028 KB Output is correct
5 Correct 2 ms 8028 KB Output is correct
6 Correct 2 ms 8248 KB Output is correct
7 Correct 2 ms 8028 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 2 ms 8072 KB Output is correct
10 Correct 48 ms 17236 KB Output is correct
11 Correct 29 ms 16972 KB Output is correct
12 Correct 100 ms 34576 KB Output is correct
13 Correct 61 ms 17864 KB Output is correct
14 Correct 76 ms 19152 KB Output is correct
15 Correct 2 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 2 ms 8284 KB Output is correct
18 Correct 36 ms 19080 KB Output is correct
19 Correct 54 ms 23124 KB Output is correct
20 Correct 48 ms 19248 KB Output is correct
21 Correct 45 ms 13344 KB Output is correct
22 Correct 41 ms 14676 KB Output is correct
23 Correct 35 ms 16980 KB Output is correct
24 Correct 28 ms 15840 KB Output is correct
25 Correct 33 ms 21336 KB Output is correct
26 Correct 47 ms 16212 KB Output is correct
27 Correct 40 ms 15452 KB Output is correct
28 Correct 33 ms 14920 KB Output is correct
29 Correct 30 ms 14300 KB Output is correct
30 Correct 32 ms 19036 KB Output is correct
31 Correct 5 ms 8792 KB Output is correct
32 Correct 20 ms 10628 KB Output is correct
33 Correct 18 ms 10580 KB Output is correct
34 Correct 17 ms 10912 KB Output is correct
35 Correct 7 ms 8924 KB Output is correct
36 Correct 18 ms 11868 KB Output is correct
37 Correct 22 ms 14428 KB Output is correct
38 Correct 16 ms 11352 KB Output is correct
39 Correct 58 ms 17796 KB Output is correct
40 Correct 74 ms 20756 KB Output is correct
41 Correct 93 ms 28500 KB Output is correct
42 Correct 62 ms 17876 KB Output is correct
43 Correct 65 ms 18516 KB Output is correct
44 Correct 51 ms 16468 KB Output is correct
45 Correct 16 ms 11872 KB Output is correct
46 Correct 52 ms 19828 KB Output is correct
47 Correct 59 ms 23308 KB Output is correct
48 Correct 109 ms 33364 KB Output is correct
49 Correct 52 ms 19920 KB Output is correct
50 Correct 68 ms 21052 KB Output is correct
51 Correct 71 ms 19152 KB Output is correct