The smallest path
Practice
3.6 (5 votes)
Advanced algorithms
Algorithms
C++
Square root decomposition
Problem
58% Success 1024 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given an undirected tree with $$N$$ nodes and the tree is rooted at node 1. Each node has an integer value $$A[i]$$ associated with it represented by an array $$A$$ of size $$N$$.
Also, you are given $$Q$$ queries of the following type:
- $$u v k$$: Find the \(k^{th}\) smallest value present on the simple path between node $$u$$ and node $$v$$. If the number of nodes on the simple path between node $$u$$ and node $$v$$ is less than $$k$$, then print $$-1$$.
Input format
- The first line contains two space-separated integers $$N Q$$ denoting the number of nodes and queries respectively.
- The second line contains $$N$$ space-separated integers denoting array $$A$$.
- Next $$N - 1$$ lines contain two space-separated integers denoting the edges of the tree.
- Next $$Q$$ lines contain three space-separated integers denoting the queries.
Output format
Print $$Q$$ space-separated integers denoting the answer of queries.
Constraints
\(1 \le N \le 4 \times 10^4 \\ 1 \le Q \le 10^5 \\ 1 \le A[i] \le 10^9 \\ \)
Submissions
Please login to view your submissions
Similar Problems
Points:50
2 votes
Tags:
AlgorithmsHardSquare Root Decomposition
Points:50
4 votes
Tags:
AlgorithmsApprovedHardSqrt-Decomposition
Points:50
1 votes
Tags:
AlgorithmsApprovedHardSqrt-Decomposition
Editorial