#P13823. 「Diligent-OI R2 C」所谓伊人

    ID: 13569 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>图论并查集2025洛谷原创广度优先搜索 BFS深度优先搜索 DFS最短路洛谷月赛

「Diligent-OI R2 C」所谓伊人

Description

Given a directed graph with nn vertices and mm edges, numbered from 11 to nn, with no guarantee of no duplicate edges or self-loops. Each vertex ii has a weight pip_i.

If there exists a path from vertex uu to vertex vv, you can swap the weights of uu and vv.

For each vertex ii, output the minimum number of swaps needed to maximize the weight of vertex ii. Note that each answer is independent, meaning the swaps should be considered starting from the initial graph each time.

Input Format

Note that this problem requires efficient input and output methods, and during implementation, attention should be paid to the impact of constants on program efficiency.

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

The second line contains nn integers p1,p2,,pnp_1,p_2,\dots,p_n.

The next mm lines each contain two integers u,vu,v representing a directed edge from vertex uu to vertex vv.

Output Format

Output a line containing the minimum number of swaps needed to maximize the weight of each vertex 1,2,,n1,2,\dots,n in sequence.

6 5
1 1 4 5 1 4
1 2
2 1
3 4
4 5
3 5
0 0 1 0 1 0

Hint

Sample #1 Explanation

It can be proven that the maximum possible weights for the 66 vertices are 1,1,5,5,5,41,1,5,5,5,4, respectively.

  • To maximize the weight of vertex 11: No swaps needed.
  • To maximize the weight of vertex 22: No swaps needed.
  • To maximize the weight of vertex 33: Swap the weights of vertices 33 and 44.
  • To maximize the weight of vertex 44: No swaps needed.
  • To maximize the weight of vertex 55: Swap the weights of vertices 44 and 55.
  • To maximize the weight of vertex 66: No swaps needed.

Data Range

For all data, it is guaranteed that $1\le n,m\le 5\times10^5,1\le p_i\le10^9,1\le u,v\le n$. No guarantee of no duplicate edges or self-loops.

Note that this problem requires bundled testing.

  • Subtask 1 (5pts): n,m3n,m\le3.
  • Subtask 2 (25pts): n,m103n,m\le10^3.
  • Subtask 3 (8pts): The graph is a chain. That is, for all i=1,2,,n1i=1,2,\dots,n-1, there is exactly one directed edge between ii and i+1i+1, but the direction is uncertain.
  • Subtask 4 (12pts): The graph is a tree. That is, m=n1m=n-1, and After converting the graph into an undirected graph, it becomes connected.
  • Subtask 5 (20pts): n,m5×104n,m\le5\times10^4, and the graph is randomly generated. The random generation method is described below.
  • Subtask 6 (10pts): n,m105n,m\le10^5.
  • Subtask 7 (20pts): n,m5×105n,m\le5\times10^5.

Random generation method for Subtask 5:

  • First, determine n,mn,m and the sequence pp (not necessarily random).
  • Then, for each of the mm edges, select u,vu,v uniformly at random from the integers 11 to nn.

Note that this problem requires efficient input and output methods, and during implementation, attention should be paid to the impact of constants on program efficiency.