답안 #474246

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
474246 2021-09-17T13:47:07 Z hoanghq2004 Magic Tree (CEOI19_magictree) C++14
100 / 100
141 ms 32680 KB
/*
    Central European Olympiad in Informatics 2019 (Day 2)

    Using dynamic programming: dp[u][t] is the maximum of fruits can be harvested at time point earlier than or equal t
    Formula: dp[u][t] = sum(dp[v][t]) (if t < t[u])
                      = max(sum(dp[v][t]), sum(dp[v][t[u]]) + w[u]) otherwise
    So easy to find that: dp[u][t] is an non-decreasing array with t = 0...k and the number of distinct values will smaller than or equal the number of vertex in the subtree rooted u + 1
and the values only can be increase if t = t[v]
    Optimize:
        - map <int, long long> opt[u] contains all values of dp[u][i] - dp[u][i - 1]
        - merge small to large so the complexity is O(n * log^2(n))
        - for each subtree rooted u, dp[u][t[u]] += w[u], so opt[u][t[u]] += w and the values after t[u] have to be increase that: dp[u][t] = max(dp[u][t], w[u]).
          here, define cur = w[u] - dp[u][t] with t = t[u]...
          while opt[u][t] < w[u], dp[u][t] is smaller than w[u], so we should assign dp[u][t] = w[u]
          subtract opt[u][t] from cur and assign opt[u][t] = 0 (erase from map)
          in other case, dp[u][t] is larger than w[u] then all values from t shouldn't be changed, subtract w[u] from opt[u][t]
*/

#include <bits/stdc++.h>

using namespace std;

int n, m, k;
vector <int> e[100010];
int w[100010], t[100010];
map <int, long long> opt[100010];
long long res;

void dfs(int u) {
    for (auto v: e[u]) {
        dfs(v);
        if (opt[u].size() < opt[v].size()) opt[u].swap(opt[v]);
        for (auto it: opt[v]) opt[u][it.first] += it.second;
        opt[v].clear();
    }
    if (w[u] == 0) return;
    opt[u][t[u]] += w[u];
    auto it = opt[u].upper_bound(t[u]);
    while (it != opt[u].end()) {
        if (w[u] >= it -> second) {
            w[u] -= it -> second;
            opt[u].erase(it++);
        }
        else {
            it -> second -= w[u];
            break;
        }
    }
}

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

    cin >> n >> m >> k;
    for (int i = 2; i <= n; ++i) {
        int p;
        cin >> p;
        e[p].push_back(i);
    }
    while (m--) {
        int u;
        cin >> u;
        cin >> t[u] >> w[u];
    }
    dfs(1);
    for (auto it: opt[1]) res += it.second;
    cout << res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7364 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 4 ms 7364 KB Output is correct
