답안 #659878

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
659878 2022-11-19T15:47:43 Z 600Mihnea Magic Tree (CEOI19_magictree) C++17
47 / 100
2000 ms 22364 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]) {
        delta[type[a]] += w[a];
        delta[type[a] + 1] -= w[a];
        ll mx = 0, cur = 0;
        for (int i = 1; i <= k; i++) {
          cur += delta[i];
          delta[i] = max(mx, cur) - mx;
          mx = max(mx, cur);
        }
      }
    }
  }
//  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];
  }
  {
    map<int, int> mp;
    for (int i = 1; i <= n; i++) {
      if (type[i]) {
        mp[type[i]] = 0;
      }
    }
    int cr = 0;
    for (auto &it : mp) {
      it.second = ++cr;
    }
    for (int i = 1; i <= n; i++) {
      if (type[i]) {
        type[i] = mp[type[i]];
      }
    }
    k = min(k, m);
  }
  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:93:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2704 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 614 ms 7268 KB Output is correct
2 Correct 190 ms 12756 KB Output is correct
3 Execution timed out 2055 ms 10960 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Execution timed out 2088 ms 22364 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 6248 KB Output is correct
2 Correct 52 ms 6228 KB Output is correct
3 Correct 47 ms 12044 KB Output is correct
4 Correct 37 ms 9748 KB Output is correct
5 Correct 42 ms 18644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2704 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 59 ms 6220 KB Output is correct
11 Correct 51 ms 6252 KB Output is correct
12 Correct 47 ms 11992 KB Output is correct
13 Correct 37 ms 9692 KB Output is correct
14 Correct 47 ms 18620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 3412 KB Output is correct
2 Correct 44 ms 6292 KB Output is correct
3 Correct 45 ms 6288 KB Output is correct
4 Correct 49 ms 6416 KB Output is correct
5 Correct 53 ms 8292 KB Output is correct
6 Correct 57 ms 9744 KB Output is correct
7 Correct 35 ms 12368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2704 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Execution timed out 2088 ms 22364 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2704 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 614 ms 7268 KB Output is correct
11 Correct 190 ms 12756 KB Output is correct
12 Execution timed out 2055 ms 10960 KB Time limit exceeded
13 Halted 0 ms 0 KB -