RebelReach
Practice
3.8 (5 votes)
Binary search
Trees
Math
Algorithms
Problem
44% Success 2676 Attempts 50 Points 2s Time Limit 512MB Memory 1024 KB Max Code

In a kingdom with \(N\) cities connected by \(N-1\) roads, the king's palace is located in the first city. Each city has a specific number of guards, given in an array \(Guards[]\), where \(Guards[i]\) is the number of guards in the city \(i\). The kingdom must assess the risk of revolts starting from any city and how close rebels could get to the king's palace. The guards' effectiveness is such that one guard can stop one rebel. 

To tackle this, the king has asked you to develop a method for analyzing such scenarios. This includes answering queries of type 'query(\(U\), \(X\))', where \(U\) represents the city where the revolt might begin, and \(X\) indicates the number of protestors at the start. Your task is to determine for each query the nearest city to the king's palace (i.e., city 1) that the rebels can reach.

Note: If the rebels can reach City 1, you should print City 1, as this is the city where the King's palace is located.

Input format

  • The first line contains a single integer \(T\), which denotes the number of test cases.
  • For each test case:
    • The first line contains \(N\) denoting the number of Cities.
    • The following \(N-1\) lines contain 2 space-separated integers, \(u\) & \(v\) indicating that there is a road between City \(u\) & City \(v\) 
    • The next line contains N integers, denoting the array Guards.
    • The next line contains \(Q\) denoting the number of queries.
    • The next \(Q\) lines containspace-separated integers, \(U\) and \(X\), where \(U\) denotes the city where the revolt might begin, and \(X\) indicates the number of rebels at the start.

Output format

For each test case, answer all the \(Q\)  queries. For each query, print in a new line the nearest city to the king's palace (i.e., city 1) that the rebels can reach.

Constraints

\(1 \leq T \leq 10^5 \\ 3 \leq N \leq10^6 \\ 1≤u,v≤N\\ 1≤Guards[i]≤10^9\ ∀\ i∈[1,N] \\ 1 \leq Q \leq 10^6\\ 1≤U≤N\\ 1≤X≤10^{15} \\ \text{The sum of all values of N over all test cases doesn't exceed }10^6 \\ \text{The sum of all values of Q over all test cases doesn't exceed }10^6\)

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
246 votes
Tags:
ReadyMathematicsSortingApprovedEasy-Medium
Points:20
354 votes
Tags:
ReadyMapApprovedEasy
Points:50
36 votes
Tags:
AlgorithmsApprovedBinary SearchHardOpenSorting