#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 GG with nn vertices and mm edges, where the vertices are numbered from 11 to nn. A simple directed graph is defined as a directed graph with no multiple edges and no self-loops.

A vertex rr is defined to be a root of the directed graph GG if and only if for every 1kn1\leq k\leq n, there exists exactly one directed simple path from vertex rr to vertex kk, 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 rr is a root of graph GG, then vertex rr is called a type-1 vertex of graph GG.
  • If vertex rr is not a type-1 vertex of graph GG, and there exists a way to delete edges such that the graph GG' obtained after deleting some edges from GG satisfies: all type-1 vertices in GG are roots of GG', and vertex rr is also a root of GG', then vertex rr is called a type-2 vertex of graph GG.
  • If vertex rr does not satisfy the above conditions, then vertex rr is called a type-3 vertex of graph GG.

According to the definitions above, every vertex in graph GG belongs to exactly one of the three types: type-1, type-2, or type-3. You need to determine which type each vertex 1n1\sim n belongs to.

Input Format

This problem has multiple test cases.

The first line contains a non-negative integer cc, indicating the test point ID. c=0c=0 means this test point is a sample.

The second line contains a positive integer tt, 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 n,mn,m, representing the number of vertices and edges in the directed graph.

The next mm lines each contain two positive integers u,vu,v, representing a directed edge from uu to vv. It is guaranteed that 1u,vn1\leq u,v\leq n, and the given directed graph GG has no multiple edges and no self-loops.

Output Format

For each test case, output one line containing a string ss of length exactly nn, representing the type of each vertex. Here si=1s_i=1 means vertex ii is a type-1 vertex, si=2s_i=2 means vertex ii is a type-2 vertex, and si=3s_i=3 means vertex ii 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 11 contains two test cases.

For the first test case, the input graph is as follows:

Since 1,3,41,3,4 all have no path that can reach 22, 1,3,41,3,4 are all type-3 vertices. Since there are three directed simple paths from 22 to 11: 212\to 1, 2412\to 4\to 1, 23412\to 3\to 4\to 1, vertex 22 is not a type-1 vertex. After deleting edges 141\to 4, 414\to 1, 343\to 4, 434\to 3, the directed simple path from 22 to each of 1,3,41,3,4 becomes unique, so 22 is a root of graph GG', i.e. 22 is a type-2 vertex.

For the second test case, the input graph is as follows:

It is easy to see that 3,43,4 are both type-1 vertices. After deleting edge 232\to 3, the directed simple path from every vertex to every other vertex is unique, so 1,21,2 are both type-2 vertices.

[Constraints]

For all test cases, it is guaranteed that 1t101\leq t\leq 10, 2n1052\leq n\leq 10^5, 1m2×1051\leq m\leq 2\times 10^5, and graph GG has no multiple edges and no self-loops.

::cute-table{tuack}

Test Point ID tt\leq nn\leq mm\leq Special Property
11 33 1010 2020 None
22 1010 10310^3 20002000 A
3,43,4 ^ ^ B
5,65,6 None
77 10510^5 2×1052\times 10^5 A
8,98,9 ^ BC
101310\sim 13 B
14,1514,15 C
162016\sim 20 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 11 is a type-1 vertex of graph GG.

Translated by ChatGPT 5