Submission #1083444

# Submission time Handle Problem Language Result Execution time Memory
1083444 2024-09-03T07:24:10 Z vjudge1 Magic Tree (CEOI19_magictree) C++17
100 / 100
81 ms 34616 KB
#include <bits/stdc++.h>


#define                int  long long
#define                pii  pair<int, int>
#define                 fi  first
#define                 se  second
#define                 ld  long double
#define                 vi  vector<int>
#define                vii  vector<vector<int>>
#define             all(v)  (v).begin(), (v).end()
#define       rep(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i)
#define       per(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i)

using namespace std;

const int MOD = 1e9 + 7;

int add(int a, int b) {
  a += b;
  if (a >= MOD) a -= MOD;
  return a;
}

int mul(int a, int b) {
  (a *= b) %= MOD;
  return a;
}

int bin_pow(int x, int y) {
  int res=1;
  while(y){if(y&1)res=res*x%MOD;x=x*x%MOD;y>>=1;}
  return res;
}

const int INF = 1e17;
const int maxn = 1e5 + 5;

int N, M, K;
int day[maxn + 5], weight[maxn + 5];
map<int, int> f[maxn + 5];
vector<int> g[maxn + 5];

void dfs(int u) {
  for (int v : g[u]) {
    dfs(v);
    if (f[v].size() > f[u].size()) swap(f[v], f[u]);
    for (auto it : f[v]) f[u][it.first] += it.second;
    f[v].clear();
  }
  if (day[u] && weight[u]) {
    int s = 0;
    while (true) {
      auto it = f[u].upper_bound(day[u]);
      if (it == f[u].end()) break;
      if (it->second + s <= weight[u]) {
        s += it->second;
        f[u].erase(it);
      } else {
        it->second += s - weight[u];
        break;
      }
    }
    f[u][day[u]] += weight[u];
  }
}

