Follow The Prize
시간 제한7초메모리 제한2048 MB
숨겨진 상품의 위치를 m번의 위치 교환에 따라 추적하고 마지막 위치를 출력한다.
문제
A total of cups were placed upside-down on a table, a prize was placed under one of them, and then the cups were shuffled. Luckily, you know under which cup the prize began, and how the cups were shuffled. Can you determine the final position of the prize?
A cup (and the prize which it might cover) can only be in one of distinct positions at any given time: . No two cups can be in the same position at any given time.
The cups are shuffled by performing a series of swaps. A swap is defined as exchanging the location of two cups. For example if the cups in positions and were swapped, the cup originally in position would end in position , and the cup originally in position would end in position . If the prize were originally under cup , this swap would have moved the prize to position .
입력
The first line is , an integer between and , inclusive, defining the number of cups.
The second line is an integer between and , inclusive, which defines the initial position of the prize.
The third line is , a non-negative integer less than or equal to , defining the number of swaps performed to shuffle the cups.
The final lines report the swaps that were performed, in order. Each line consists of two different space-separated integers, between and , defining the positions of the cups that were exchanged for that swap.
출력
The output is a single integer between and , which is the position of the prize after all of the swaps are performed.