QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 1024 MB Puntuación total: 100

#10882. 旅行

Estadísticas

托伦(Toruń)有许多旅游景点。我们的导游准备了一份包含 $m$ 条单向步行路线的列表,连接着市中心的 $n$ 个集合点。路线编号为 $1$ 到 $m$,集合点编号为 $1$ 到 $n$。每条路线从一个集合点通往另一个集合点,并允许参与者在途中参观一个景点。在不同的路线上参观同一个景点是可能的,并且在同一对集合点之间可能存在多条路线。我们希望在休息日组织一次“有趣的游览”。

游览是一系列步行路线的序列,使得每条路线的起点都是前一条路线的终点。此外,最后一条路线的终点必须是第一条路线的起点。

如果游览过程中没有连续两次参观同一个景点,我们称之为“有趣的”。换句话说,游览中任意两条连续的路线所参观的景点必须不同,且第一条路线和最后一条路线所参观的景点也必须不同。注意,我们不介意非连续的路线参观同一个景点。特别地,同一条路线可以在游览中多次使用(但不能连续使用)。

你的任务是检查是否可以组织一次有趣的游览,如果可以,请找出其中一个。你可以输出任何包含最多 $m$ 条路线的有趣游览。可以证明,如果存在有趣的游览,则一定存在一个包含最多 $m$ 条路线的游览。

输入格式

第一行包含一个正整数 $t$ ($1 \le t \le 5 \cdot 10^5$),表示测试用例的数量。

每个测试用例的第一行包含两个正整数 $n$ 和 $m$ ($2 \le n, 1 \le m$),分别表示集合点的数量和路线的数量。

接下来的 $m$ 行,每行描述一条路线。第 $i$ 行包含三个正整数 $x_i, y_i$ 和 $c_i$ ($1 \le x_i, y_i \le n, x_i \neq y_i, 1 \le c_i \le m$),表示第 $i$ 条路线从集合点 $x_i$ 出发,到达集合点 $y_i$,并允许参观景点 $c_i$。

令 $N$ 和 $M$ 分别表示所有测试用例中 $n$ 和 $m$ 的总和。你可以假设 $N, M \le 10^6$。

输出格式

对于每个测试用例,如果可以组织有趣的游览,第一行输出 YES,否则输出 NO。如果是前者,第二行应首先包含一个正整数 $k$ ($2 \le k \le m$),表示构成有趣游览的路线数量。随后是 $k$ 个由空格分隔的整数 $p_1, p_2, \dots, p_k$。这些数字描述了一个有趣的游览,即我们先走路线 $p_1$,然后是 $p_2$,以此类推,最后走路线 $p_k$ 回到起始集合点。

示例中第 4 个测试用例的示意图。箭头表示集合点之间的路线。

样例

输入 1

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

输出 1

NO
YES
2 2 3
NO
YES
6 3 4 5 6 1 2
YES
4 2 4 2 3

子任务

子任务 数据范围 分值
1 $m \le 10$ 且 $t \le 100$ 9
2 $M \le 5000$ 23
3 $M \le 5 \cdot 10^4$ 19
4 $M \le 2 \cdot 10^5$ 25
5 无附加限制 24

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

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.