Assigning Fares | 프로그래밍의 벗 PivotOJ
PivotOJ

Assigning Fares

시간 제한: 6000ms메모리 제한: 1024MB출처: MOOI 2019-20 finalBOJ 30694

문제

Mayor of city M. decided to launch several new metro lines during 2020. Since the city has a very limited budget, it was decided not to dig new tunnels but to use the existing underground network.

The tunnel system of the city M. consists of nn metro stations. The stations are connected with n1n - 1 bidirectional tunnels. Between every two stations vv and uu there is exactly one simple path. Each metro line the mayor wants to create is a simple path between stations aia_i and bib_i. Metro lines can intersects freely, that is, they can share common stations or even common tunnels. However, it’s not yet decided which of two directions each line will go. More precisely, between the stations aia_i and bib_i the trains will go either from aia_i to bib_i, or from bib_i to aia_i, but not simultaneously.

The city M uses complicated faring rules. Each station is assigned with a positive integer cic_i — the fare zone of the station. The cost of travel from vv to uu is defined as c_u − c_v roubles. Of course, such travel only allowed in case there is a metro line, the trains on which go from vv to uu. Mayor doesn’t want to have any travels with a negative cost, so it was decided to assign directions of metro lines and station fare zones in such a way, that fare zones are strictly increasing during any travel on any metro line.

Mayor wants firstly assign each station a fare zone and then choose a lines direction, such that all fare zones are increasing along any line. In connection with the approaching celebration of the day of the city, the mayor wants to assign fare zones so that the maximum cic_i will be as low as possible. Please help mayor to design a new assignment or determine if it’s impossible to do. Please note that you only need to assign the fare zones optimally, you don’t need to print lines’ directions. This way, you solution will be considered correct if there will be a way to assign directions of every metro line, so that the fare zones will be strictly increasing along any movement of the trains.

Please note, that in some groups it’s not required to minimize the answer, you only need to determine whether it’s possible to assign fare zones in a valid way.

입력

The first line contains an integers nn, mm, tt (2 ≤ n ≤ 500\, 000, 1 ≤ m ≤ 500\, 000, 0 ≤ t ≤ 1) — the number of stations in the city, the number of metro lines, and the parameter tt. In case t=0t = 0 you don’t have to minimize the answer. In case t=1t = 1 you should find an answer with the largest fare zone being the smallest possible.

Each of the following n1n - 1 lines describes another metro tunnel. Each tunnel is described with integers viv_i, uiu_i (1 ≤ v_i , u_i ≤ n, viuiv_i \ne u_i). It’s guaranteed, that there is exactly one simple path between any two stations.

Each of the following mm lines describes another metro line. Each line is described with integers aia_i, bib_i (1 ≤ a_i , b_i ≤ n, aibia_i \ne b_i).

출력

In the first line print integer kk — the maximum fare zone used. In case parameter t is 00, you don’t have to minimize kk. In case t=1t = 1, the kk should be the smallest possible.

In the next line print integers c1c_1, c2c_2, \dots, cnc_n (1 ≤ c_i ≤ k) — stations’ fare zones.

In case there are several possible answers, print any of them. In case it’s impossible to assign fare zones, print “-1”.

힌트

In the first example, line 131 \rightarrow 3 goes through the stations 11, 22, 33 in this order. In this order the fare zones of the stations are increasing. Since this line has 33 stations, at least three fare zones are needed. So the answer 11, 22, 33 is optimal.

In the second example, it’s impossible to assign fare zones to be consistent with a metro lines.

예제

예제 1

입력
3 1 1
1 2
2 3
1 3
출력
3
1 2 3

예제 2

입력
4 3 0
1 2
1 3
1 4
2 3
2 4
3 4
출력
-1
코드를 제출하려면 로그인하세요.