Furthest vertex
Graphs
Medium
Shortest path algorithm
90.0% success
790 attempts
1s time
256MB memory
1024KB max code

There are n vertices.

Your task is to perform q queries:

1 a b - add an undirected edge of the length 1 between vertices a and b

2 a - find the distance to the furthest vertex reachable from the vertex a

Guaranteed that graph will not contain loops and cycles during all the time.

\(INPUT\)

First line of the input contains two integers n and q \((1 \leq n \leq 10^5, 1 \leq q \leq 3 \cdot 10^5)\)

Then follow q lines. The i-th of them contains integers: \(type_i\) \((1 \leq type_i \leq 2)\), \(a_i\) \((1 \leq a \leq n)\) and if \(type_i = 1\) \(b_i\) \((1 \leq b_i \leq n)\).

\(OUTPUT\)

Print the distance which is needed for each query of the second type.

Examples
Input
3 5
1 1 2
2 1
2 3
1 1 3
2 3
Output
1
0
2

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading Editor...