Submission #834060

# Submission time Handle Problem Language Result Execution time Memory
834060 2023-08-22T10:27:21 Z MilosMilutinovic Magic Tree (CEOI19_magictree) C++14
100 / 100
121 ms 34428 KB
/**
 *    author:  wxhtzdy
 *    created: 22.08.2023 12:12:51
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n, m, k;
  cin >> n >> m >> k;
  vector<vector<int>> g(n);
  for (int i = 1; i < n; i++) {
    int p;
    cin >> p;
    --p;
    g[p].push_back(i);
  }
  vector<int> x(n, -1), y(n);
  for (int i = 0; i < m; i++) {
    int v, d, w;
    cin >> v >> d >> w;
    --v;
    x[v] = d;
    y[v] = w;    
  }
  vector<set<pair<int, long long>>> dp(n);
  function<void(int)> Dfs = [&](int v) {
    int ch = -1;
    for (int u : g[v]) {
      Dfs(u);
      if (ch == -1 || (int) dp[u].size() > (int) dp[ch].size()) {
        ch = u;
      }
    }     
    if (ch != -1) {
      swap(dp[v], dp[ch]);
    }
    for (int u : g[v]) {
      if (u == ch) {
        continue;
      }            
      for (auto& p : dp[u]) {
        auto it = dp[v].lower_bound({p.first, -1LL});
        if (it != dp[v].end() && it->first == p.first) {
          int fir = it->first;
          long long sec = it->second;
          dp[v].erase(it);
          dp[v].emplace(fir, sec + p.second);                    
        } else {
          dp[v].insert(p);
        }
      }
    }
    if (x[v] != -1) {
      auto it = dp[v].lower_bound({x[v], -1LL});
      if (it != dp[v].end() && it->first == x[v]) {
        int fir = it->first;
        long long sec = it->second;
        dp[v].erase(it);
        dp[v].emplace(fir, sec + y[v]);
      } else {
        dp[v].emplace(x[v], y[v]); 
      }
      long long s = 0;
      while (true) {
        auto it = dp[v].lower_bound({x[v] + 1, -1LL});
        if (it == dp[v].end()) {
          break;
        }
        if (s + it->second <= y[v]) {
          s += it->second;
          dp[v].erase(it);
        } else {
          int fir = it->first;
          long long sec = it->second;
          dp[v].erase(it);
          dp[v].emplace(fir, sec - (y[v] - s));
          break;
        }
      }
    }
  };
  Dfs(0);
  long long ans = 0;
  for (auto& p : dp[0]) {
    ans += p.second;
  }
  cout << ans << '\n';                
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 17256 KB Output is correct
2 Correct 36 ms 19788 KB Output is correct
3 Correct 119 ms 34428 KB Output is correct
4 Correct 63 ms 19072 KB Output is correct
5 Correct 98 ms 21048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 46 ms 26812 KB Output is correct
5 Correct 64 ms 30848 KB Output is correct
6 Correct 71 ms 27076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 13396 KB Output is correct
2 Correct 43 ms 12620 KB Output is correct
3 Correct 45 ms 18032 KB Output is correct
4 Correct 33 ms 14092 KB Output is correct
5 Correct 47 ms 26876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 62 ms 16176 KB Output is correct
11 Correct 55 ms 15128 KB Output is correct
12 Correct 52 ms 17960 KB Output is correct
13 Correct 34 ms 14092 KB Output is correct
14 Correct 49 ms 26880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2388 KB Output is correct
2 Correct 28 ms 9888 KB Output is correct
3 Correct 24 ms 9840 KB Output is correct
4 Correct 23 ms 9916 KB Output is correct
5 Correct 9 ms 8616 KB Output is correct
6 Correct 22 ms 12628 KB Output is correct
7 Correct 22 ms 16944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 46 ms 26812 KB Output is correct
14 Correct 64 ms 30848 KB Output is correct
15 Correct 71 ms 27076 KB Output is correct
16 Correct 62 ms 16176 KB Output is correct
17 Correct 55 ms 15128 KB Output is correct
18 Correct 52 ms 17960 KB Output is correct
19 Correct 34 ms 14092 KB Output is correct
20 Correct 49 ms 26880 KB Output is correct
21 Correct 15 ms 4872 KB Output is correct
22 Correct 63 ms 17520 KB Output is correct
23 Correct 68 ms 20780 KB Output is correct
24 Correct 116 ms 28488 KB Output is correct
25 Correct 60 ms 18068 KB Output is correct
26 Correct 105 ms 20044 KB Output is correct
27 Correct 79 ms 19716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 53 ms 17256 KB Output is correct
11 Correct 36 ms 19788 KB Output is correct
12 Correct 119 ms 34428 KB Output is correct
13 Correct 63 ms 19072 KB Output is correct
14 Correct 98 ms 21048 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 46 ms 26812 KB Output is correct
19 Correct 64 ms 30848 KB Output is correct
20 Correct 71 ms 27076 KB Output is correct
21 Correct 50 ms 13396 KB Output is correct
22 Correct 43 ms 12620 KB Output is correct
23 Correct 45 ms 18032 KB Output is correct
24 Correct 33 ms 14092 KB Output is correct
25 Correct 47 ms 26876 KB Output is correct
26 Correct 62 ms 16176 KB Output is correct
27 Correct 55 ms 15128 KB Output is correct
28 Correct 52 ms 17960 KB Output is correct
29 Correct 34 ms 14092 KB Output is correct
30 Correct 49 ms 26880 KB Output is correct
31 Correct 4 ms 2388 KB Output is correct
32 Correct 28 ms 9888 KB Output is correct
33 Correct 24 ms 9840 KB Output is correct
34 Correct 23 ms 9916 KB Output is correct
35 Correct 9 ms 8616 KB Output is correct
36 Correct 22 ms 12628 KB Output is correct
37 Correct 22 ms 16944 KB Output is correct
38 Correct 15 ms 4872 KB Output is correct
39 Correct 63 ms 17520 KB Output is correct
40 Correct 68 ms 20780 KB Output is correct
41 Correct 116 ms 28488 KB Output is correct
42 Correct 60 ms 18068 KB Output is correct
43 Correct 105 ms 20044 KB Output is correct
44 Correct 79 ms 19716 KB Output is correct
45 Correct 17 ms 5460 KB Output is correct
46 Correct 70 ms 19440 KB Output is correct
47 Correct 85 ms 22760 KB Output is correct
48 Correct 121 ms 31872 KB Output is correct
49 Correct 62 ms 19204 KB Output is correct
50 Correct 109 ms 21548 KB Output is correct
51 Correct 90 ms 21044 KB Output is correct