CF2249F.Even Simple Path

NOI/NOI+/CTSC

通过率:0%

时间限制:2.50s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a simple undirected graph with nn vertices and mm edges.

A path is simple if it visits no vertex more than once. The length of a path is the number of edges in it.

Find a shortest simple path with even length from vertex 11 to vertex nn, or determine that no such path exists.

给你一个包含 nn 个顶点和 mm 条边的简单无向图。

一条路径称为简单路径,如果它不重复访问任何顶点。路径的长度定义为该路径中边的数量。

请找出一条从顶点 11 到顶点 nn 的最短简单路径,且其长度为偶数;若不存在这样的路径,则判定其不存在。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (2≤n≤10002\le n\le 1000, 0≤m≤n(n−1)20\le m\le \frac{n(n-1)}2) — the number of vertices and the number of edges in the graph.

Then mm lines follow, the ii-th line containing two integers uiu_i and viv_i (1≤ui,vi≤n1\le u_i,v_i\le n, ui≠viu_i\ne v_i) — the two vertices that the ii-th edge connects.

It is guaranteed that there are no self-loops or multiple edges in the graph.

It is guaranteed that the sum of n3n^3 over all test cases does not exceed 100031000^3.

It is guaranteed that the sum of mm over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤10002\le n\le 1000,0≤m≤n(n−1)20\le m\le \frac{n(n-1)}2)—— 分别表示图中的顶点数和边数。

接下来是 mm 行,其中第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1\le u_i,v_i\le n,ui≠viu_i\ne v_i)—— 表示第 ii 条边所连接的两个顶点。

保证图中不存在自环或重边。

保证所有测试用例的 n3n^3 之和不超过 100031000^3。

保证所有测试用例的 mm 之和不超过 10610^6。

输出格式

For each test case, if no such path exists, print −1-1.

Otherwise, print any shortest simple path of even length from vertex 11 to vertex nn:

  • On the first line, print its length kk;
  • On the second line, print k+1k+1 vertices p0,p1,…,pkp_0,p_1,\ldots,p_k in order, where p0=1p_0=1 and pk=np_k=n.

If there are multiple possible answers, you may print any of them.

对于每个测试用例,若不存在这样的路径,则输出 −1-1。

否则,输出任意一条从顶点 11 到顶点 nn 的最短简单路径,且该路径长度为偶数:

  • 第一行输出其长度 kk;
  • 第二行按顺序输出 k+1k+1 个顶点 p0,p1,…,pkp_0,p_1,\ldots,p_k,其中 p0=1p_0=1 且 pk=np_k=n。

若存在多种可能的答案,输出任意一种即可。

输入输出样例

  • 输入#1

    5
    2 0
    3 2
    1 2
    2 3
    4 3
    1 2
    2 3
    3 4
    5 4
    1 5
    1 2
    2 5
    3 4
    6 7
    1 6
    1 2
    2 3
    3 4
    4 6
    2 5
    4 5

    输出#1

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

说明/提示

In the first test case, there is no path from vertex 11 to vertex 22, so there is no answer.

In the second test case, the path 1→2→31 \to 2 \to 3 has length 22, and it is the shortest simple path with even length.

In the third test case, the only simple path from vertex 11 to vertex 44 has length 33, which is not even, so there is no answer.

In the fourth test case, the path consisting only of the edge 1→51 \to 5 has length 11, which is odd and cannot be an answer. The path 1→2→51 \to 2 \to 5 has length 22.

In the fifth test case, the path 1→2→3→4→61 \to 2 \to 3 \to 4 \to 6 has length 44, while the path 1→61 \to 6 has length 11, which is not even.

在第一个测试用例中,不存在从顶点 11 到顶点 22 的路径,因此无解。

在第二个测试用例中,路径 1→2→31 \to 2 \to 3 的长度为 22,且它是长度为偶数的最短简单路径。

在第三个测试用例中,从顶点 11 到顶点 44 的唯一简单路径长度为 33,该长度为奇数,因此无解。

在第四个测试用例中,仅由边 1→51 \to 5 构成的路径长度为 11,为奇数,不能作为答案;路径 1→2→51 \to 2 \to 5 的长度为 22。

在第五个测试用例中,路径 1→2→3→4→61 \to 2 \to 3 \to 4 \to 6 的长度为 44,而路径 1→61 \to 6 的长度为 11,为奇数,不能作为答案。

输入解题思路,AI测评打分。不知道怎么写?

首页