#5411. Problem 3. The Chase

0

Problem 3. The Chase

Problem 3. The Chase

USACO 2026 Second Contest, Gold

Bessie is trying to evade the farmers. The farmers own NN (2N51052 \le N \le 5 \cdot 10^5) farms with a one way road between the ii-th farm and the aia_i-th farm (1iN1 \le i \le N, aiia_i \neq i). There are FF (1FN1 \le F \le N) farmers and the ii-th farmer is initially stationed at farm sis_i (1siN1 \le s_i \le N, all sis_i unique). At each time step, every farmer takes the road at their current farm and moves to the next. Bessie gets caught if she is ever located at the same farm as any farmer.

Suppose Bessie starts at some farm bb. At each time step, she has two options: she can either choose to rest (stay at her current farm) or take the road and move to the next farm. If she chooses to move, she moves simultaneously with the farmers. Bessie moves so that she is never caught by any farmer in a finite number of timesteps.

For each starting farm bb (1bN1 \le b \le N), find the maximum number of times Bessie can choose the rest option if she starts at farm bb.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains NN and FF, the number of farms and the number of farmers.

The second line contains a1aNa_1 \ldots a_N, the one-way roads going out of each farm.

The third line contains s1sFs_1 \ldots s_F, the starting locations of each farmer.

OUTPUT FORMAT (print output to the terminal / stdout):

Output NN lines, the bb-th of which must consist of a single integer denoting the maximum number of times Bessie can choose the rest option if she starts at farm bb. If there is no way for Bessie to ensure she is never caught after a finite number of timesteps, then output 1-1. If Bessie can rest an infinite number of times, then output 2-2.

SAMPLE INPUT:


4 1
2 1 4 3
1

SAMPLE OUTPUT:


-1
0
-2
-2

  • Farm 1: If Bessie starts at a farm with a farmer, then she is caught immediately, and you should output 1-1.
  • Farm 2: Bessie must choose to move at every timestep to avoid being caught by the farmer who starts at farm 11.
  • Farms 3-4: Bessie can rest an infinite number of times without being caught.

SCORING: Input 2: N50N \le 50 Inputs 3-10: N2000N \le 2000 Inputs 11-20: No additional constraints.

Problem credits: Alex Liang