#P1954. [NOI2010] 航空管制

    ID: 900 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>贪心2010NOI 系列Special Judge深度优先搜索,DFS优先队列

[NOI2010] 航空管制

Description

During the World Expo, Shanghai’s air passenger traffic greatly exceeded normal levels, and air traffic control also occurred frequently. Recently, Xiao X was delayed at the airport for more than two hours twice in a row due to air traffic control. Xiao X was very dissatisfied with this.

On the way to Yantai this time, Xiao X unfortunately encountered air traffic control again. So Xiao X began to think about the problem of air traffic control.

Suppose there are currently nn delayed flights, numbered from 11 to nn. The airport has only one takeoff runway, and all flights must take off one by one in some order (called the takeoff sequence). Define a flight’s takeoff position as its position in the takeoff sequence, i.e., the index of when it takes off.

There are two types of constraints on the takeoff sequence:

  • Type 1 (latest takeoff position limit): the takeoff position of flight ii must not exceed kik_i.
  • Type 2 (relative takeoff order constraints): there are some constraints (a,b)(a,b), meaning flight aa must take off earlier than flight bb, i.e., the takeoff position of flight aa must be less than that of flight bb.

Xiao X’s first question is: given the two types of constraints above, can we compute a feasible takeoff sequence? The second question is: considering both types of constraints, how to compute for each flight its minimal possible takeoff position among all feasible takeoff sequences.

Input Format

The first line contains two positive integers nn and mm, where nn is the number of flights and mm is the number of Type 2 (relative takeoff order) constraints.

The second line contains nn positive integers k1,k2,,knk_1,k_2,\cdots,k_n.

The next mm lines each contain two positive integers aa and bb, representing one relative takeoff order constraint (a,b)(a,b), where 1a,bn1\leq a,b\leq n, meaning flight aa must take off before flight bb.

Output Format

The first line contains nn integers representing one feasible takeoff sequence, with adjacent integers separated by single spaces. The input guarantees that at least one feasible takeoff sequence exists. If there are multiple feasible sequences, output any one of them.

The second line contains nn integers t1,t2,,tnt_1,t_2,\cdots,t_n, where tit_i is the minimal possible takeoff position of flight ii among all feasible sequences, with adjacent integers separated by single spaces.

5 5
4 5 2 5 4
1 2
3 2
5 1
3 4
3 1

3 5 1 4 2
3 4 1 2 1

5 0
3 3 3 5 5

3 2 1 5 4
1 1 1 4 4

Hint

Sample Explanation

In sample 11:

The takeoff sequence 3 5 1 4 23\ 5\ 1\ 4\ 2 satisfies all the constraints. All takeoff sequences that satisfy the constraints are:

$$\begin{aligned} 3\ 4\ 5\ 1\ 2\\ 3\ 5\ 1\ 2\ 4\\ 3\ 5\ 1\ 4\ 2\\ 3\ 5\ 4\ 1\ 2\\ 5\ 3\ 1\ 2\ 4\\ 5\ 3\ 1\ 4\ 2\\ 5\ 3\ 4\ 1\ 2 \end{aligned}$$

Because there are constraints (5,1)(5,1) and (3,1)(3,1), flight 11 can only be scheduled after flights 55 and 33, so its earliest takeoff position is 33. Other flights are similar.

In sample 22:

Although flights 4,54,5 have no relative takeoff order constraints, since flights 1,2,31,2,3 must occupy the first 33 takeoff positions, 4,54,5 can be scheduled no earlier than position 44.

Constraints

For 30%30\% testdata: n10n\leq 10.

For 60%60\% testdata: n500n\leq 500.

For 100%100\% testdata: n2×103,m104n\leq 2\times 10^3,m\leq 10^4.

Thanks to @FlierKing for providing the special judge (SPJ).

Translated by ChatGPT 5