eolymp
bolt
Try our new interface for solving problems
Məsələlər

Queries on Graph

Queries on Graph

After previous \textbf{2} problems, you see that our hero likes problems on queries. He is presenting to you a new problem. It is like a shortest-path problem on dynamic graph. You are given \textbf{N} nodes. At each query you add an edge between \textbf{2} nodes (bidirectional edge) or you have to calculate a distance between \textbf{2} nodes. Assume that every time graph doesn’t contain any loops and cycles. 1) Add edge between \textbf{2 }nodes and the length of this edge is \textbf{1}. This query is represented as “\textbf{1 i j}” 2) Output the distance between \textbf{i}-th and \textbf{j}-th node, if there is no path between those nodes output \textbf{-1}. This query is represented as “\textbf{2 i j}” \InputFile You are given an integer \textbf{N} (\textbf{1} ≤ \textbf{N} ≤ \textbf{100000}) - the number of nodes and \textbf{Q} (\textbf{1} ≤ \textbf{Q} ≤ \textbf{100000}) - the number of queries. The next \textbf{Q} lines contain one query each, of one of the above forms. \OutputFile For each query of the type “\textbf{2 i j}” print the distance between \textbf{i}-th and \textbf{j}-th node. If there is no path between those nodes output \textbf{-1}.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
5 6
1 1 2
1 2 3
2 1 3
2 1 5
1 2 5
2 1 5
Çıxış verilənləri #1
2
-1
2
Müəllif Mahammad Valiyev
Mənbə Local Contest #2 Qafqaz University by Mahammad Valiyev