Problem 3017 --Salty Fish

3017: Salty Fish

"
Time Limit $4$ 秒/Second(s) Memory Limit $512$ 兆字节/Megabyte(s)
提交总数 $0$ 正确数量 $0$
裁判形式 标准裁判/Standard Judge 我的状态 尚未尝试
难度 分类标签
Little Q has an apple tree with nn nodes, labeled by 1,2,…,n1,2,…,n. The root of the tree is the 11-th node and the length of each edge is one unit. There are aiai apples on the ii-th node. The price of each apple is one dollar, so if you sell tt apples, you will gain tt dollars. 

Skywalkert, a close friend of Little Q, lost most of his money betting on programming contests, so he wants to steal some apples from this apple tree and sell them to make money. 

The security system takes pictures of the nodes once per hour using mm cameras. Let's denote d(x,y)d(x,y) as the number of edges on the shortest path from the xx-th node to the yy-th node, and denote set p(x,k)p(x,k) as {y|y in x's subtree and d(x,y)≤k}{y|y in x's subtree and d(x,y)≤k}. Note that x∈p(x,k)x∈p(x,k). The image from the ii-th camera shows the picture of all the nodes in p(xi,ki)p(xi,ki). If the security system detects a change in any of these images, it sounds an alarm, the thief will be caught by Little Q. 

Skywalkert is also a gifted hacker. He can lock some cameras so that images from these cameras will never change. Specifically, if he want to lock the ii-th camera, he needs to pay cici dollars to do such a hack. 

Please write a program to help Skywalkert make money optimally without being caught.
The first line of the input contains an integer T(1≤T≤10000)T(1≤T≤10000), denoting the number of test cases. 

In each test case, there are two integers n,m(1≤n,m≤300000)n,m(1≤n,m≤300000) in the first line, denoting the number of nodes and cameras. 

In the second line, there are n−1n−1 integers f2,f3,...,fn(1≤fi<i)f2,f3,...,fn(1≤fi<i), denoting the father of each node. 

In the third line, there are nn integers a1,a2,...,an(1≤ai≤109)a1,a2,...,an(1≤ai≤109), denoting the number of apples on each node. 

For the next mm lines, each line contains three integers xi,ki,ci(1≤xi≤n,0≤ki≤n,1≤ci≤109)xi,ki,ci(1≤xi≤n,0≤ki≤n,1≤ci≤109), denoting each camera. 

It is guaranteed that ∑n≤106∑n≤106 and ∑m≤106∑m≤106.
For each test case, print a single line containing an integer, denoting the maximum amount of dollars Skywalkert can earn.
1
6 3
1 1 2 2 3
2 5 4 3 3 2
2 1 3
3 1 7
1 2 4
6

推荐代码 查看3017 所有题解 上传题解视频得图灵币

本题记录 用 户(点击查看用户) 运行号(点击购买题解) 时 间
算法最快[$ $ms]
内存最少[$ $KB]
第一AC
第一挑战

赛题来源/所属竞赛 2019 Multi-University Training Contest 6 N/A

竞赛编号 竞赛名称 竞赛时间 访问比赛