QOJ.ac

QOJ

Time Limit: 2.0 s Memory Limit: 256 MB Total points: 100

#17224. The Chase

统计

题目描述

Bessie 正在试图躲避农夫。农夫们拥有 $N$ ($2 \le N \le 5 \cdot 10^5$) 个农场,且在第 $i$ 个农场和第 $a_i$ 个农场之间有一条单向道路 ($1 \le i \le N$, $a_i \neq i$)。共有 $F$ ($1 \le F \le N$) 个农夫,第 $i$ 个农夫初始位于农场 $s_i$ ($1 \le s_i \le N$, 所有 $s_i$ 互不相同)。在每个时间步,每个农夫都会沿着当前农场的道路移动到下一个农场。如果 Bessie 在任何时刻与任何农夫位于同一个农场,她就会被抓住。

假设 Bessie 从某个农场 $b$ 出发。在每个时间步,她有两种选择:休息(留在当前农场)或沿着道路移动到下一个农场。如果她选择移动,她将与农夫同时移动。Bessie 的移动方式需确保她在有限的时间步内永远不会被任何农夫抓住。

对于每个起始农场 $b$ ($1 \le b \le N$),求出如果 Bessie 从农场 $b$ 出发,她最多可以选择休息的次数。

输入格式

第一行包含 $N$ 和 $F$,分别表示农场的数量和农夫的数量。

第二行包含 $a_1 \ldots a_N$,表示从每个农场出发的单向道路。

第三行包含 $s_1 \ldots s_F$,表示每个农夫的初始位置。

输出格式

输出 $N$ 行,第 $b$ 行包含一个整数,表示如果 Bessie 从农场 $b$ 出发,她最多可以选择休息的次数。如果 Bessie 无法确保在有限的时间步后永远不被抓住,则输出 $-1$。如果 Bessie 可以无限次休息,则输出 $-2$。

样例

输入 1

4 1
2 1 4 3
1

输出 1

-1
0
-2
-2

说明 1

  • 农场 1:如果 Bessie 从一个有农夫的农场出发,她会立即被抓住,此时应输出 $-1$。
  • 农场 2:Bessie 必须在每个时间步都选择移动,以避免被从农场 1 出发的农夫抓住。
  • 农场 3-4:Bessie 可以无限次休息而不被抓住。

子任务

  • 输入 2:$N \le 50$
  • 输入 3-10:$N \le 2000$
  • 输入 11-20:无额外限制。

Problem credits: Alex Liang

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.