Tree And Special node
Practice
3.7 (6 votes)
Algorithms
Depth first search
Graphs
Medium
Problem
79% Success 4359 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Given a rooted undirected tree T consisting of N nodes with 1 as the root of the tree. Each node of the tree has a value associated with it, where the value of the ith node is A[i]. A node x is said to be special if the path from the root to node x contains all distinct values. Your task is to find the number of special nodes.

Input:

The first line contains an integer N denoting the number of nodes in the tree.

Next line contains N space separated integers where the ith integer denotes A[i].

Next N-1 lines consist of two space-separated integers u and v, denoting that there is an edge between node u to node v.

Output:

Print a single integer, denoting the number of special nodes in the tree.

Constraints:

\(1 \le N \le 10^6 \\ 0 \le A[i] \le 10^6 \\ 1 \le u, v \le N\)

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
152 votes
Tags:
ApprovedDepth First SearchGraphsMediumOpen
Points:30
8 votes
Tags:
AlgorithmsApprovedCombinatoricsDepth First SearchDynamic ProgrammingGraphsMathMediumReadyRecruit
Points:30
6 votes
Tags:
DatastructuresGraphsAlgorithmsDepth First Search