PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000023. Số bước dịch chuyển đến đích (Planets Queries II)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Số bước dịch chuyển đến đích (Planets Queries II)
Planets Queries II
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
There are planets indexed from to . Each planet has one directed teleporter to .
For each query , start at and try to reach by following available teleporters. You may take any number of teleports, but cannot reverse them.
Find the minimum number of teleports needed to reach , or -1 if it is impossible. When , the answer is .
Input
- The first line contains .
- The second line contains integers .
- The next lines contain two planet indices .
Output
Print one result per line, or -1 if the destination is unreachable.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; .
Examples
Input
5 3
2 3 2 3 2
1 2
1 3
1 4
Output
1
2
-1
Explanation
From planet , planet is one teleport away and planet two teleports away. Planet cannot be reached by following teleporters from .