King Arthur’s Kingdom
Practice
2.2 (4 votes)
Bit manipulation
Depth first search
Easy
Graphs
Problem
18% Success 1553 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a tree consisting of N nodes and another integer K, where the \(i^{th}\) node has value equal to \(v[i]\).

Now, we call the value of a sub-tree the number of distinct \(v[i]\) among all nodes it contains. You need to find the number of sub-trees of this tree having value \( \le K \) .

A sub-tree of a tree is a subset of nodes and edges of the tree that form a single connected component.

Input: :

Input starts with an integer \( ( 1 \le T \le 15)\) denoting the number of test cases. Each case starts with two positive integers N and K.

The next line consists of N space separated integers, where the \(i^{th}\) integer denotes \(v[i]\). Each of the next \(N-1\) lines contains 2 space separated integers u and v, denoting an edge between nodes u and v,

Output:

For each test case, print the answer on a new line.

Constraints:

\( 1 \le T \le 15 \)

\( 2 \le N \le 18 \)

\( 1 \le K \le N \)

\( 0 \le v[i] \le 10^9 \)

\( 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
7 votes
Tags:
AlgorithmsBinary SearchDepth First SearchEasySearching
Points:30
9 votes
Tags:
Depth First SearchEasyGraphsImplementationRecursion
Points:30
174 votes
Tags:
ApprovedDepth First SearchEasyGraphsOpen