Submission #730841

# Submission time Handle Problem Language Result Execution time Memory
730841 2023-04-26T14:03:08 Z MilosMilutinovic Magic Tree (CEOI19_magictree) C++14
0 / 100
2000 ms 17596 KB
/**
 *    author:  wxhtzdy
 *    created: 26.04.2023 14:44:59
**/
#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);
    g[i].push_back(p);
  }
  vector<int> a(n, -1), b(n);
  for (int i = 0; i < m; i++) {
    int v, d, w;
    cin >> v >> d >> w;
    --v; --d;
    a[v] = d;
    b[v] = w;    
  }
  vector<int> rt(n);
  vector<vector<pair<int, int>>> vals(n);
  function<void(int, int)> Dfs = [&](int v, int pv) {
    int f = -1;
    for (int u : g[v]) {
      if (u == pv) {
        continue;
      }
      Dfs(u, v);
      if (f == -1 || vals[rt[f]].size() < vals[rt[u]].size()) {
        f = u;  
      }
    }
    if (f == -1) {
      rt[v] = v;
      if (a[v] != -1) {
        vals[v].emplace_back(a[v], b[v]);
      }
      return;  
    }
    rt[v] = rt[f];
    for (int u : g[v]) {
      if (u == f || u == pv) {
        continue;
      }
      for (auto& p : vals[rt[u]]) {
        vals[rt[v]].push_back(p);
      }
    }
    sort(vals[rt[v]].begin(), vals[rt[v]].end());
    if (a[v] != -1) {
      vals[rt[v]].emplace_back(a[v], b[v]);
      for (auto& p : vals[rt[v]]) {
        if (p.first > a[v]) {
          vals[rt[v]].emplace_back(p.first, -b[v]);
          break;
        }
      }
    }
  };
  Dfs(0, 0);
  sort(vals[rt[0]].begin(), vals[rt[0]].end());
  long long ans = 0;
  long long sum = 0;
  for (auto& p : vals[rt[0]]) {
    sum += p.second;
    ans = max(ans, sum);
  }
  cout << ans << '\n';                 
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 65 ms 12352 KB Output is correct
2 Execution timed out 2074 ms 15208 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 21 ms 452 KB Output is correct
2 Incorrect 15 ms 544 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 126 ms 17596 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 2260 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -