#P13823. 「Diligent-OI R2 C」所谓伊人
「Diligent-OI R2 C」所谓伊人
Description
Given a directed graph with vertices and edges, numbered from to , with no guarantee of no duplicate edges or self-loops. Each vertex has a weight .
If there exists a path from vertex to vertex , you can swap the weights of and .
For each vertex , output the minimum number of swaps needed to maximize the weight of vertex . 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 representing the number of vertices and edges in the directed graph.
The second line contains integers .
The next lines each contain two integers representing a directed edge from vertex to vertex .
Output Format
Output a line containing the minimum number of swaps needed to maximize the weight of each vertex 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 vertices are , respectively.
- To maximize the weight of vertex : No swaps needed.
- To maximize the weight of vertex : No swaps needed.
- To maximize the weight of vertex : Swap the weights of vertices and .
- To maximize the weight of vertex : No swaps needed.
- To maximize the weight of vertex : Swap the weights of vertices and .
- To maximize the weight of vertex : 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): .
- Subtask 2 (25pts): .
- Subtask 3 (8pts): The graph is a chain. That is, for all , there is exactly one directed edge between and , but the direction is uncertain.
- Subtask 4 (12pts): The graph is a tree. That is, , and After converting the graph into an undirected graph, it becomes connected.
- Subtask 5 (20pts): , and the graph is randomly generated. The random generation method is described below.
- Subtask 6 (10pts): .
- Subtask 7 (20pts): .
Random generation method for Subtask 5:
- First, determine and the sequence (not necessarily random).
- Then, for each of the edges, select uniformly at random from the integers to .
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.
京公网安备 11011102002149号