There is a country consisting of \(N\) cities numbered with integer numbers from \(0\) to \(N - 1\) with values \(A_i \; (0 \leq i \leq N-1)\), and \(N-1\) bidirectional roads connecting them, such that we can travel between any two cities using these roads. In other words, these cities and roads form a tree.
The president has assigned \(Q\) tasks to \(Q \) people, and each person has to visit a city \(V \) from their current city \(U\) to complete their work. However, due to the long travel distances, the president allows them to relax at cities having value \(K.\) The workers have come to you seeking assistance in determining the maximum number of cities where they can relax. Your task is to help them find the maximum number of cities where they can relax.
Input format
- The first line contains an integer \(N\) denotes the number of cities.
- The second line contains \(N\) space-separated integers denoting the values of cities \(A_i\).
- The next \(N - 1\) line contains \(2\) space-separated integers \(u\) and \(v\) denoting the connection between cities.
- The fourth line contains \(Q\) denoting the number of tasks.
- Each of the next \(Q\) lines contains three space-separated integers \(U, V\) and \(K\).
Output format
For each task, print the maximum number of cities where they can relax in a new line.
Constraints
\(2 \lt N \le 10 ^ 5\)
\(0 \leq u, v \leq N-1\)
\(0 \lt Q \le 10 ^ 5\)
\(0 \leq U, V \le N-1\)
\(U \neq V\)
\(0 \lt A_i\ ,\ K \le 10 ^ 9\)
5 14 2 10 7 10 0 2 2 4 3 1 4 1 3 0 4 1 0 1 6 3 0 2
0 0 1
- First query: There are no any cities with value \(1 \) in the path from \(0\) to \(4\).
- Second query: There are no any cities with values \(6 \) in the path from \(0\) to \(1\).
- Third query: There is only one city with value \(2\) in the path from \(3\) to \(0\), that is city \(1\).
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
Login to unlock the editorial
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