G.W. Coding Contest 3 P6: Evil Questions Attacking Ontario
At Dr. G.W. Williams S.S, Each of the \(N\) classrooms can be represented by a number from 1 to \(N\), with 1 being the main entrance. There are also \(N-1\) hallways, each stemming out from the main entrance connecting different pairs of classrooms both ways. Each hallway takes 1 minute to traverse.
It is possible to reach any classroom from any classroom using this layout.
Unfortunately EQAO is starting, and the school closes some number of classrooms per day for each of the \(D\) days of EQAO so that you cannot travel through these classrooms. This makes it possible to not be able to reach every classroom. To try to combat this, the school architects have decided to build a temporary hallway every day to connect each of the classrooms at layer \(L\) from west to east.
Annoyingly, your friends volunteer you into a game where you sum the shortest paths from classroom \(A\) to classroom \(B\) for each of the \(D\) days given these new constraints. Even more annoyingly, the school happens to shut down all classrooms connected to classroom \(A\) and \(B\) which are one layer below \(A\) and \(B\).
Given the layout of the school, the number of days for EQAO, and \(A\), \(B\) and \(L\) for each day, can you find the sum of the shortest paths for your friends?
The first line contains the integer \(N\), the number of classrooms in Dr. G.W. Williams.
The following \(N-1\) lines contain integers \(u\)i and \(v\)i, representing a hallway from classroom \(u\)i to \(v\)i
The next line contains the integer \(D\), the number of days of EQAO
The following \(D\) lines contain the integers \(A\)i, \(B\)i, and \(L\)i, representing the two classrooms to find the shortest path between, and the layer which the temporary hallway is being built for day \(i\).
\(\{\text{1 } \leq N \leq 10^3 \}\)
\(\{\text{1 } \leq Q \leq 10^6 \}\)
For each of the \(D\) days, output how long it will take to get from classroom \(A\)i to \(B\)i in minutes on a new line.
As you may be able to tell, the school's solution to the travel problem is suboptimal (they tried their best), meaning that for some days of EQAO it is impossible to reach all classrooms. None of the inputs will contain these cases.
It will also be guaranteed classroom A is not directly under to classroom B and vice versa. For example, 1 is directly under 7.
Sample Input 1:
Sample Output 1:
Diagram of school without EQAO:
Submit Solution