Special Paths | Google OA | July 2022 | Graph
Anonymous User
7630

You are given the following:

  1. A tree with N nodes
  2. An array A denoting the value of each node

A path is called special path if the following conditions are satisfied:

  1. All nodes in the path are traversed exactly once.
  2. The value of starting and terminating node in the path is same and startnode != terminating node
  3. The values of any node in the path are not greater than the value of starting node.

Task:
Count the no. of special paths in the tree. (Two paths are different if they contain at least one different node.)

Example:
N = 5
edges = [(1,2), (1,3), (3,4), (3,5)]
A = [2,3,1,2,3]

image

Output:
2

Explanation:
path 1: 1(2)->3(1)->4(2)
path 2: 2(3)->1(2)->3(1)->5(3)

Constraints
1<= T <= 10
2 <= N <= 10^5
1<=Ai <= 10^9

I tried brute force by applying DFS at every node. I got TLE.
Any suggestions or approach is appreciated :)

Comments (12)