#1465. Dream and the Multiverse REMATCH
Dream and the Multiverse REMATCH
题目背景
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 . Note that does not have to hold for a plausible pair.
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 .
输入格式
The first line of the input contains two space-separated integers and .
The second line contains space-separated integers .
lines follow. Each of these lines contains two space-separated 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 space-separated integers and describing a query.
输出格式
For each query, print a single line containing one integer ― the number of plausible pairs such that .
8 2
1 2 5 1 4 3 3
2 4
4 7
3
4 6
5 7
1 8
2
2
18
说明/提示
- for each valid
- the graph described on the input is acyclic
Subtasks
Subtask #1 (17 points):
Subtask #2 (83 points): original constraints