제출 #424606

#제출 시각아이디문제언어결과실행 시간메모리
424606blueMagic Tree (CEOI19_magictree)C++17
34 / 100
573 ms1048580 KiB
#include <iostream>
#include <vector>
using namespace std;

/*
Let dp[u][d] be the maximum amount of juice that can be collected if the edge (u, p[u]) is cut on day d.
Let dp'[u][d] = max{d1 <= d | dp[u][d]}

dp[u][d] = (d == fruit_day[u]) * fruit_weight[u]
           + sum{v in children[u] | dp'[v][d]}

dp'[u][d] = max(dp'[u][d-1], dp[u][d])
*/

int main()
{
    int n, m, k;
    cin >> n >> m >> k;

    vector<int> children[n+1];
    for(int i = 2; i <= n; i++)
    {
        int p;
        cin >> p;
        children[p].push_back(i);
    }

    vector<int> fruit_day(n+1, 1);
    vector<long long> fruit_weight(n+1, 0);
    for(int f = 1; f <= m; f++)
    {
        int v, d, w;
        cin >> v >> d >> w;
        fruit_day[v] = d;
        fruit_weight[v] = w;
    }

    vector< vector<long long> > dp(n+1, vector<long long>(k+1, 0));
    vector< vector<long long> > dp1(n+1, vector<long long>(k+1, 0));
    for(int u = n; u >= 1; u--)
    {
        dp[u][0] = 0;
        dp1[u][0] = 0;
        for(int d = 1; d <= k; d++)
        {
            dp[u][d] = (d == fruit_day[u]) * fruit_weight[u];
            for(int v: children[u])
                dp[u][d] += dp1[v][d];

            dp1[u][d] = max(dp[u][d], dp1[u][d-1]);
        }
    }

    cout << dp1[1][k] << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...