A vertex set
Practice
2.8 (5 votes)
Algorithms
Dynamic programming
2d dynamic programming
C++
Problem
65% Success 719 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a tree \(T\) with \(N\) nodes and the tree is rooted at node 1. Each node has a value \(A[i]\) associated with it. Let us define a set \(X\) as follows:

\(X = \text{\{}v_1, v_2 , .... , v_k \}\) , set X has nodes \(v_1, v_2, ..... ,v_k\) such that there does not exist an edge between any two vertices in \(X\).

Also, \(F(v)\) is the maximum value node present in the subtree of node \(v\) (including itself).

\(V(X)\)\(\sum_{i = 1}^{i = k} F(v_i)\) where \(\{v_1, v_2 , .... ,v _ k \} \ \text{belong to X} \)

Find the maximum value of \(V(X)\).

Input format

  • The first line contains an integer \(T\) denoting the number of test cases.
  • The first line of each test case contains an integer \(N\) denoting the number of nodes in the tree.
  • Next \(N-1\) lines contain two space-separated integers denoting the edges.
  • The last line contains \(N\) space-separated integers denoting the value of nodes.

Output format

For each test case, print the maximum value of \(F(X)\) in a new line.

Constraints

\(1 \le T \le 10 \\ 1 \le N \le 10^5 \\ 1 \le A[i] \le 10^6\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
5 votes
Tags:
2D dynamic programmingAlgorithmsDynamic Programming
Points:30
4 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingMediumOpen
Points:30
16 votes
Tags:
ApprovedBasic ProgrammingImplementationMediumOpenRecursionTwo dimensional