In the vibrant realm of Connectville, there stands a giant tree that acts as a roadmap for its many towns. This tree showcases \(N\) towns numbered \(1\) to \(N\), connected by \(N-1\) pathways. The townspeople of Connectville have a favorite pastime: a daily puzzle game.
Each morning, \(M\) riddles are put forth by the town's riddle makers. For each riddle, they'd pick three towns - \(A\), \(B\), and \(C\) - and ask everyone a fun question: How many towns (let's say \(X\)), other than towns \(A\), \(B\), and \(C\), exist such that one could travel through all four towns (A, B, C, and X) without revisiting any town along the way?
Can you unravel the riddles of Connectville's towns?
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 vertices in the tree.
- The next \(N-1\) lines contain 2 space-separated integers, \(u\) and \(v\), indicating that there is a pathway connecting towns \(u\) & \(v\).
- The next line contains \(M\) denoting the number of queries.
- The next \(M\) lines contain 3 unique space-separated integers, \(A\), \(B\), and \(C\).
Output format
For each test case, answer all the \(M\) queries. For each query print in a new line the number of towns other than towns \(A\), \(B\), and \(C\), that exist such that one could travel through all four towns without revisiting any town along the way.
Constraints
The first line denotes T = 1.
For test case \(1\):
We are given:
- N = 6, M = 2, and the tree is linked as follows:
Now,
- For the first query, we are given A=1, B=6, and C=4.
- We have 2 possible values for X : 3 and 2, as-
- we can visit towns in order \(3 → 2 → 1 → 4 → 6\), and
- we can visit towns in order \(2 → 1 → 4 → 6\).
- Hence, the answer to this query is 2.
- We have 2 possible values for X : 3 and 2, as-
- For the Second query, we are given A=4, B=5, and C=6.
- No town X exists such that we could travel through all four towns (A, B, C, and X) without revisiting any town along the way.
- Hence, the answer to this query is 0.
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