#P12562. [UTS 2024] Two Trees

[UTS 2024] Two Trees

题目描述

You are given two equal undirected trees GG and HH consisting of nn vertices. Trees are equal if, for all pairs of vertices (u;v)(u;v), the edge between GuG_u and GvG_v exists if and only if the edge between HuH_u and HvH_v exists.

Some vertices can be connected to their version in another tree with an undirected edge. Especially, a leaf vertex GvG_v can be connected to vertex HvH_v. Let us say that if leaf vertex GvG_v is connected to HvH_v directly, then vertex vv is enabled, and the vertex is disabled otherwise.

Initially, all leaf vertices are disabled. You are asked to handle the following queries:

  • 11 vv --- toggle the status of vertex vv, if vv is disabled then change its status to enabled and vice versa otherwise;
  • 22 uu vv --- print the length of the shortest path from vertex GuG_u to vertex HvH_v.

输入格式

The first line contains two integers nn and qq (3n21053 \le n \le 2 \cdot 10^5; 1q21051 \le q \le 2 \cdot 10^5) --- number of vertices and queries, respectively.

Each of the following n1n-1 lines contains two integers uu, vv (1u,vn)(1 \le u,v \le n) --- description of trees, edges (Gu;Gv)(G_u;G_v) and (Hu;Hv)(H_u;H_v).

Each of the following qq lines contains a description of queries. There are two possible query descriptions:

  • 11 vv (1vn)(1 \le v \le n) --- toggle the status of vertex vv;
  • 22 uu vv (1u,vn)(1 \le u,v \le n) --- print the length of the shortest path from vertex GuG_u to HvH_v.

输出格式

For each query of the second type, print a single integer in a single line --- the length of the shortest path between given vertices. If there is no such path, print -1\texttt{-1}.

7 11
1 2
2 3
1 4
2 5
4 6
4 7
1 6
1 3
2 1 6
2 1 2
1 7
2 5 4
2 6 3
1 6
1 3
1 5
2 6 3
2
3
5
4
6
3 2
1 2
1 3
1 3
2 1 2
3

提示

Let kk be the number of queries of the second type.

  • (33 points): n16n \le 16, q16q \le 16;
  • (33 points): n16n \le 16, q2105q \le 2 \cdot 10^5;
  • (22 points): n2000n \le 2000, q2105,k5q \le 2 \cdot 10^5,k \le 5;
  • (88 points): n2000n \le 2000, q2000q \le 2000;
  • (99 points): n2105n \le 2 \cdot 10^5, q2105,k5q \le 2 \cdot 10^5, k \le 5;
  • (3030 points): n2105n \le 2 \cdot 10^5, q2105q \le 2 \cdot 10^5, there is no query of first type after query of second type;
  • (4545 points): no additional restrictions.