Dizzy Tree
Time limit1sMemory limit1024 MB
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.
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 nodes whose root is node . The nodes are numbered from to . Write a program that answers each of the queries below.
1 x: Change the root to node .2 x: Print the number of pairs with . The pairs and are treated as the same ordered pair. ()
Input
The first line contains and , separated by a space.
From the second line to line , each line contains two node numbers and connected by an edge, separated by a space.
From line to line , 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 and is the deepest node that has both and as descendants.