Score : 625 points
Problem Statement
You are given a directed graph with N vertices and M edges.
For i=1,2,…,M, the i-th edge is directed from vertex si to vertex ti.
Determine whether there is a permutation P=(P1,P2,…,PN) of (1,2,…,N) that satisfies both of the following conditions, and if there is, provide an example.
- Psi<Pti for all i=1,2,…,M.
- Li≤Pi≤Ri for all i=1,2,…,N.
Constraints
- 2≤N≤2×105
- $0 \leq M \leq \min\lbrace 4 \times 10^5, N(N-1) \rbrace$
- 1≤si,ti≤N
- si=ti
- i=j⟹(si,ti)=(sj,tj)
- 1≤Li≤Ri≤N
- All input values are integers.
The input is given from Standard Input in the following format:
N M
s1 t1
s2 t2
⋮
sM tM
L1 R1
L2 R2
⋮
LN RN
Output
If there is no P that satisfies the conditions, print No
. If there is a P that satisfies the conditions, print Yes
in the first line, and the elements of P separated by spaces in the second line, in the following format.
If multiple P's satisfy the conditions, any of them will be accepted.
Yes
P1 P2 … PN
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) satisfies the conditions. In fact,
- for the first edge (s1,t1)=(1,5), we have P1=3<5=P5;
- for the second edge (s2,t2)=(2,1), we have P2=1<3=P1;
- for the third edge (s3,t3)=(2,5), we have P2=1<5=P5;
- for the fourth edge (s4,t4)=(4,3), we have P4=2<4=P3.
Also,
- L1=1≤P1=3≤R1=5,
- L2=1≤P2=1≤R2=3,
- L3=3≤P3=4≤R3=4,
- L4=1≤P4=2≤R4=3,
- L5=4≤P5=5≤R5=5.
2 2
1 2
2 1
1 2
1 2
No
No P satisfies the conditions, so print No
.