given n−1 integers a2,… ,an and a tree with n vertices established at vertex 1. The leaves are all at a similar distance d from the root. Review that a tree is an associated undirected chart without cycles. The distance between two vertices is the number of edges on the straightforward way between them. All non-root vertices with degree 1 are leaves. In the event that vertices
Correct answer will be upvoted else downvoted. Computer science.
You are given n−1 integers a2,… ,an and a tree with n vertices established at vertex 1. The leaves are all at a similar distance d from the root.
Review that a tree is an associated undirected chart without cycles. The distance between two vertices is the number of edges on the straightforward way between them. All non-root vertices with degree 1 are leaves. In the event that vertices s and f are associated by an edge and the distance of f from the root is more noteworthy than the distance of s from the root, then, at that point, f is known as an offspring of s.
At first, there are a red coin and a blue coin on the vertex 1. Leave r alone the vertex where the red coin is and left b alone the vertex where the blue coin is. You should take d actions. A move comprises of three stages:
Move the red coin to any offspring of r.
Move the blue coin to any vertex b′ with the end goal that dist(1,b′)=dist(1,b)+1. Here dist(x,y) shows the length of the straightforward way among x and y. Note that b and b′ are not really associated by an edge.
You can alternatively trade the two coins (or skirt this progression).
Note that r and b can be equivalent whenever, and there could be no number composed on the root.
After each move, you gain |ar−ab| focuses. What's the most extreme number of focuses you can acquire after d moves?
Input
The primary line contains a solitary integer t (1≤t≤104) — the number of experiments.
The principal line of each experiment contains a solitary integer n (2≤n≤2⋅105) — the number of vertices in the tree.
The second line of each experiment contains n−1 integers v2,v3,… ,vn (1≤vi≤n, vi≠i) — the I-th of them shows that there is an edge between vertices I and vi. It is ensured, that these edges structure a tree.
The third line of each experiment contains n−1 integers a2,… ,an (1≤
It is ensured that the amount of n for all experiments doesn't surpass 2⋅105.
Output
For each experiment, print a solitary integer: the most extreme number of focuses you can acquire after d moves.
Step by step
Solved in 4 steps with 1 images