atcoder#ABC304H. [ABC304Ex] Constrained Topological Sort

[ABC304Ex] Constrained Topological Sort

Score : 625625 points

Problem Statement

You are given a directed graph with NN vertices and MM edges. For i=1,2,,Mi = 1, 2, \ldots, M, the ii-th edge is directed from vertex sis_i to vertex tit_i.

Determine whether there is a permutation P=(P1,P2,,PN)P = (P_1, P_2, \ldots, P_N) of (1,2,,N)(1, 2, \ldots, N) that satisfies both of the following conditions, and if there is, provide an example.

  • Psi<PtiP_{s_i} \lt P_{t_i} for all i=1,2,,Mi = 1, 2, \ldots, M.
  • LiPiRiL_i \leq P_i \leq R_i for all i=1,2,,Ni = 1, 2, \ldots, N.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • $0 \leq M \leq \min\lbrace 4 \times 10^5, N(N-1) \rbrace$
  • 1si,tiN1 \leq s_i, t_i \leq N
  • sitis_i \neq t_i
  • ij    (si,ti)(sj,tj)i \neq j \implies (s_i, t_i) \neq (s_j, t_j)
  • 1LiRiN1 \leq L_i \leq R_i \leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN MM

s1s_1 t1t_1

s2s_2 t2t_2

\vdots

sMs_M tMt_M

L1L_1 R1R_1

L2L_2 R2R_2

\vdots

LNL_N RNR_N

Output

If there is no PP that satisfies the conditions, print No. If there is a PP that satisfies the conditions, print Yes in the first line, and the elements of PP separated by spaces in the second line, in the following format. If multiple PP's satisfy the conditions, any of them will be accepted.

Yes

P1P_1 P2P_2 \ldots PNP_N

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

P=(3,1,4,2,5)P = (3, 1, 4, 2, 5) satisfies the conditions. In fact,

  • for the first edge (s1,t1)=(1,5)(s_1, t_1) = (1, 5), we have P1=3<5=P5P_1 = 3 \lt 5 = P_5;
  • for the second edge (s2,t2)=(2,1)(s_2, t_2) = (2, 1), we have P2=1<3=P1P_2 = 1 \lt 3 = P_1;
  • for the third edge (s3,t3)=(2,5)(s_3, t_3) = (2, 5), we have P2=1<5=P5P_2 = 1 \lt 5 = P_5;
  • for the fourth edge (s4,t4)=(4,3)(s_4, t_4) = (4, 3), we have P4=2<4=P3P_4 = 2 \lt 4 = P_3.

Also,

  • L1=1P1=3R1=5L_1 = 1 \leq P_1 = 3 \leq R_1 = 5,
  • L2=1P2=1R2=3L_2 = 1 \leq P_2 = 1 \leq R_2 = 3,
  • L3=3P3=4R3=4L_3 = 3 \leq P_3 = 4 \leq R_3 = 4,
  • L4=1P4=2R4=3L_4 = 1 \leq P_4 = 2 \leq R_4 = 3,
  • L5=4P5=5R5=5L_5 = 4 \leq P_5 = 5 \leq R_5 = 5.
2 2
1 2
2 1
1 2
1 2
No

No PP satisfies the conditions, so print No.