CF1481D.AB Graph

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.

You are given a complete directed graph with nn vertices without self-loops. In other words, you have nn vertices and each pair of vertices uu and vv (u≠vu \neq v) has both directed edges (u,v)(u, v) and (v,u)(v, u).

Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges (u,v)(u, v) and (v,u)(v, u) may have different labels).

You are also given an integer m>0m \gt 0. You should find a path of length mm such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.

You can visit the same vertex and the same directed edge any number of times.

你的朋友萨勒姆是瓦拉维赫的兄弟,他只喜欢数学和几何问题。他已解决过大量此类问题,但据瓦拉维赫所说,为了从大学毕业,他必须再多解一些图论问题。由于萨勒姆不擅长图论,他向你求助以下问题。

给你一个含 nn 个顶点的完全有向图,且图中不含自环。换言之,你拥有 nn 个顶点,且对任意两个不同顶点 uu 和 vv(即 u≠vu \neq v),均存在两条有向边 (u,v)(u, v) 和 (v,u)(v, u)。

图中每条有向边均被标记为单个字符:要么是 'a',要么是 'b'(注意:边 (u,v)(u, v) 与 (v,u)(v, u) 的标签可以不同)。

你还给定一个正整数 m>0m > 0。你需要找出一条长度为 mm 的路径,使得沿该路径遍历时所经过的各边标签按顺序拼接而成的字符串是一个回文串。此处路径的长度定义为路径中所含边的数量。

你可以重复访问同一顶点,也可以重复经过同一条有向边任意多次。

输入格式

The first line contains a single integer tt (1≤t≤5001 \le t \le 500) — the number of test cases.

The first line of each test case contains two integers nn and mm (2≤n≤10002 \leq n \leq 1000; 1≤m≤1051 \leq m \leq 10^{5}) — the number of vertices in the graph and desirable length of the palindrome.

Each of the next nn lines contains nn characters. The jj-th character of the ii-th line describes the character on the edge that is going from node ii to node jj.

Every character is either 'a' or 'b' if i≠ji \neq j, or '*' if i=ji = j, since the graph doesn't contain self-loops.

It's guaranteed that the sum of nn over test cases doesn't exceed 10001000 and the sum of mm doesn't exceed 10510^5.

第一行包含一个整数 tt(1≤t≤5001 \le t \le 500)—— 测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤10002 \leq n \leq 1000;1≤m≤1051 \leq m \leq 10^{5})—— 图中顶点的数量以及所期望的回文串长度。

接下来的 nn 行,每行包含 nn 个字符。第 ii 行的第 jj 个字符表示从节点 ii 指向节点 jj 的边上的字符。

每个字符要么是 'a' 或 'b'(当 i≠ji \neq j 时),要么是 '*'(当 i=ji = j 时),因为图中不含自环。

保证所有测试用例的 nn 之和不超过 10001000,且所有测试用例的 mm 之和不超过 10510^5。

输出格式

For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of m+1m + 1 integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.

Otherwise, (if there is no answer) print "NO".

对于每个测试用例,若存在满足条件的路径,则输出 "YES",并紧接着输出该路径本身:一个由 m+1m + 1 个整数组成的序列,表示路径中各顶点的下标(按正确顺序排列)。若存在多个合法路径,输出任意一个即可。

否则(即不存在满足条件的路径),输出 "NO"。

输入输出样例

  • 输入#1

    5
    3 1
    *ba
    b*b
    ab*
    3 3
    *ba
    b*b
    ab*
    3 4
    *ba
    b*b
    ab*
    4 6
    *aaa
    b*ba
    ab*a
    bba*
    2 6
    *a
    b*

    输出#1

    YES
    1 2
    YES
    2 1 3 2
    YES
    1 3 1 3 1
    YES
    1 2 1 3 4 1 4
    NO

说明/提示

The graph from the first three test cases is shown below:

In the first test case, the answer sequence is [1,2][1,2] which means that the path is:

1xrightarrowtextb21 \\xrightarrow{\\text{b}} 2

So the string that is obtained by the given path is b.

In the second test case, the answer sequence is [2,1,3,2][2,1,3,2] which means that the path is:

2xrightarrowtextb1xrightarrowtexta3xrightarrowtextb22 \\xrightarrow{\\text{b}} 1 \\xrightarrow{\\text{a}} 3 \\xrightarrow{\\text{b}} 2

So the string that is obtained by the given path is bab.

In the third test case, the answer sequence is [1,3,1,3,1][1,3,1,3,1] which means that the path is:

1xrightarrowtexta3xrightarrowtexta1xrightarrowtexta3xrightarrowtexta11 \\xrightarrow{\\text{a}} 3 \\xrightarrow{\\text{a}} 1 \\xrightarrow{\\text{a}} 3 \\xrightarrow{\\text{a}} 1

So the string that is obtained by the given path is aaaa.

The string obtained in the fourth test case is abaaba.

前三个测试用例对应的图如下所示:

在第一个测试用例中,答案序列为 [1,2][1,2],表示路径为:

1xrightarrowtextb21 \\xrightarrow{\\text{b}} 2

因此,该路径所生成的字符串为 b。

在第二个测试用例中,答案序列为 [2,1,3,2][2,1,3,2],表示路径为:

2xrightarrowtextb1xrightarrowtexta3xrightarrowtextb22 \\xrightarrow{\\text{b}} 1 \\xrightarrow{\\text{a}} 3 \\xrightarrow{\\text{b}} 2

因此,该路径所生成的字符串为 bab。

在第三个测试用例中,答案序列为 [1,3,1,3,1][1,3,1,3,1],表示路径为:

1xrightarrowtexta3xrightarrowtexta1xrightarrowtexta3xrightarrowtexta11 \\xrightarrow{\\text{a}} 3 \\xrightarrow{\\text{a}} 1 \\xrightarrow{\\text{a}} 3 \\xrightarrow{\\text{a}} 1

因此,该路径所生成的字符串为 aaaa。

第四个测试用例中生成的字符串为 abaaba。

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

首页