#P7349. 「MCOI-04」Dream and the Multiverse
「MCOI-04」Dream and the Multiverse
Description
Dream models time and space as a rooted directed tree with nodes, where the root is node and all edges are directed from shallow to deep.
Dream uses his superpower to add extra directed edges to this tree, and the final graph is still a directed acyclic graph.
Dream further models an event as a node in the graph, and models an era as a simple path in the graph.
Dream considers a pair of events to be feasible if and only if there exists an era whose first event is and last event is .
Dream now has queries. The -th query is given by two positive integers and , where .
For each query, Dream wants to know how many feasible event pairs satisfy .
Input Format
The first line contains two integers .
The next line contains positive integers describing the tree. The -th number denotes the index of the parent of node , namely , which means there is a directed edge from to .
The next lines each contain two positive integers , indicating an extra directed edge added from to .
The next line contains a positive integer .
The next lines each contain two positive integers , representing a query.
Output Format
Output lines, each containing one integer, which is the answer to the corresponding query.
2 2
1
1 2
1 2
1
1 2
3
Hint
Constraints and Notes
This problem uses bundled testdata.
- Subtask 1 (1 pts): The tree is a chain.
- Subtask 2 (11 pts): .
- Subtask 3 (7 pts): .
- Subtask 4 (23 pts): .
- Subtask 5 (17 pts): .
- Subtask 6 (41 pts): No special constraints.
For of the testdata, , , .
It is guaranteed that the extra edges will not form a cycle, and the given form a rooted tree with root .
It is guaranteed that .
Notes
Minecraft OI Round 4 C
idea & solution: w33z8kqrqk8zzzx33 check: ClCN
Translated by ChatGPT 5
京公网安备 11011102002149号