#DSU0000063. Chiếc thang của Takahashi (Ladder Takahashi)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chiếc thang của Takahashi (Ladder Takahashi)
Ladder Takahashi
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
A building has floors numbered by positive integers starting at floor ; floor numbers can be very large. Takahashi starts on floor and wishes to reach the highest possible floor.
There are ladders. Ladder directly connects floors and , and may be used in either direction. Takahashi may use a ladder multiple times and change ladders whenever they share a floor. Apart from the listed ladders, there is no way to move between different floors.
Determine the largest floor number reachable from floor . If Takahashi cannot leave his starting floor, the answer is .
Input
The first line contains ; each of the next lines contains .
Output
Print the highest reachable floor.
Subtasks
- Subtask 1 (20 points): ; , .
- Subtask 2 (30 points): ; , .
- Subtask 3 (50 points): ; , .
Examples
Input
4
1 4
4 3
4 10
8 3
Output
10
Explanation
From floor 1, the ladders lead to floor 4 and then floor 10. No reachable floor exceeds 10, so the answer is 10.