void solve(int tc) {
  // trong 1 subtree, neu chon dinh thi nhung dinh a[v] > a[u] ko chon
  // con ko chon dinh u thi chon cac dinh a[v] > a[u]
  
  cin >> N >> M >> K;
  rep(i, 2, N) {
    int p; cin >> p;
    g[p].push_back(i);
  }
  rep(i, 1, M) {
    int v, d, w; cin >> v >> d >> w;
    day[v] = d;
    weight[v] = w;
  }
  dfs(1);
  int ans = 0;
  for (auto it : f[1]) {
    ans += it.second;
  }
  cout << ans << '\n';
}

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);

  int tc = 1;
  // cin >> tc;
  for (int i = 1; i <= tc; ++i) {
    solve(i);
  }
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7516 KB Output is correct
2 Correct 3 ms 7348 KB Output is correct
3 Correct 3 ms 7396 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 4 ms 7260 KB Output is correct
6 Correct 5 ms 7516 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 13396 KB Output is correct
2 Correct 32 ms 19536 KB Output is correct
3 Correct 81 ms 16468 KB Output is correct
4 Correct 63 ms 14796 KB Output is correct
5 Correct 71 ms 17240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7512 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7512 KB Output is correct
4 Correct 39 ms 30620 KB Output is correct
5 Correct 57 ms 34616 KB Output is correct
6 Correct 52 ms 30544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 12196 KB Output is correct
2 Correct 37 ms 12120 KB Output is correct
3 Correct 49 ms 19536 KB Output is correct
4 Correct 26 ms 10964 KB Output is correct
5 Correct 37 ms 30548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7516 KB Output is correct
2 Correct 3 ms 7348 KB Output is correct
3 Correct 3 ms 7396 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 4 ms 7260 KB Output is correct
6 Correct 5 ms 7516 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 41 ms 11868 KB Output is correct
11 Correct 40 ms 11864 KB Output is correct
12 Correct 33 ms 19280 KB Output is correct
13 Correct 23 ms 10712 KB Output is correct
14 Correct 37 ms 30300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8280 KB Output is correct
2 Correct 20 ms 11356 KB Output is correct
3 Correct 19 ms 11436 KB Output is correct
4 Correct 23 ms 11356 KB Output is correct
5 Correct 13 ms 10196 KB Output is correct
6 Correct 19 ms 14684 KB Output is correct
7 Correct 20 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7516 KB Output is correct
2 Correct 3 ms 7348 KB Output is correct
3 Correct 3 ms 7396 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 4 ms 7260 KB Output is correct
6 Correct 5 ms 7516 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 5 ms 7512 KB Output is correct
11 Correct 3 ms 7516 KB Output is correct
12 Correct 3 ms 7512 KB Output is correct
13 Correct 39 ms 30620 KB Output is correct
14 Correct 57 ms 34616 KB Output is correct
15 Correct 52 ms 30544 KB Output is correct
16 Correct 41 ms 11868 KB Output is correct
17 Correct 40 ms 11864 KB Output is correct
18 Correct 33 ms 19280 KB Output is correct
19 Correct 23 ms 10712 KB Output is correct
20 Correct 37 ms 30300 KB Output is correct
21 Correct 13 ms 8796 KB Output is correct
22 Correct 39 ms 12040 KB Output is correct
23 Correct 49 ms 13028 KB Output is correct
24 Correct 74 ms 14164 KB Output is correct
25 Correct 45 ms 13512 KB Output is correct
26 Correct 64 ms 18000 KB Output is correct
27 Correct 62 ms 20052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7516 KB Output is correct
2 Correct 3 ms 7348 KB Output is correct
3 Correct 3 ms 7396 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 4 ms 7260 KB Output is correct
6 Correct 5 ms 7516 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 39 ms 13396 KB Output is correct
11 Correct 32 ms 19536 KB Output is correct
12 Correct 81 ms 16468 KB Output is correct
13 Correct 63 ms 14796 KB Output is correct
14 Correct 71 ms 17240 KB Output is correct
15 Correct 5 ms 7512 KB Output is correct
16 Correct 3 ms 7516 KB Output is correct
17 Correct 3 ms 7512 KB Output is correct
18 Correct 39 ms 30620 KB Output is correct
19 Correct 57 ms 34616 KB Output is correct
20 Correct 52 ms 30544 KB Output is correct
21 Correct 47 ms 12196 KB Output is correct
22 Correct 37 ms 12120 KB Output is correct
23 Correct 49 ms 19536 KB Output is correct
24 Correct 26 ms 10964 KB Output is correct
25 Correct 37 ms 30548 KB Output is correct
26 Correct 41 ms 11868 KB Output is correct
27 Correct 40 ms 11864 KB Output is correct
28 Correct 33 ms 19280 KB Output is correct
29 Correct 23 ms 10712 KB Output is correct
30 Correct 37 ms 30300 KB Output is correct
31 Correct 6 ms 8280 KB Output is correct
32 Correct 20 ms 11356 KB Output is correct
33 Correct 19 ms 11436 KB Output is correct
34 Correct 23 ms 11356 KB Output is correct
35 Correct 13 ms 10196 KB Output is correct
36 Correct 19 ms 14684 KB Output is correct
37 Correct 20 ms 19036 KB Output is correct
38 Correct 13 ms 8796 KB Output is correct
39 Correct 39 ms 12040 KB Output is correct
40 Correct 49 ms 13028 KB Output is correct
41 Correct 74 ms 14164 KB Output is correct
42 Correct 45 ms 13512 KB Output is correct
43 Correct 64 ms 18000 KB Output is correct
44 Correct 62 ms 20052 KB Output is correct
45 Correct 14 ms 8280 KB Output is correct
46 Correct 56 ms 11092 KB Output is correct
47 Correct 51 ms 12880 KB Output is correct
48 Correct 76 ms 14416 KB Output is correct
49 Correct 56 ms 13316 KB Output is correct
50 Correct 78 ms 18372 KB Output is correct
51 Correct 57 ms 20048 KB Output is correct