Submission #659872

# Submission time Handle Problem Language Result Execution time Memory
659872 2022-11-19T15:40:17 Z 600Mihnea Magic Tree (CEOI19_magictree) C++17
12 / 100
2000 ms 20808 KB
bool home = 0;
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = (int) 1e5 + 7;
int n;
int m;
int k;
int p[N];
int type[N];
int w[N];
int sub[N];
int bg[N];
ll delta[N];
vector<int> g[N];

void bu(int a) {
  sub[a] = 1;
  for (auto &b : g[a]) {
    bu(b);
    sub[a] += sub[b];
    if (sub[b] > sub[bg[a]]) {
      bg[a] = b;
    }
  }
}

vector<pair<int, ll>> get_and_clr_dp() {
  vector<pair<int, ll>> sol;
  for (int i = 1; i <= k; i++) {
    if (delta[i]) {
      sol.push_back({i, delta[i]});
      delta[i] = 0;
    }
  }
  return sol;
}

ll dpaux[N];

void solve(int a) {
  if (g[a].empty()) {
    if (type[a]) {
      delta[type[a]] += w[a];
    }
  } else {
    assert(bg[a] != -1);
    vector<vector<pair<int, ll>>> all;
    for (auto &b : g[a]) {
      if (b != bg[a]) {
        solve(b);
        all.push_back(get_and_clr_dp());
        continue;
      }
    }
    {
      int b = bg[a];
      solve(b);
      for (auto &v : all) {
        for (auto &g : v) {
          delta[g.first] += g.second;
        }
      }
      if (type[a]) {
        for (int i = 1; i <= k; i++) {
          dpaux[i] = dpaux[i - 1] + delta[i];
        }
        dpaux[type[a]] += w[a];
        for (int i = 1; i <= k; i++) {
          delta[i] = max(0LL, dpaux[i] - dpaux[i - 1]);
        }
      }
    }
  }
//  cout << a << " : ";
  ll sol = 0;
  for (int i = 1; i <= k; i++) {
    sol += delta[i];
  //  cout << sol << " ";
  }
 // cout << "\n";
}

int main() {
  if (!home) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  } else {
    freopen("input.txt", "r", stdin);
  }


  cin >> n >> m >> k;
  if (0) {
    cout << "  : ";
    for (int i = 1; i <= k; i++) {
      cout << i << " ";
    }
    cout << "\n";
  }
  for (int i = 2; i <= n; i++) {
    cin >> p[i];
    g[p[i]].push_back(i);
  }
  for (int i = 1; i <= m; i++) {
    int v;
    cin >> v;
    cin >> type[v];
    cin >> w[v];
  }
  bu(1);
  solve(1);
  ll sol = 0;
  for (int i = 1; i <= k; i++) {
    sol += delta[i];
  }
  cout << sol << "\n";
  return 0;
}
/**

dp[vertex][last_value] = ?



**/

Compilation message

magictree.cpp: In function 'int main()':
magictree.cpp:91:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2086 ms 8356 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 52 ms 8336 KB Output is correct
2 Correct 53 ms 8256 KB Output is correct
3 Correct 56 ms 14128 KB Output is correct
4 Correct 43 ms 11452 KB Output is correct
5 Correct 45 ms 20808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 327 ms 4468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -