You are given a forest on $$$n$$$ vertices, initially without edges.
You are also given $$$q$$$ queries of three types:
It is guaranteed that integers $$$i$$$ are distinct and that the graph remains a forest.
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n,q \le 2\cdot 10^5$$$) — the number of vertices and the number of queries, respectively.
The $$$j$$$-th of the following $$$q$$$ lines describes a query according to the following principle:
First, a single string $$$type$$$ is given ($$$type \in$$$ {link, cut, get_path}) — the type of the query.
If $$$type$$$ is link, then two integers $$$u,v$$$ ($$$1 \le u,v \le n$$$) follow, meaning that a new undirected edge between $$$u$$$ and $$$v$$$ is added.
If $$$type$$$ is cut, then a single integer $$$i$$$ ($$$1 \le i \lt j \le q$$$) follows — the index of the query that added the edge that is required to be removed.
If $$$type$$$ is get_path, then two integers $$$u,v$$$ ($$$1 \le u,v \le n$$$) follow — the endpoints of a queried path.
For each of the queries of type get_path, output the length of the path if it exists; otherwise, output $$$-1$$$.
9 12link 1 2link 2 3get_path 1 3cut 2get_path 1 3link 8 7link 8 9link 2 8get_path 9 3get_path 9 2link 3 1get_path 9 3
2 -1 -1 2 4
You have a dynamic graph on $$$n$$$ vertices. The graph is described by an array $$$a$$$: vertex $$$i$$$ has an edge to $$$a_i$$$. Initially, $$$a_i=i$$$ for all $$$i$$$.
You are given $$$q$$$ triples ($$$u$$$,$$$x$$$,$$$s$$$). For each of them, do the following:
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n,q \le 2\cdot 10^5$$$) — the number of vertices and the number of queries, respectively.
Each of the following $$$q$$$ lines contains three integers $$$u,x,s$$$ ($$$1 \le u,x,s \le n$$$) — parameters of a query.
For each query, output a single integer — the number of steps of the walk.
8 71 2 22 3 13 1 11 7 17 2 15 3 56 2 6
1 3 3 2 4 5 5
You are given a forest on $$$n$$$ vertices, initially without edges.
You are also given $$$q$$$ queries of three types:
It is guaranteed that integers $$$i$$$ are distinct and that the graph remains a forest.
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n,q \le 2\cdot 10^5$$$) — the number of vertices and the number of queries, respectively.
The $$$j$$$-th of the following $$$q$$$ lines describes a query according to the following principle:
First, a single string $$$type$$$ is given ($$$type \in$$$ {link, cut, lca}) — the type of the query.
If $$$type$$$ is link, then two integers $$$u,v$$$ ($$$1 \le u,v \le n$$$) follow, meaning that a new undirected edge between $$$u$$$ and $$$v$$$ is added.
If $$$type$$$ is cut, then a single integer $$$i$$$ ($$$1 \le i \lt j \le q$$$) follows — the index of the query that added the edge that is required to be removed.
If $$$type$$$ is lca, then three integers $$$u,v,r$$$ ($$$1 \le u,v,r \le n$$$) follow — the two vertices and the root.
For each of the queries of type lca, output the lowest common ancestor if $$$u,v,r$$$ are from the same tree; otherwise, output $$$-1$$$.
9 16link 1 2link 1 3link 2 5link 5 4link 5 6link 7 2link 8 6link 9 2lca 8 9 4lca 8 9 5lca 8 9 1cut 3link 6 7lca 8 9 4lca 8 9 5lca 8 9 1
5 5 2 6 6 2
You are given a forest on $$$n$$$ blue vertices, initially without edges.
You are also given $$$q$$$ queries of six types:
It is guaranteed that integers $$$i$$$ are distinct and that the graph remains a forest.
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n,q \le 2\cdot 10^5$$$) — the number of vertices and the number of queries, respectively.
The $$$j$$$-th of the following $$$q$$$ lines describes a query according to the following principle:
First, a single string $$$type$$$ is given ($$$type \in$$$ {link, cut, make_blue, make_red, get_blue, get_red}) — the type of the query.
If $$$type$$$ is link, then three integers $$$u,v,w$$$ ($$$1 \le u,v \le n$$$, $$$1 \le w \le 10^9$$$) follow, meaning that a new undirected edge with weight $$$w$$$ is added between $$$u$$$ and $$$v$$$.
If $$$type$$$ is cut, then a single integer $$$i$$$ ($$$1 \le i \lt j \le q$$$) follows — the index of the query that added the edge that is required to be removed.
If $$$type$$$ is make_blue, then a single integer $$$u$$$ ($$$1 \le u \le n$$$) follows — the vertex to be colored blue.
If $$$type$$$ is make_red, then a single integer $$$u$$$ ($$$1 \le u \le n$$$) follows — the vertex to be colored red.
If $$$type$$$ is get_blue, then a single integer $$$u$$$ ($$$1 \le u \le n$$$) follows — the vertex to find the shortest path to a blue vertex from.
If $$$type$$$ is get_red, then a single integer $$$u$$$ ($$$1 \le u \le n$$$) follows — the vertex to find the shortest path to a red vertex from.
For each of the queries of type get_blue or get_red, output the length of the shortest path from $$$u$$$ to a blue or a red vertex, respectively.
9 19link 1 2 1link 1 3 1link 2 5 3link 5 4 1link 5 6 1link 7 2 1link 8 6 1link 9 2 1make_red 3get_red 8make_red 7get_red 8cut 3get_red 5make_red 4make_red 6get_blue 5make_red 5get_blue 5
7 6 -1 0 2