eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

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}.
Ліміт часу 1 секунда
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
5 6
1 1 2
1 2 3
2 1 3
2 1 5
1 2 5
2 1 5
Вихідні дані #1
2
-1
2
Автор Mahammad Valiyev
Джерело Local Contest #2 Qafqaz University by Mahammad Valiyev