#P10790. [NOI2024] 树形图
[NOI2024] 树形图
Background
Due to differences in the performance of judging machines, the time limit for this problem is doubled.
Problem Description
Given a simple directed graph with vertices and edges, where the vertices are numbered from to . A simple directed graph is defined as a directed graph with no multiple edges and no self-loops.
A vertex is defined to be a root of the directed graph if and only if for every , there exists exactly one directed simple path from vertex to vertex , where a simple path is defined as a path that does not pass through any repeated vertex.
The type of each vertex is defined as follows:
- If vertex is a root of graph , then vertex is called a type-1 vertex of graph .
- If vertex is not a type-1 vertex of graph , and there exists a way to delete edges such that the graph obtained after deleting some edges from satisfies: all type-1 vertices in are roots of , and vertex is also a root of , then vertex is called a type-2 vertex of graph .
- If vertex does not satisfy the above conditions, then vertex is called a type-3 vertex of graph .
According to the definitions above, every vertex in graph belongs to exactly one of the three types: type-1, type-2, or type-3. You need to determine which type each vertex belongs to.
Input Format
This problem has multiple test cases.
The first line contains a non-negative integer , indicating the test point ID. means this test point is a sample.
The second line contains a positive integer , indicating the number of test cases.
Then follow the test cases one by one. For each test case:
The first line contains two positive integers , representing the number of vertices and edges in the directed graph.
The next lines each contain two positive integers , representing a directed edge from to . It is guaranteed that , and the given directed graph has no multiple edges and no self-loops.
Output Format
For each test case, output one line containing a string of length exactly , representing the type of each vertex. Here means vertex is a type-1 vertex, means vertex is a type-2 vertex, and means vertex is a type-3 vertex.
0
2
4 7
2 1
4 1
1 4
2 3
3 4
2 4
4 3
4 5
1 2
2 3
2 4
3 1
4 3
3233
2211
见 graphee2.in/ans
这个样例满足测试点 2 的约束条件
见 graphee3.in/ans
这个样例满足测试点 3,4 的约束条件
见 graphee4.in/ans
这个样例满足测试点 5,6 的约束条件
见 graphee5.in/ans
这个样例满足测试点 8,9 的约束条件
见 graphee6.in/ans
这个样例满足测试点 14,15 的约束条件
Hint
[Sample 1 Explanation]
Sample contains two test cases.
For the first test case, the input graph is as follows:

Since all have no path that can reach , are all type-3 vertices. Since there are three directed simple paths from to : , , , vertex is not a type-1 vertex. After deleting edges , , , , the directed simple path from to each of becomes unique, so is a root of graph , i.e. is a type-2 vertex.
For the second test case, the input graph is as follows:

It is easy to see that are both type-1 vertices. After deleting edge , the directed simple path from every vertex to every other vertex is unique, so are both type-2 vertices.
[Constraints]
For all test cases, it is guaranteed that , , , and graph has no multiple edges and no self-loops.
::cute-table{tuack}
| Test Point ID | Special Property | |||
|---|---|---|---|---|
| None | ||||
| A | ||||
| ^ | ^ | B | ||
| None | ||||
| A | ||||
| ^ | BC | |||
| B | ||||
| C | ||||
| None | ||||
- Special Property A: It is guaranteed that there are no type-1 vertices.
- Special Property B: It is guaranteed that there are no type-2 vertices.
- Special Property C: It is guaranteed that vertex is a type-1 vertex of graph .
Translated by ChatGPT 5