I have gone over the scenarios in my head,
and there are 6.96969 billion outcomes, and only one of them -
- do I win.
Dream abstracts the fabric of spacetime as a directed rooted tree (arborescence) with nodes (numbered through ). Node is the root and for each , the parent of node is . All edges of this tree are directed away from the root.
Then, Dream employs a magical superpower and adds directed edges to this tree in such a way that the resulting directed graph remains acyclic (a DAG).
Let's call a node of this DAG an event and further call a simple path on this DAG an era. Dream considers a pair of events to be plausible if there is an era whose first event is and last event is .
Dream now wants you to answer queries. In each query, he gives you two positive integers and , where , and he wishes to know the number of plausible pairs of events such that .
Constraints
The given tree and extra edges form a DAG.
Subtask 1 [1%]
The tree is a line graph.
Subtask 2 [11%]
Subtask 3 [7%]
Subtask 4 [23%]
Subtask 5 [17%]
Subtask 6 [41%]
No additional constraints.
Input Specification
The first line of the input contains two integers and .
The second line contains integers .
lines follow. Each of these lines contains two integers and describing an additional edge from node to node .
The following line contains a single integer .
lines follow. Each of these lines contains two integers and describing a query.
Output Specification
For each query, print a single line containing one integer — the number of plausible pairs such that .
Sample Input
8 2
1 2 5 1 4 3 3
2 4
4 7
3
4 6
5 7
1 8
Sample Output
6
5
27
Comments
TechnobladeNeverDies, henrybaolol9. TechnobladeNeverDies
And neither does Technoblade.
This comment is hidden due to too much negative feedback. Show it anyway.
TechnobladeNeverDies