PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000017. Nhảy trên hành tinh (Planets Queries I)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Nhảy trên hành tinh (Planets Queries I)
Planets Queries I
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
There are planets numbered to . Planet has exactly one teleporter leading to , possibly back to itself.
For each query , start at planet , follow teleporters exactly times, and report the final planet.
Planets can be revisited when teleporters form a cycle.
Input
- The first line contains .
- The second line contains integers .
- The next lines contain .
Output
Print the destination planet index on its own line for each query.
Subtasks
- Subtask 1 (20 points): ; ; ; ; .
- Subtask 2 (30 points): ; ; ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input
4 3
2 1 1 4
1 2
3 4
4 1
Output
1
2
4
Explanation
From planet , two teleports follow , returning to . Four teleports from planet end at planet .