QOJ.ac

QOJ

时间限制: 1 s 内存限制: 256 MB 总分: 100

#5527. 交换之王

统计

你有一个用于处理 $1$ 到 $n$ 的整数排列 $p_1, p_2, \dots, p_n$ 的设备。它能执行 $m$ 种操作。第 $i$ 种操作由两个整数 $a_i, b_i$ 描述($1 \le a_i, b_i \le n, a_i \neq b_i$)。如果你执行该操作,它将执行以下动作:如果 $p_{a_i} > p_{b_i}$,则设备会交换 $p_{a_i}$ 和 $p_{b_i}$。否则,它什么也不做。

你可以以任意顺序、任意次数地应用这些操作(你可以多次使用同一种操作)。

你感兴趣的是,是否可以使用该设备从任意排列得到任意其他排列。换句话说,确定对于任意两个 $1$ 到 $n$ 的排列 $p_1, p_2, \dots, p_n$ 和 $q_1, q_2, \dots, q_n$,是否存在一个操作序列,将其应用于 $p$ 后可以得到 $q$。

回想一下,$1$ 到 $n$ 的整数排列是一个长度为 $n$ 的数组,其中包含 $1$ 到 $n$ 的每个整数恰好一次。

输入格式

第一行包含一个整数 $t$ ($1 \le t \le 10^4$),表示测试用例的数量。接下来是各测试用例的描述。

每个测试用例的第一行包含两个整数 $n, m$ ($1 \le n \le 2 \cdot 10^5, 0 \le m \le 2 \cdot 10^5$),表示设备可以处理的排列长度以及可以执行的操作数量。

接下来的 $m$ 行中,第 $i$ 行包含两个整数 $a_i, b_i$ ($1 \le a_i, b_i \le n, a_i \neq b_i$),描述第 $i$ 种操作:如果 $p_{a_i} > p_{b_i}$,设备可以交换排列中的元素 $p_{a_i}, p_{b_i}$。

保证所有对 $(a_i, b_i)$ 两两不同,但请注意,对于某些 $x, y$,对 $(x, y)$ 和 $(y, x)$ 可能同时出现。

保证所有测试用例中 $n$ 的总和不超过 $2 \cdot 10^5$,且所有测试用例中 $m$ 的总和不超过 $2 \cdot 10^5$。

输出格式

对于每个测试用例,如果可以使用该设备从任意排列得到任意其他排列,则打印 YES,否则打印 NO。

你可以以任何大小写形式打印 YES 和 NO(例如,字符串 yEs, yes, Yes 都将被视为肯定回答)。

样例

样例输入 1

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

样例输出 1

NO
YES
NO
YES
NO

说明

在第一个样例中,如果 $p_1 > p_2$,我们可以交换 $p_1$ 和 $p_2$。因此,我们可以从排列 $(1, 2)$ 得到排列 $(2, 1)$,但我们无法从排列 $(2, 1)$ 得到排列 $(1, 2)$,所以答案是 NO。

在第二个样例中,无论哪个更大,我们都可以交换 $p_1, p_2$(因为我们同时拥有 $a_i = 1, b_i = 2$ 和 $a_i = 2, b_i = 1$ 的交换操作)。同样,无论哪个更大,我们也可以交换 $p_1, p_3$。容易证明,在这种情况下,我们可以从任意排列得到任意其他排列,所以答案是 YES。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.