All Tracks Algorithms Graphs Shortest Path Algorithms Problem

Furthest vertex
Tag(s):

Medium-Hard

Problem
Editorial
Analytics

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.

SAMPLE INPUT
3 5
1 1 2
2 1
2 3
1 1 3
2 3
SAMPLE OUTPUT
1
0
2
Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Scala 2.11.8, Swift, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

November Clash '16

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications