Minimum Nodes
Practice
4.3 (7 votes)
Algorithms
Depth first search
Easy
Graphs
Trees
Problem
67% Success 2953 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given a tree with N nodes and each node has some value assigned to it. Now you are given Q tasks of the form X K.
For each task, you have to find the path starting from X such that sum of nodes in the path is at least K and it contains minimum number of nodes. If such path exists, print the count of nodes in the path, else print -1.
Input format
- First line: Two space-separated integers N and Q
- Second line: N space-separated integers (denoting vali)
- Next N -1 lines: Two space-separated integers U and V (denoting an edge between the nodes U and V)
- Next Q lines:Two space-separated integers X and K
Output format
For each task, print the required answer in a new line.
Input Constraints
\(1 \le N \le 10^5\)
\(1 \le Q \le 10\)
\( 1\le val_i \le 10^9\)
\(1 \le U,V \le N\)
\(1 \le X \le N\)
\(1 \le K \le 10^{10}\)
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
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
41 votes
Tags:
AlgorithmsC++Depth First SearchGraphs
Points:20
37 votes
Tags:
AlgorithmsApprovedBacktrackingDepth First SearchEasyGraphsOpenRecursion
Points:20
24 votes
Tags:
AlgorithmsEasyGraphsHiring
Editorial
Login to unlock the editorial