#P9150. 邮箱题

    ID: 9141 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>图论洛谷原创O2优化强连通分量洛谷月赛均摊分析

邮箱题

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 nn vertices and mm edges. Each vertex contains a key to another vertex: vertex ii contains the key to vertex kik_i. You can enter a vertex if and only if you have the key to that vertex. It is guaranteed that kik_i 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 ii and are at vertex ii. For each ii, you need to find:

  1. How many vertices you can reach.
  2. How many vertices you can reach and then return to the starting vertex ii.

Please note: all given edges are directed edges!

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, the number of test cases. For each test case:

The first line contains two integers n,mn, m, representing the number of vertices and edges in the graph.

The second line contains nn integers k1,k2,,knk_1, k_2, \ldots, k_n, meaning that vertex ii contains the key to vertex kik_i. It is guaranteed that kik_i forms a permutation.

The next mm lines each contain two integers x,yx, y, indicating a directed edge from xx to yy in the graph. It is guaranteed that there are no multiple edges or self-loops.

Output Format

For each test case, output nn lines. Each line contains two integers. On the ii-th line, the integers represent the number of vertices reachable when starting from vertex ii, 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).

  1. The set of vertices that 11 can reach is {1,2,3,4}\{1,2,3,4\}, and the set of vertices that 11 can reach and return to 11 is {1,2,3,4}\{1,2,3,4\}.
  2. The set of vertices that 22 can reach is {2,3}\{2,3\}, and the set of vertices that 22 can reach and return to 22 is {2}\{2\}.
  3. The set of vertices that 33 can reach is {3}\{3\}, and the set of vertices that 33 can reach and return to 33 is {3}\{3\}.
  4. The set of vertices that 44 can reach is {4}\{4\}, and the set of vertices that 44 can reach and return to 44 is {4}\{4\}.

This is a valid traversal process: start from 11, the initial key is 22, reach vertex 22 and obtain key 33, reach vertex 33 and obtain key 44, return to vertex 11, reach vertex 44 and obtain key 11, reach vertex 33, and return to vertex 11.

[Constraints]

For 100%100\% of the data, n3n \ge 3, m0m \ge 0, n1.5×106\sum n \le 1.5\times{10}^6, m3×106\sum m \le 3\times{10}^6, 1T2×1041 \le T \le 2\times{10}^4, 1x,yn1 \le x, y \le n. 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 nn Constraint on mm Score Dependency
1 n6n\le 6 m12m\le 12 2020 \
2 n3107\sum n^3\le {10}^7 m32×107\sum m^3\le 2\times {10}^7 2525
3 n2108\sum n^2\le {10}^8 m2108\sum m^2\le {10}^8 Subtasks 1 and 2
4 3030 Subtasks 1, 2, and 3

Translated by ChatGPT 5