#P7570. 「MCOI-05」多宇
「MCOI-05」多宇
Description
Dream abstracts space-time as a rooted directed tree with nodes. The root is node , and all edges are directed from shallow to deep (from ancestor to descendant).
Dream uses his superpower to add additional directed edges to this tree, but the final graph is still a directed acyclic graph (DAG).
Dream further abstracts an event as a node in the graph, and an era as a simple path in the graph.
Dream says that a pair of events is feasible if and only if there exists an era such that the first event of the era is and the last event is .
Dream is not satisfied with counting ordinary feasible pairs. He believes the additional edges added by the superpower are very important.
Dream says that a pair of events is conditionally feasible if and only if is feasible, and after removing all additional edges, becomes infeasible.
Now Dream has queries. The -th query is given by two positive integers and , where .
For each query, Dream wants to know how many conditionally feasible event pairs satisfy .
Input Format
The first line contains two integers .
The next line contains positive integers describing the tree structure. The -th number denotes the index of the parent of node , . That is, there is an edge from to .
The next lines each contain two positive integers , denoting an additional edge directed from to .
The next line contains a positive integer .
The next lines each contain two positive integers , denoting one query.
Output Format
Output lines. Each line contains one integer, the answer to the corresponding query.
2 2
1
1 2
1 2
1
1 2
0
8 2
1 2 5 1 4 3 3
2 4
4 7
3
4 6
5 7
1 8
0
1
4
Hint
- Subtask 1 (3 pts): , 3s.
- Subtask 2 (29 pts): , , 3s.
- Subtask 3 (31 pts): , 5s.
- Subtask 4 (37 pts): no extra constraints, 5s.
For of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号