欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1204C.Anna, Svyatoslav and Maps
Anna, Svyatoslav and Maps
The main characters have been omitted to be short.
You are given a directed unweighted graph without loops with vertexes and a path in it (that path is not necessary simple) given by a sequence of vertexes; for each there is an arc from to .
Define the sequence of vertexes as good, if is a subsequence of , , , and is one of the shortest paths passing through the vertexes , , in that order.
A sequence is a subsequence of a sequence if can be obtained from by deletion of several (possibly, zero or all) elements. It is obvious that the sequence is good but your task is to find the shortest good subsequence.
If there are multiple shortest good subsequences, output any of them.
Input
The first line contains a single integer () — the number of vertexes in a graph.
The next lines define the graph by an adjacency matrix: the -th character in the -st line is equal to if there is an arc from vertex to the vertex else it is equal to . It is guaranteed that the graph doesn't contain loops.
The next line contains a single integer () — the number of vertexes in the path.
The next line contains integers () — the sequence of vertexes in the path. It is guaranteed that for any there is an arc from to .
Output
In the first line output a single integer () — the length of the shortest good subsequence. In the second line output integers , , () — the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct.
Note
Below you can see the graph from the first example:

The given path is passing through vertexes , , , . The sequence 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 , and in that order is . Note that subsequences and aren't good because in both cases the shortest path passing through the vertexes of these sequences is .
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 and are the shortest paths passing through the vertexes and .
Samples
4
0110
0010
0001
1000
4
1 2 3 4
3
1 2 4
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
11
1 2 4 2 4 2 4 2 4 2 4
3
011
101
110
7
1 2 3 1 3 2 1
7
1 2 3 1 3 2 1
4
0110
0001
0001
1000
3
1 2 4
2
1 4
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |