6761.Planets Queries II

Time Limit: 1s Memory Limit: 512MB

You are playing a game consisting of n planets. Each planet has a teleporter to another planet (or the planet itself).

You have to process $q$ queries of the form: You are now on planet $a$ and want to reach planet $b$ . What is the minimum number of teleportations?

Input Format(From the terminal/stdin)

The first input line contains two integers $n$ and $q$ : the number of planets and queries. The planets are numbered $1,2,\ldots,n$ .

The second line contains $n$ integers $t_1,t_2,\ldots,t_n$ : for each planet, the destination of the teleporter.

Finally, there are $q$ lines describing the queries. Each line has two integers $a$ and $b$ : you are now on planet $a$ and want to reach planet $b$ .

  • $1 \le n, q \le 2 \cdot 10^5$
  • $1 \le a,b \le n$

Output Format(To the terminal/stdout)

For each query, print the minimum number of teleportations. If it is not possible to reach the destination, print $-1$ .

Sample Input

Copy
5 3
2 3 2 3 2
1 2
1 3
1 4
 · \n
 · · · · \n
 · \n
 · \n
 · \n

Sample Output

Copy
1
2
-1
 \n
 \n
  \n
Source: CSES, Graph Algorithms, 1160

Submit

请先 登录

© 2025 FAQs