Submission #775754

#TimeUsernameProblemLanguageResultExecution timeMemory
775754danikoynovMagic Tree (CEOI19_magictree)C++14
100 / 100
231 ms59252 KiB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 1e5 + 10, maxk = 21;
int n, m, k, p[maxn], d[maxn], sub[maxn];
ll w[maxn];
///ll diff[maxn][maxk];
vector < int > adj[maxn];
map < int, ll > dp[maxn];
void dfs(int v)
{
    sub[v] = 1;
    int heavy = -1;
    for (int u : adj[v])
    {
        dfs(u);
        sub[v] += sub[u];
        if (heavy == -1 || sub[u] > sub[heavy])
            heavy = u;

        ///for (int j = 0; j <= k; j ++)
            ///diff[v][j] += diff[u][j];
    }


    if (heavy != -1)
    swap(dp[v], dp[heavy]);
    for (int u : adj[v])
    {
        if (u == heavy)
            continue;
        for (pair < int, ll > cur : dp[u])
        {
            dp[v][cur.first] += cur.second;
        }
    }

    if (d[v] != 0)
    {
        dp[v][d[v] - 1] += w[v];
        dp[v][d[v]] -= w[v];
        map < int, ll > :: iterator it, hp;
        ll cur = d[v];
        while(true)
        {
            it = dp[v].lower_bound(cur);
            if (it -> second >= 0)
                break;
            if (next(it) == dp[v].end())
            {
                dp[v][it -> first] = 0;
                break;
            }

            hp = next(it);
            dp[v][hp -> first] += it -> second;
            dp[v].erase(it);
        }
        /**diff[v][d[v] - 1] += w[v];
        diff[v][d[v]] -= w[v];
        int pt = d[v];
        while(pt < k)
        {
            if (diff[v][pt] < 0)
            {
                diff[v][pt + 1] += diff[v][pt];
                diff[v][pt] = 0;
                pt ++;
            }
            else
            {
                break;
            }
        }*/
    }
    /**for (int j = 0; j < k; j ++)
    {
        cout << diff[v][j] << " ";
    }
    cout << endl;*/
}

void solve()
{
    cin >> n >> m >> k;
    for (int i = 2; i <= n; i ++)
    {
        cin >> p[i];
        adj[p[i]].push_back(i);
    }
    for (int i = 1; i <= m; i ++)
    {
        int v;
        cin >> v >> d[v] >> w[v];
    }

    dfs(1);
    ll ans = 0;
    for (int j = 0; j < k; j ++)
        ans = ans + dp[1][j];
    cout  << ans << endl;
}

int main()
{
    solve();
    return 0;
}
/**
6 4 10
1
2
1
4
4
3 3 4
4 2 5
5 5 1
6 3 9
*/
#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...