6 Correct 4 ms 7364 KB Output is correct
7 Correct 4 ms 7368 KB Output is correct
8 Correct 4 ms 7372 KB Output is correct
9 Correct 4 ms 7372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 12564 KB Output is correct
2 Correct 48 ms 19008 KB Output is correct
3 Correct 137 ms 15924 KB Output is correct
4 Correct 77 ms 14292 KB Output is correct
5 Correct 107 ms 16548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7504 KB Output is correct
2 Correct 5 ms 7500 KB Output is correct
3 Correct 5 ms 7500 KB Output is correct
4 Correct 62 ms 28836 KB Output is correct
5 Correct 87 ms 32680 KB Output is correct
6 Correct 78 ms 28864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 11844 KB Output is correct
2 Correct 77 ms 11816 KB Output is correct
3 Correct 65 ms 18956 KB Output is correct
4 Correct 47 ms 10376 KB Output is correct
5 Correct 66 ms 29052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7364 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 4 ms 7364 KB Output is correct
6 Correct 4 ms 7364 KB Output is correct
7 Correct 4 ms 7368 KB Output is correct
8 Correct 4 ms 7372 KB Output is correct
9 Correct 4 ms 7372 KB Output is correct
10 Correct 78 ms 11212 KB Output is correct
11 Correct 77 ms 11060 KB Output is correct
12 Correct 61 ms 18372 KB Output is correct
13 Correct 42 ms 9668 KB Output is correct
14 Correct 57 ms 28364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 8024 KB Output is correct
2 Correct 37 ms 10352 KB Output is correct
3 Correct 35 ms 10352 KB Output is correct
4 Correct 34 ms 10352 KB Output is correct
5 Correct 15 ms 8780 KB Output is correct
6 Correct 33 ms 13180 KB Output is correct
7 Correct 34 ms 17456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7364 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 4 ms 7364 KB Output is correct
6 Correct 4 ms 7364 KB Output is correct
7 Correct 4 ms 7368 KB Output is correct
8 Correct 4 ms 7372 KB Output is correct
9 Correct 4 ms 7372 KB Output is correct
10 Correct 5 ms 7504 KB Output is correct
11 Correct 5 ms 7500 KB Output is correct
12 Correct 5 ms 7500 KB Output is correct
13 Correct 62 ms 28836 KB Output is correct
14 Correct 87 ms 32680 KB Output is correct
15 Correct 78 ms 28864 KB Output is correct
16 Correct 78 ms 11212 KB Output is correct
17 Correct 77 ms 11060 KB Output is correct
18 Correct 61 ms 18372 KB Output is correct
19 Correct 42 ms 9668 KB Output is correct
20 Correct 57 ms 28364 KB Output is correct
21 Correct 21 ms 8452 KB Output is correct
22 Correct 73 ms 11232 KB Output is correct
23 Correct 89 ms 13228 KB Output is correct
24 Correct 118 ms 14788 KB Output is correct
25 Correct 70 ms 13524 KB Output is correct
26 Correct 96 ms 18304 KB Output is correct
27 Correct 90 ms 20164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7364 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 4 ms 7364 KB Output is correct
6 Correct 4 ms 7364 KB Output is correct
7 Correct 4 ms 7368 KB Output is correct
8 Correct 4 ms 7372 KB Output is correct
9 Correct 4 ms 7372 KB Output is correct
10 Correct 64 ms 12564 KB Output is correct
11 Correct 48 ms 19008 KB Output is correct
12 Correct 137 ms 15924 KB Output is correct
13 Correct 77 ms 14292 KB Output is correct
14 Correct 107 ms 16548 KB Output is correct
15 Correct 5 ms 7504 KB Output is correct
16 Correct 5 ms 7500 KB Output is correct
17 Correct 5 ms 7500 KB Output is correct
18 Correct 62 ms 28836 KB Output is correct
19 Correct 87 ms 32680 KB Output is correct
20 Correct 78 ms 28864 KB Output is correct
21 Correct 73 ms 11844 KB Output is correct
22 Correct 77 ms 11816 KB Output is correct
23 Correct 65 ms 18956 KB Output is correct
24 Correct 47 ms 10376 KB Output is correct
25 Correct 66 ms 29052 KB Output is correct
26 Correct 78 ms 11212 KB Output is correct
27 Correct 77 ms 11060 KB Output is correct
28 Correct 61 ms 18372 KB Output is correct
29 Correct 42 ms 9668 KB Output is correct
30 Correct 57 ms 28364 KB Output is correct
31 Correct 9 ms 8024 KB Output is correct
32 Correct 37 ms 10352 KB Output is correct
33 Correct 35 ms 10352 KB Output is correct
34 Correct 34 ms 10352 KB Output is correct
35 Correct 15 ms 8780 KB Output is correct
36 Correct 33 ms 13180 KB Output is correct
37 Correct 34 ms 17456 KB Output is correct
38 Correct 21 ms 8452 KB Output is correct
39 Correct 73 ms 11232 KB Output is correct
40 Correct 89 ms 13228 KB Output is correct
41 Correct 118 ms 14788 KB Output is correct
42 Correct 70 ms 13524 KB Output is correct
43 Correct 96 ms 18304 KB Output is correct
44 Correct 90 ms 20164 KB Output is correct
45 Correct 23 ms 8536 KB Output is correct
46 Correct 81 ms 11576 KB Output is correct
47 Correct 89 ms 13380 KB Output is correct
48 Correct 141 ms 15812 KB Output is correct
49 Correct 77 ms 14336 KB Output is correct
50 Correct 105 ms 19268 KB Output is correct
51 Correct 94 ms 21112 KB Output is correct