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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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≤ai≤109) — the numbers composed on the vertices. 

 

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.

 

 

Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Minimum Spanning Tree
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education