PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000119. Đỉnh đỏ gần nhất (Xenia and Tree)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đỉnh đỏ gần nhất (Xenia and Tree)

Xenia and Tree

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected tree with nn vertices numbered from 11 to nn. Between any two vertices, there is exactly one simple path. The distance between two vertices is the number of edges on that path.

Initially, only vertex 11 is red; every other vertex is blue.

Process qq operations in the given order:

  • Type 1 v: Paint vertex vv red. The input guarantees that vv is blue when this operation is performed. Once red, a vertex remains red.
  • Type 2 v: Print the minimum distance from vv to any vertex that is currently red. If vv itself is red, the answer is 00.

A query must be answered using the coloring at its own time, without considering future updates.

Input

  • The first line contains two integers n,qn,q, the number of vertices and operations.
  • Each of the next n−1n-1 lines contains two integers u,vu,v describing an undirected edge.
  • Each of the following qq lines contains two integers t,vt,v (t∈{1,2}t\in\{1,2\}), describing an operation.

The edges form a tree, and vertices are numbered starting at 11.

Output

For each type-2 operation, print the minimum distance to a red vertex at the time of that operation, on a separate line.

If there are no type-2 operations, print nothing.

Subtasks

Every subtask satisfies all these common guarantees: 2≤n2\le n, 1≤q1\le q; 1≤u,v≤n1\le u,v\le n, u≠vu\ne v; the n−1n-1 edges form a connected, acyclic tree; t∈{1,2}t\in\{1,2\} and 1≤v≤n1\le v\le n; type-1 operations only paint currently blue vertices; initially only vertex 11 is red.

  • Subtask 1 (10 points): 2≤n≤402\le n\le40, 1≤q≤1001\le q\le100; all common guarantees apply.
  • Subtask 2 (20 points): 2≤n≤1 5002\le n\le1\,500, 1≤q≤1 5001\le q\le1\,500; all common guarantees apply.
  • Subtask 3 (30 points): 2≤n≤20 0002\le n\le20\,000, 1≤q≤20 0001\le q\le20\,000; all common guarantees apply.
  • Subtask 4 (40 points): 2≤n≤100 0002\le n\le100\,000, 1≤q≤100 0001\le q\le100\,000; all common guarantees apply. These are the full original constraints.

Examples

Input

5 4
1 2
2 3
2 4
4 5
2 1
2 5
1 2
2 5

Output

0
3
2

Explanation

Initially the only red vertex is 11.

  1. For 2 1, vertex 11 itself is red, hence the answer is 00.
  2. For 2 5, the path 5→4→2→15\to4\to2\to1 has three edges, hence the answer is 33.
  3. Operation 1 2 paints vertex 22 red; the red set becomes {1,2}\{1,2\}.
  4. For 2 5, the path 5→4→25\to4\to2 has only two edges, so the answer becomes 22.