Workers

4.8

4 votes
Algorithms, Euler Tour and Path, Graphs, Segment tree, Tree, Lowest Common Ancestor
Problem

There is a country consisting of N cities numbered with integer numbers from 0 to N1 with values Ai(0iN1), and N1 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 Ai.
  • The next N1 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<N105
0u,vN1
0<Q105
0U,VN1
UV
0<Ai , K109

 

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation
  • 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.
Editor Image

?