Shubham and Tree-1
Practice
4 (1 votes)
Medium
Algorithms
Medium
Trees
Dynamic programming
Problem
70% Success 574 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You are given a tree consisting of n nodes and rooted at 1. Each node and each edge of the tree have a value associated with them. Consider a node j and one of it's ancestor node i. Cost of node j with respect to node i is defined as sum of value of node j and sum of value of all edges present in the simple path from node i to node j. For each node i, output the number of distinct costs of all its descendants.

Input Format
First line contains n denoting the number of tree nodes.
Next n-1 lines each contain 3 integer numbers \(a, b, c\) denoting an edge between node a and node b of value c.
Next line contains n integers denoting the value of each node.

Output Format
For node i ranging from 1 to n, output the number of distinct costs among its descendants, each on a new line.

Constraints
\(1\le n\le 10^5\)
\(-10^9\le Value Of Node,Value Of Edge\le 10^9\)

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
10 votes
Tags:
Dynamic ProgrammingAlgorithmsTrees
Points:30
12 votes
Tags:
AlgorithmsDepth-first searchTreesDynamic Programming
Points:30
1 votes
Tags:
TreeDynamic ProgrammingAlgorithmsMedium