#abc313b. B - Who is Saikyo?

B - Who is Saikyo?

Score : 300300 points

问题描述

NN 名竞赛程序员,编号分别为 person 1、person 2、…、person NN

在这些程序员之间存在一种称为“优势”的关系。对于任意一对不同的程序员(person XX 和 person YY),恰好满足以下两种关系之一:“person XX 比 person YY 更强”或“person YY 比 person XX 更强”。

这种优势关系具有传递性。换句话说,对于所有三元组不重复的程序员(person XX、person YY 和 person ZZ),满足以下条件:

  • 如果 person XX 比 person YY 更强且 person YY 比 person ZZ 更强,那么 person XX 比 person ZZ 更强。

如果一名程序员 XX 对于除自己以外的所有人 YY 都更强,则称该程序员为最强程序员。(根据上述约束条件,我们可以证明总是存在唯一的一个这样的人。)

你手头有关于他们优势关系的 MM 条信息。第 ii 条信息是:“person AiA_i 比 person BiB_i 更强”。

你能根据这些信息确定这 NN 名程序员中的最强程序员吗?
如果可以,请输出该程序员的编号。否则,即如果有多个可能的最强程序员时,请输出 -1

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN competitive programmers numbered person 11, person 22, \ldots, and person NN.
There is a relation called superiority between the programmers. For all pairs of distinct programmers ((person XX, person YY)), exactly one of the following two relations holds: "person XX is stronger than person YY" or "person YY is stronger than person XX."
The superiority is transitive. In other words, for all triplets of distinct programmers ((person XX, person YY, person ZZ)), it holds that:

  • if person XX is stronger than person YY and person YY is stronger than person ZZ, then person XX is stronger than person ZZ.

A person XX is said to be the strongest programmer if person XX is stronger than person YY for all people YY other than person XX. (Under the constraints above, we can prove that there is always exactly one such person.)

You have MM pieces of information on their superiority. The ii-th of them is that "person AiA_i is stronger than person BiB_i."
Can you determine the strongest programmer among the NN based on the information?
If you can, print the person's number. Otherwise, that is, if there are multiple possible strongest programmers, print -1.

Constraints

  • 2N502 \leq N \leq 50
  • 0MN(N1)20 \leq M \leq \frac{N(N-1)}{2}
  • 1Ai,BiN1 \leq A_i, B_i \leq N
  • AiBiA_i \neq B_i
  • If iji \neq j, then (Ai,Bi)(Aj,Bj)(A_i, B_i) \neq (A_j, B_j).
  • There is at least one way to determine superiorities for all pairs of distinct programmers, that is consistent with the given information.

Input

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

NN MM

A1A_1 B1B_1

A2A_2 B2B_2

\vdots

AMA_M BMB_M

Output

If you can uniquely determine the strongest programmer, print the person's number; otherwise, print -1.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

1

You have two pieces of information: "person 11 is stronger than person 22" and "person 22 is stronger than person 33."
By the transitivity, you can also infer that "person 11 is stronger than person 33," so person 11 is the strongest programmer.

Sample Input 2

3 2
1 3
2 3

Sample Output 2

-1

Both person 11 and person 22 may be the strongest programmer. Since you cannot uniquely determine which is the strongest, you should print -1.

Sample Input 3

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

Sample Output 3

-1

update @ 2024/3/10 08:55:03