102233: [AtCoder]ABC223 D - Restricted Permutation

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:6 Solved:0

Description

Score : $400$ points

Problem Statement

Among the sequences $P$ that are permutations of $(1, 2, \dots, N)$ and satisfy the condition below, find the lexicographically smallest sequence.

  • For each $i = 1, \dots, M$, $A_i$ appears earlier than $B_i$ in $P$.

If there is no such $P$, print -1.

Constraints

  • $2 \leq N \leq 2 \times 10^5$
  • $1 \leq M \leq 2 \times 10^5$
  • $1 \leq A_i, B_i \leq N$
  • $A_i \neq B_i$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$ $M$
$A_1$ $B_1$
$\vdots$
$A_M$ $B_M$

Output

Print the answer.


Sample Input 1

4 3
2 1
3 4
2 4

Sample Output 1

2 1 3 4

The following five permutations $P$ satisfy the condition: $(2, 1, 3, 4), (2, 3, 1, 4), (2, 3, 4, 1), (3, 2, 1, 4), (3, 2, 4, 1)$. The lexicographically smallest among them is $(2, 1, 3, 4)$.


Sample Input 2

2 3
1 2
1 2
2 1

Sample Output 2

-1

No permutations $P$ satisfy the condition.

Input

题意翻译

将 $( 1,2,\dots,n )$ 重新排列后得到一个数列 $P$,满足对于 $\forall i\in [1,M]$,$P$ 中的 $A_i$ 要出现在 $B_i$ 之前。在此前提下要求 $P$ 的字典序最小。如果不存在这样的 $P$,请输出 $-1$。

Output

得分:400分

问题描述

在满足以下条件的序列$P$中,找到字典序最小的序列。

  • 对于每个$i = 1, \dots, M$,$A_i$在$P$中出现在$B_i$之前。

如果没有这样的$P$,打印-1

约束

  • $2 \leq N \leq 2 \times 10^5$
  • $1 \leq M \leq 2 \times 10^5$
  • $1 \leq A_i, B_i \leq N$
  • $A_i \neq B_i$
  • 输入中的所有值都是整数。

输入

从标准输入以以下格式获取输入:

$N$ $M$
$A_1$ $B_1$
$\vdots$
$A_M$ $B_M$

输出

打印答案。


样例输入1

4 3
2 1
3 4
2 4

样例输出1

2 1 3 4

满足条件的序列$P$有以下五个:$(2, 1, 3, 4), (2, 3, 1, 4), (2, 3, 4, 1), (3, 2, 1, 4), (3, 2, 4, 1)$。它们中字典序最小的是$(2, 1, 3, 4)$。


样例输入2

2 3
1 2
1 2
2 1

样例输出2

-1

没有满足条件的序列$P$。

加入题单

算法标签: