CF1204C.Anna, Svyatoslav and Maps

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The main characters have been omitted to be short.

You are given a directed unweighted graph without loops with nn vertexes and a path in it (that path is not necessary simple) given by a sequence p1,p2,…,pmp_1, p_2, \ldots, p_m of mm vertexes; for each 1≤i<m1 \leq i \lt m there is an arc from pip_i to pi+1p_{i+1}.

Define the sequence v1,v2,…,vkv_1, v_2, \ldots, v_k of kk vertexes as good, if vv is a subsequence of pp, v1=p1v_1 = p_1, vk=pmv_k = p_m, and pp is one of the shortest paths passing through the vertexes v1v_1, …\ldots, vkv_k in that order.

A sequence aa is a subsequence of a sequence bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements. It is obvious that the sequence pp is good but your task is to find the shortest good subsequence.

If there are multiple shortest good subsequences, output any of them.

为简洁起见,题目中省略了主要角色。

给定一个含 nn 个顶点的无环有向无权图,以及该图中的一条路径(该路径不一定是简单路径),由顶点序列 p1,p2,…,pmp_1, p_2, \ldots, p_m 给出;对每个 1≤i<m1 \leq i < m,均存在一条从 pip_i 指向 pi+1p_{i+1} 的有向边。

定义长度为 kk 的顶点序列 v1,v2,…,vkv_1, v_2, \ldots, v_k 为“好”的,当且仅当:

  • vv 是 pp 的一个子序列;
  • v1=p1v_1 = p_1,且 vk=pmv_k = p_m;
  • pp 是经过顶点 v1,…,vkv_1, \ldots, v_k(按此顺序)的所有路径中最短者之一。

序列 aa 是序列 bb 的子序列,指 aa 可通过从 bb 中删除若干(可能为零个或全部)元素而得到。显然,序列 pp 本身是“好”的,但你的任务是找出最短的“好”子序列。

若存在多个最短的“好”子序列,输出其中任意一个即可。

输入格式

The first line contains a single integer nn (2≤n≤1002 \le n \le 100) — the number of vertexes in a graph.

The next nn lines define the graph by an adjacency matrix: the jj-th character in the ii-st line is equal to 11 if there is an arc from vertex ii to the vertex jj else it is equal to 00. It is guaranteed that the graph doesn't contain loops.

The next line contains a single integer mm (2≤m≤1062 \le m \le 10^6) — the number of vertexes in the path.

The next line contains mm integers p1,p2,…,pmp_1, p_2, \ldots, p_m (1≤pi≤n1 \le p_i \le n) — the sequence of vertexes in the path. It is guaranteed that for any 1≤i<m1 \leq i \lt m there is an arc from pip_i to pi+1p_{i+1}.

第一行包含一个整数 nn(2≤n≤1002 \le n \le 100)—— 图中顶点的数量。

接下来的 nn 行通过邻接矩阵定义该图:第 ii 行的第 jj 个字符为 11 当且仅当存在一条从顶点 ii 指向顶点 jj 的有向边,否则为 00。保证图中不含自环(即不存在从顶点指向自身的边)。

下一行包含一个整数 mm(2≤m≤1062 \le m \le 10^6)—— 路径中顶点的数量。

再下一行包含 mm 个整数 p1,p2,…,pmp_1, p_2, \ldots, p_m(1≤pi≤n1 \le p_i \le n)—— 路径中顶点的序列。保证对任意 1≤i<m1 \leq i \lt m,均存在一条从 pip_i 指向 pi+1p_{i+1} 的有向边。

输出格式

In the first line output a single integer kk (2≤k≤m2 \leq k \leq m) — the length of the shortest good subsequence. In the second line output kk integers v1v_1, …\ldots, vkv_k (1≤vi≤n1 \leq v_i \leq n) — the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct.

第一行输出一个整数 kk(2≤k≤m2 \leq k \leq m)——最短的“好”子序列的长度。
第二行输出 kk 个整数 v1v_1, …\ldots, vkv_k(1≤vi≤n1 \leq v_i \leq n)——该子序列中的顶点。
若存在多个最短子序列,输出任意一个即可。任意两个相邻的数必须互不相同。

输入输出样例

  • 输入#1

    4
    0110
    0010
    0001
    1000
    4
    1 2 3 4

    输出#1

    3
    1 2 4
  • 输入#2

    4
    0110
    0010
    1001
    1000
    20
    1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

    输出#2

    11
    1 2 4 2 4 2 4 2 4 2 4
  • 输入#3

    3
    011
    101
    110
    7
    1 2 3 1 3 2 1

    输出#3

    7
    1 2 3 1 3 2 1
  • 输入#4

    4
    0110
    0001
    0001
    1000
    3
    1 2 4

    输出#4

    2
    1 4

说明/提示

Below you can see the graph from the first example:

The given path is passing through vertexes 11, 22, 33, 44. The sequence 1−2−41-2-4 is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes 11, 22 and 44 in that order is 1−2−3−41-2-3-4. Note that subsequences 1−41-4 and 1−3−41-3-4 aren't good because in both cases the shortest path passing through the vertexes of these sequences is 1−3−41-3-4.

In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes.

In the fourth example, the paths 1−2−41-2-4 and 1−3−41-3-4 are the shortest paths passing through the vertexes 11 and 44.

下面你可以看到第一个样例的图:

给定路径依次经过顶点 11、22、33、44。序列 1−2−41-2-4 是“好”的,因为它是给定路径的一个子序列,其首尾元素分别等于给定路径的首尾元素,且按顺序经过顶点 11、22、44 的最短路径为 1−2−3−41-2-3-4。注意:子序列 1−41-4 和 1−3−41-3-4 均不是“好”的,因为在两种情况下,经过这些序列中顶点的最短路径均为 1−3−41-3-4。

在第三个样例中,该图是完全图,因此任意一个相邻元素互不相同的顶点序列都构成一条具有相同顶点数的路径。

在第四个样例中,路径 1−2−41-2-4 和 1−3−41-3-4 均为经过顶点 11 和 44 的最短路径。

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

首页