답안 #1083443

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1083443 2024-09-03T07:23:20 Z LucasLe Magic Tree (CEOI19_magictree) C++17
100 / 100
93 ms 35156 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);
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7464 KB Output is correct
4 Correct 3 ms 7512 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7492 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7260 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 13652 KB Output is correct
2 Correct 29 ms 19844 KB Output is correct
3 Correct 93 ms 17236 KB Output is correct
4 Correct 50 ms 15564 KB Output is correct
5 Correct 78 ms 18004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7516 KB Output is correct
2 Correct 4 ms 7516 KB Output is correct
3 Correct 3 ms 7512 KB Output is correct
4 Correct 40 ms 31180 KB Output is correct
5 Correct 59 ms 35156 KB Output is correct
6 Correct 49 ms 31352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 12808 KB Output is correct
2 Correct 42 ms 12948 KB Output is correct
3 Correct 34 ms 20564 KB Output is correct
4 Correct 25 ms 11728 KB Output is correct
5 Correct 39 ms 31312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7464 KB Output is correct
4 Correct 3 ms 7512 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7492 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7260 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 40 ms 12376 KB Output is correct
11 Correct 38 ms 12116 KB Output is correct
12 Correct 40 ms 19764 KB Output is correct
13 Correct 24 ms 10968 KB Output is correct
14 Correct 38 ms 30880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 8284 KB Output is correct
2 Correct 21 ms 11356 KB Output is correct
3 Correct 18 ms 11356 KB Output is correct
4 Correct 21 ms 11544 KB Output is correct
5 Correct 8 ms 10200 KB Output is correct
6 Correct 18 ms 14684 KB Output is correct
7 Correct 23 ms 18892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7464 KB Output is correct
4 Correct 3 ms 7512 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7492 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7260 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 4 ms 7516 KB Output is correct
11 Correct 4 ms 7516 KB Output is correct
12 Correct 3 ms 7512 KB Output is correct
13 Correct 40 ms 31180 KB Output is correct
14 Correct 59 ms 35156 KB Output is correct
15 Correct 49 ms 31352 KB Output is correct
16 Correct 40 ms 12376 KB Output is correct
17 Correct 38 ms 12116 KB Output is correct
18 Correct 40 ms 19764 KB Output is correct
19 Correct 24 ms 10968 KB Output is correct
20 Correct 38 ms 30880 KB Output is correct
21 Correct 13 ms 8792 KB Output is correct
22 Correct 43 ms 12624 KB Output is correct
23 Correct 52 ms 14164 KB Output is correct
24 Correct 77 ms 15956 KB Output is correct
25 Correct 43 ms 14796 KB Output is correct
26 Correct 64 ms 19796 KB Output is correct
27 Correct 59 ms 21836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7464 KB Output is correct
4 Correct 3 ms 7512 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7492 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7260 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 38 ms 13652 KB Output is correct
11 Correct 29 ms 19844 KB Output is correct
12 Correct 93 ms 17236 KB Output is correct
13 Correct 50 ms 15564 KB Output is correct
14 Correct 78 ms 18004 KB Output is correct
15 Correct 4 ms 7516 KB Output is correct
16 Correct 4 ms 7516 KB Output is correct
17 Correct 3 ms 7512 KB Output is correct
18 Correct 40 ms 31180 KB Output is correct
19 Correct 59 ms 35156 KB Output is correct
20 Correct 49 ms 31352 KB Output is correct
21 Correct 36 ms 12808 KB Output is correct
22 Correct 42 ms 12948 KB Output is correct
23 Correct 34 ms 20564 KB Output is correct
24 Correct 25 ms 11728 KB Output is correct
25 Correct 39 ms 31312 KB Output is correct
26 Correct 40 ms 12376 KB Output is correct
27 Correct 38 ms 12116 KB Output is correct
28 Correct 40 ms 19764 KB Output is correct
29 Correct 24 ms 10968 KB Output is correct
30 Correct 38 ms 30880 KB Output is correct
31 Correct 7 ms 8284 KB Output is correct
32 Correct 21 ms 11356 KB Output is correct
33 Correct 18 ms 11356 KB Output is correct
34 Correct 21 ms 11544 KB Output is correct
35 Correct 8 ms 10200 KB Output is correct
36 Correct 18 ms 14684 KB Output is correct
37 Correct 23 ms 18892 KB Output is correct
38 Correct 13 ms 8792 KB Output is correct
39 Correct 43 ms 12624 KB Output is correct
40 Correct 52 ms 14164 KB Output is correct
41 Correct 77 ms 15956 KB Output is correct
42 Correct 43 ms 14796 KB Output is correct
43 Correct 64 ms 19796 KB Output is correct
44 Correct 59 ms 21836 KB Output is correct
45 Correct 15 ms 8796 KB Output is correct
46 Correct 45 ms 12784 KB Output is correct
47 Correct 49 ms 14420 KB Output is correct
48 Correct 88 ms 16916 KB Output is correct
49 Correct 55 ms 15568 KB Output is correct
50 Correct 64 ms 20868 KB Output is correct
51 Correct 58 ms 22644 KB Output is correct