Tree and K-Tuple
Practice
5 (3 votes)
Algorithms
Approved
Graphs
Medium
Trees
Problem
89% Success 614 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Problem:
You have a Tree with N nodes rooted at 1 where \(i^{th}\) node is associated with a value \(A_i\). Now consider the following definition:
K-Tuple: A tuple of \(K+1\) nodes \((X_{1},X_{2},X_{3},\dots,X_{K+1})\) is a K-Tuple if:
1.) \(X_{i}\) is an ancestor of \(X_{i+1} \forall 1 \leq i \leq K\)
2.) \(A_{X_{i}} > A_{X_{i+1}} \forall 1 \leq i \leq K \)

Calculate the number of K-Tuples in the tree.

Input:
First line contains two integers N and K, the number of nodes in the tree and the value for the Tuple type you need to calculate answer for.
Next line contains N space separated integers where the \(i^{th}\) integer denotes the value \(A_i\).
Next \(N-1\) lines contains X and Y denoting that there is an edge between them.

Output:
Output a single integer mod \((10^9+7)\) denoting the number of K-tuples in the tree.

Constraints:
\( 1 ≤ N,A_i ≤ 100000 \)
\( 1 ≤ X,Y ≤ N \)
\( 1 ≤ K ≤ 20 \)

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:50
Tags:
AlgorithmsApprovedDepth First SearchGraphsMedium
Points:50
37 votes
Tags:
Data StructuresDisjoint setGraphsMapsMediumReadyTrees
Points:50
22 votes
Tags:
GraphsMediumOpen