HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Sections > Linear Data Structures: Arrays > problem:


50822 - Linked List

Guest
• Review clarifications (1)

Section problems

• 50431 - Sultan's Game
• 50420 - Teachers Sightseeing
• 50977 - Gaussian Elimination
• 50395 - Page "Like" Averages
• 50516 - Lines
• 50446 - Snake
• 50345 - Orders
• 51127 - Maze solver
• 50822 - Linked List
• 51126 - Dr. Fllanxha
• 50529 - Row to Table
• 50499 - Table to Row
• 50823 - Secret Number
• 50853 - Parking Place
• 50990 - Two Neighbors
• 50496 - Falling Bricks
• 51021 - Number of Nodes

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 2000/4000/4000/4000 ms. Memory limit 65000/65000/65000/65000 Kb.
Question by Ibrahim Mesecan.

Linked List

You are given an integer list. Then you are given another integer list which shows the links between the numbers. The numbers in the first list show the original list. The second list shows the links. For example, according to the example on the left, the first number is the 5th number. Then, 5th number is linked to the second number, etc.

Question: Write a program that is going to get information for n numbers and prints the sum of pth and qth visited numbers in the linked list.

Input specification
In the first line you will be given three integers:

  • the number of numbers (n) and
  • order of the numbers (p and q) we are searching for
Then in the following line you will be given n numbers (links of the numbers) And in the last line you will be given n numbers where 0 ≤ n ≤ 20,000; 1 ≤ (p and q) ≤ n; and each number in the list are between -3e4 and 3e4.
Note: Assume that number positions are from 1 to n.

Output specification:
Show just one number: sum of pth and qth numbers.

Sample Input I
5 4 3
5 4 1 3 2
9 5 8 6 2
Sample Output I
14

Explanation: The numbers start with

  1. the 5th number: 2
  2. The fifth number is linked to the second number: 5
  3. The second number is linked to the 4th number: 6 (qth visited number)
  4. And it’s linked to the 3rd number: 8 (pth visited number)
So, the third and fourth numbers are 6 and 8, and the sum is 14.



Для отправки решений необходимо выполнить вход.

www.contester.ru