#SRD0000119. Đỉnh đỏ gần nhất (Xenia and Tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đỉ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 vertices numbered from to . 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 is red; every other vertex is blue.
Process operations in the given order:
- Type
1 v: Paint vertex red. The input guarantees that is blue when this operation is performed. Once red, a vertex remains red. - Type
2 v: Print the minimum distance from to any vertex that is currently red. If itself is red, the answer is .
A query must be answered using the coloring at its own time, without considering future updates.
Input
- The first line contains two integers , the number of vertices and operations.
- Each of the next lines contains two integers describing an undirected edge.
- Each of the following lines contains two integers (), describing an operation.
The edges form a tree, and vertices are numbered starting at .
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: , ; , ; the edges form a connected, acyclic tree; and ; type-1 operations only paint currently blue vertices; initially only vertex is red.
- Subtask 1 (10 points): , ; all common guarantees apply.
- Subtask 2 (20 points): , ; all common guarantees apply.
- Subtask 3 (30 points): , ; all common guarantees apply.
- Subtask 4 (40 points): , ; 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 .
- For
2 1, vertex itself is red, hence the answer is . - For
2 5, the path has three edges, hence the answer is . - Operation
1 2paints vertex red; the red set becomes . - For
2 5, the path has only two edges, so the answer becomes .