#P9150. 邮箱题
邮箱题
Background
A mailbox is a long-standing box for receiving letters. In the West, mailboxes are mainly red, while in the East, mailboxes are mainly green.
Problem Description
You are given a directed graph with vertices and edges. Each vertex contains a key to another vertex: vertex contains the key to vertex . You can enter a vertex if and only if you have the key to that vertex. It is guaranteed that forms a permutation.
As long as you enter a vertex, you obtain the key inside that vertex. Once obtained, a key is never consumed.
Now you have obtained the key to vertex and are at vertex . For each , you need to find:
- How many vertices you can reach.
- How many vertices you can reach and then return to the starting vertex .
Please note: all given edges are directed edges!
Input Format
This problem has multiple test cases.
The first line contains a positive integer , the number of test cases. For each test case:
The first line contains two integers , representing the number of vertices and edges in the graph.
The second line contains integers , meaning that vertex contains the key to vertex . It is guaranteed that forms a permutation.
The next lines each contain two integers , indicating a directed edge from to in the graph. It is guaranteed that there are no multiple edges or self-loops.
Output Format
For each test case, output lines. Each line contains two integers. On the -th line, the integers represent the number of vertices reachable when starting from vertex , and the number of vertices that are reachable and can return to the starting point.
3
4 5
2 3 4 1
1 2
2 3
3 1
1 4
4 3
5 6
2 3 4 5 1
1 2
2 3
3 4
4 5
5 2
4 1
3 2
2 3 1
1 2
1 3
4 4
2 1
1 1
1 1
5 5
5 5
3 1
2 1
1 1
2 1
1 1
1 1
Hint
[Sample Explanation]
The following is the explanation for the first test case (the content in parentheses in the figure is the key number on the vertex).

- The set of vertices that can reach is , and the set of vertices that can reach and return to is .
- The set of vertices that can reach is , and the set of vertices that can reach and return to is .
- The set of vertices that can reach is , and the set of vertices that can reach and return to is .
- The set of vertices that can reach is , and the set of vertices that can reach and return to is .
This is a valid traversal process: start from , the initial key is , reach vertex and obtain key , reach vertex and obtain key , return to vertex , reach vertex and obtain key , reach vertex , and return to vertex .
[Constraints]
For of the data, , , , , , . It is guaranteed that the graph contains no multiple edges or self-loops.
This problem uses bundled testdata and enables subtask dependencies!
| Subtask | Constraint on | Constraint on | Score | Dependency |
|---|---|---|---|---|
| 1 | \ | |||
| 2 | ||||
| 3 | Subtasks 1 and 2 | |||
| 4 | Subtasks 1, 2, and 3 | |||
Translated by ChatGPT 5