Yet Another Tree Problem

 Nayra doesn't like stories of people receiving random trees as birthday presents, but this time she received a tree as a present for her own birthday! After struggling for a day trying to figure out what to do with this tree, she asked Aryan for help. He gave her this problem.

You are given a tree with N vertices and weighted edges. It is rooted at vertex 1.

Define g(u,v) to be the maximum weight of an edge on the shortest path between vertex u and vertex v in this tree.

Just calculating g(u,v) is too easy, so you have to process Q queries.

In each query, you are given K distinct vertices v1,v2,,vK. You have to compute the sum of the g-values of each pair of vertices among these K, i.e, the value

i=1K1j=i+1Kg(vi,vj)

Note: The input and output are large, so use fast input-output methods.

Input Format

  • The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains two space-separated integers N and Q, denoting the number of vertices in the tree and the number of queries respectively.
  • The second line contains N1 space-separated integers P2,P3,,PN, where Pi is the parent of vertex i.
  • The third line contains N1 space-separated integers W2,W3,,WN, where Wi is the weight of the edge between vertices i and Pi.
  • The next Q lines contain queries you will have to answer. The description of Q queries follows.
  • The i-th line contains an integer Ki followed by Ki distinct space-separated integers v1,v2,,vKi, where Ki is the number of vertices in this query and v1,v2,vKi denote the vertices themselves.

Output Format

For each test, print one line containing Q space-separated integers. The i-th of these integers should be the answer to the i-th query.

Constraints

  • 1T3
  • 10N105
  • 1Pi<i
  • 1Wi5106
  • 1Q3104
  • 10KiN
  • 1viN
  • All vi present in a single query are distinct
  • It is guaranteed that the sum of Ki over all queries over all test cases in a single test file does not exceed 1.5106
  • It is guaranteed that the sum of Q over all test cases in a single test file does not exceed 3104

Sample Input 1 

1
10 1
1 2 2 3 5 4 4 8 8
20 30 40 50 60 70 80 90 100
10 1 4 5 6 2 3 7 8 9 10

Sample Output 1 

3300

Sample Input 2 

1
10 2
1 2 2 3 5 4 4 8 8
20 30 40 50 60 70 80 90 100
4 1 4 5 6
4 2 3 7 8

Sample Output 2 

320 410

Explanation

In the second sample:

Let's look at different values of g(u,v) for this tree.

For the first query, g(1,4)=40g(1,5)=50g(1,6)=60g(4,5)=50g(4,6)=60g(5,6)=60. The sum of all these values is 320.

For the second query, g(2,3)=30g(2,7)=70g(2,8)=80g(3,7)=70g(3,8)=80g(7,8)=80. The sum of all these values is 410.

Note: The second sample does not satisfy the constraint Ki10, so it won't be present in the actual test data. It's added here only for illustration.

Sample tree

Problem Code: YATP

Comments

Popular posts from this blog

Flipkart Runway: Season 3 | 2025 | Internship

Walmart Sparkplug 2022 | 150 students will be selected for summer internship | ₹1-1.1 lakh monthly

Software Testing Week 5 : Assignment 5 | 2022