#abc277c. C - Ladder Takahashi

    ID: 3822 传统题 1000ms 256MiB 尝试: 98 已通过: 29 难度: 6 上传者: 标签>来源atcoder基础算法BFS高级算法并查集其他离散化图的遍历DFS

C - Ladder Takahashi

Score : 300300 points

问题描述

存在一座高度为 10910^9 层的大楼,其中共有 NN 条梯子。
Takahashi 正位于第 11 层(最低层),他希望通过使用梯子(可能不使用任何梯子)到达尽可能高的楼层。
这些梯子从 11NN 编号,其中第 ii 条梯子连接着第 AiA_i 层和第 BiB_i 层。可以双向使用第 ii 条梯子,在第 AiA_i 层与第 BiB_i 层之间移动,但不能在其他楼层之间直接移动。
Takahashi 可以在同一楼层内自由移动,但在不借助梯子的情况下无法在不同楼层之间移动。
请问 Takahashi 最多能到达哪一层楼?

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a 10910^9-story building with NN ladders.
Takahashi, who is on the 11-st (lowest) floor, wants to reach the highest floor possible by using ladders (possibly none).
The ladders are numbered from 11 to NN, and ladder ii connects the AiA_i-th and BiB_i-th floors. One can use ladder ii in either direction to move from the AiA_i-th floor to the BiB_i-th floor or vice versa, but not between other floors.
Takahashi can freely move within the same floor, but cannot move between floors without using ladders.
What is the highest floor Takahashi can reach?

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1Ai,Bi1091 \leq A_i, B_i \leq 10^9
  • AiBiA_i \neq B_i
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 B1B_1

A2A_2 B2B_2

\ldots

ANA_N BNB_N

Output

Print an integer representing the answer.

Sample Input 1

4
1 4
4 3
4 10
8 3

Sample Output 1

10

He can reach the 1010-th floor by using ladder 11 to get to the 44-th floor and then ladder 33 to get to the 1010-th floor.

Sample Input 2

6
1 3
1 5
1 12
3 5
3 12
5 12

Sample Output 2

12

Sample Input 3

3
500000000 600000000
600000000 700000000
700000000 800000000

Sample Output 3

1

He may be unable to move between floors.

update @ 2024/3/10 11:39:21