CF1193B.Magic Tree

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

We have a magic tree: a rooted tree on nn vertices. The vertices are numbered 11 through nn. Vertex 11 is the root.

The magic tree gives us magic fruit. The fruit only grows in vertices of the tree other than the root. Each vertex contains at most one piece of fruit.

It is now day 0 and no fruit is ripe yet. Each fruit will only be ripe for a single day. For each fruit, we are given the vertex vjv_j where it grows, the day djd_j on which it will be ripe, and the amount wjw_j of magic juice we can extract from it if we harvest it when it is ripe.

The fruits have to be harvested by cutting some branches of the tree. On each day, you may cut as many branches of the tree as you like. The parts of the tree you cut off will fall to the ground and you can collect all the ripe fruits they contain. All fruits that fall to the ground when they are not ripe are discarded and no magic juice is collected from them.

Formally, on each day, you may erase some edges of the tree. Whenever you do so, the tree will split into multiple connected components. You then erase all components that do not contain the root and you harvest all ripe fruits those components contained.

Given is a description of the tree together with the locations, ripening days and juiciness of all mm fruits. Calculate the maximum total amount of magic juice we can harvest from the tree.

我们有一棵“魔法树”:一棵包含 nn 个顶点的有根树。顶点编号为 11 到 nn,其中顶点 11 是树根。

这棵魔法树会结出魔法果实。果实只生长在除根以外的顶点上,且每个顶点至多结一颗果实。

当前是第 00 天,所有果实均未成熟。每颗果实仅在某一天成熟(即仅在该天可被采摘)。对于第 jj 颗果实,已知其生长位置(顶点)vjv_j、成熟日期(天数)djd_j,以及若在其成熟当天采摘所能获得的魔法果汁量 wjw_j。

采摘果实需通过砍断树的某些边来实现。每天你可以砍断任意数量的边。被砍断后脱落至地面的部分所含的所有成熟果实均可被收集;但若果实随枝条一起掉落时尚未成熟,则该果实被丢弃,无法获得任何果汁。

形式化地讲:每天你可以删除树中的若干条边。执行此操作后,树将分裂成多个连通分量。随后你删去所有不包含根节点的连通分量,并收获这些被删去分量中所有已成熟的果实。

现给出该树的结构描述,以及全部 mm 颗果实的生长位置、成熟日期和果汁量。请计算我们能从该树中收获的魔法果汁总量的最大值。

输入格式

The first line contains three space-separated integers nn (2≤n≤100,0002 \le n \le 100,000), mm (1≤m≤n−11 \le m \le n-1) and kk (1≤k≤100,0001 \le k \le 100,000) – the number of vertices, the number of fruits, and the maximum day on which a fruit may become ripe.

The following n−1n-1 lines contain the integers p2,…,pnp_2, \dots, p_n, one per line. For each ii (from 22 to nn, inclusive), vertex pip_i (1≤pi≤i−11 \leq p_i \le i-1) is the parent of vertex ii.

Each of the last mm lines describes one fruit. The jj-th of these lines has the form "vj dj wjv_j\ d_j\ w_j" (2≤vj≤n2 \le v_j \le n, 1≤dj≤k1 \le d_j \le k, 1≤wj≤1091 \le w_j \le 10^9).

It is guaranteed that no vertex contains more than one fruit (i.e., the values vjv_j are distinct).

第一行包含三个以空格分隔的整数 nn(2≤n≤100,0002 \le n \le 100{,}000)、mm(1≤m≤n−11 \le m \le n-1)和 kk(1≤k≤100,0001 \le k \le 100{,}000),分别表示顶点数、果实数以及果实可能成熟的最晚天数。

接下来的 n−1n-1 行每行包含一个整数 p2,…,pnp_2, \dots, p_n。对于每个 ii(从 22 到 nn,含端点),顶点 pip_i(1≤pi≤i−11 \leq p_i \le i-1)是顶点 ii 的父节点。

最后 mm 行中的每一行描述一个果实。其中第 jj 行的形式为 "vj dj wjv_j\ d_j\ w_j"(2≤vj≤n2 \le v_j \le n,1≤dj≤k1 \le d_j \le k,1≤wj≤1091 \le w_j \le 10^9)。

保证任意顶点至多包含一个果实(即所有 vjv_j 的值互不相同)。

输出格式

Output a single line with a single integer, the maximum amount of magic juice we can harvest from the tree.

输出一行,包含一个整数,表示我们能从树上收获的最大魔法果汁量。

输入输出样例

  • 输入#1

    6 4 10
    1
    2
    1
    4
    4
    3 4 5
    4 7 2
    5 4 1
    6 9 3

    输出#1

    9

说明/提示

In the example input, one optimal solution looks as follows:

  • On day 4, cut the edge between vertices 4 and 5 and harvest a ripe fruit with 1 unit of magic juice. On the same day, cut the edge between vertices 1 and 2 and harvest 5 units of magic juice from the ripe fruit in vertex 3.
  • On day 7, do nothing. (We could harvest the fruit in vertex 4 that just became ripe, but doing so is not optimal.)
  • On day 9, cut the edge between vertices 1 and 4. Discard the fruit in vertex 4 that is no longer ripe, and harvest 3 units of magic juice from the ripe fruit in vertex 6. (Alternately, we could achieve the same effect by cutting the edge between vertices 4 and 6.)

在样例输入中,一个最优解如下所示:

  • 第 4 天:切断顶点 4 与顶点 5 之间的边,并采摘一颗已成熟的果实,获得 1 单位魔力果汁;同一天,切断顶点 1 与顶点 2 之间的边,并从顶点 3 处已成熟的果实中收获 5 单位魔力果汁。
  • 第 7 天:不进行任何操作。(此时顶点 4 的果实恰好成熟,我们本可采摘它,但这样做并非最优。)
  • 第 9 天:切断顶点 1 与顶点 4 之间的边;丢弃顶点 4 处已不再成熟的果实,并从顶点 6 处已成熟的果实中收获 3 单位魔力果汁。(或者,我们也可通过切断顶点 4 与顶点 6 之间的边来达到相同效果。)

输入解题思路,AI测评打分。不知道怎么写?

首页