Under construction

Still being built. Things may change

Dizzy Tree

Time limit1sMemory limit1024 MB

Summary
Given a tree whose root can be changed, answer queries that count the node pairs whose LCA under the current root is a given node.
Level

Medium7 of 10

Topics
Tree, DFS, Math
Solved
No attempts yet

Problem

Hobanu's pet tree is sick and feels dizzy. The tree keeps changing its root to keep its balance. While watching the tree, Hobanu got curious and asked it the queries below. Because of the dizziness, the tree could not answer them correctly. Let's find the answers for the poor tree.

Problem

The input is a tree with NN nodes whose root is node 11. The nodes are numbered from 11 to NN. Write a program that answers each of the QQ queries below.

  • 1 x: Change the root to node xx.
  • 2 x: Print the number of pairs (a,b)(a, b) with LCA(a,b)=xLCA(a, b) = x. The pairs (a,b)(a, b) and (b,a)(b, a) are treated as the same ordered pair. (a≠ba \neq b)

Input

The first line contains NN and QQ, separated by a space. (1≤N≤200 000,1≤Q≤200 000)(1 \le N \le 200\,000, 1 \le Q \le 200\,000)

From the second line to line NN, each line contains two node numbers aa and bb connected by an edge, separated by a space. (1≤a,b≤N)(1 \le a, b \le N)

From line N+1N+1 to line N+QN+Q, the queries described above are given.

Output

For each type 2 query, print the answer on its own line, in the order the queries are given. At least one type 2 query is given.

Hint

LCA stands for Lowest Common Ancestor. The LCA of aa and bb is the deepest node that has both aa and bb as descendants.

Examples1

  1. Example 1

    Input
    6 4
    1 2
    2 3
    1 4
    5 2
    4 6
    2 2
    2 1
    1 4
    2 1
    
    Expected output
    3
    11
    3