CF1765H.Hospital Queue

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn people (numbered from 11 to nn ) signed up for a doctor's appointment. The doctor has to choose in which order he will appoint these people. The ii -th patient should be appointed among the first pip_i people. There are also mm restrictions of the following format: the ii -th restriction is denoted by two integers (ai,bi)(a_i, b_i) and means that the patient with the index aia_i should be appointed earlier than the patient with the index bib_i .

For example, if n=4n = 4 , p=[2,3,2,4]p = [2, 3, 2, 4] , m=1m = 1 , a=[3]a = [3] and b=[1]b = [1] , then the only order of appointment of patients that does not violate the restrictions is [3,1,2,4][3, 1, 2, 4] . For n=3n =3 , p=[3,3,3]p = [3, 3, 3] , m=0m = 0 , a=[]a = [] and b=[]b = [] , any order of appointment is valid.

For each patient, calculate the minimum position in the order that they can have among all possible orderings that don't violate the restrictions.

输入格式

The first line contains two integers nn and mm ( 1n20001 \le n \le 2000 ; 0m20000 \le m \le 2000 ).

The second line contains nn integers p1,p2,,pnp_1, p_2, \dots, p_n ( 1pin1 \le p_i \le n ).

Then mm lines follow. The ii -th of them contains two integers aia_i and bib_i ( 1ai,bin1 \le a_i, b_i \le n ; aibia_i \ne b_i ). All pairs of (ai,bi)(a_i, b_i) are distinct (i. e. if iji \ne j , then either aiaja_i \ne a_j , bibjb_i \ne b_j , or both).

Additional constraint on the input: there is at least one valid order of patients.

输出格式

Print nn integers, where ii -th integer is equal to the minimum position of ii -th patient in the order, among all valid orders. Positions in the order are numbered from 11 to nn .

输入输出样例

  • 输入#1

    4 1
    2 3 2 4
    3 1

    输出#1

    2 3 1 4
  • 输入#2

    3 0
    3 3 3

    输出#2

    1 1 1
  • 输入#3

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

    输出#3

    4 2 1 1 5

说明/提示

In the first example, [3,1,2,4][3, 1, 2, 4] the only one valid order, so the minimum position of each patient is equal to their position in this order.

In the second example, any order is valid, so any patient can be appointed first.

In the third example, there are three valid orders: [4,2,3,1,5][4, 2, 3, 1, 5] , [3,4,2,1,5][3, 4, 2, 1, 5] and [4,3,2,1,5][4, 3, 2, 1, 5] .

首页