Under construction

Still being built. Things may change

Province Capture

Time limit2sMemory limit1024 MB

Summary
Given a tree where provinces are toggled as captured, answer each path query with the number of path nodes inside the minimal subtree spanning all captured nodes.
Level

Hard8 of 10

Topics
Tree, Segment tree
Solved
No attempts yet

Statement

Deadpool has decided to get back at the Canadian government. He cannot manage it alone, so he will carry out his plan together with the inimitable Copycat.

Deadpool has decided to capture all of Canada. Then he will let it go, just for fun. Canada is divided into several administrative units, which we will simply call provinces. Some provinces are connected by two-way roads. Canada has nn provinces and n−1n-1 roads in total. There is exactly one simple path between any two provinces, so Canada is a tree.

As the plan unfolds, Deadpool captures some provinces and releases others. After each action, the provinces under his control are those that belong to the minimal connected subgraph containing all captured provinces. Every captured province is under his control, but not every province under his control is captured.

At certain moments, emergency medical trips take place from one province to another. A trip is more dangerous the more provinces under Deadpool's control it passes through.

For each trip, determine how many provinces under Deadpool's control lie on its path. Initially, all provinces are free. If one or both endpoints of a trip are under Deadpool's control, they are counted too.

Input

The first line contains a natural number nn (1≤n≤1051 \le n \le 10^5), the number of provinces. Each of the next n−1n-1 lines contains two natural numbers uu, vv (1≤u,v≤n,u≠v1 \le u, v \le n, u \neq v), the provinces connected by a two-way road. The given graph is guaranteed to be a tree.

The next line contains a natural number mm (1≤m≤1051 \le m \le 10^5), the number of events. Each of the next mm lines describes an event in chronological order. First comes a natural number tt (1≤t≤21 \le t \le 2), the type of the query.

If t=1t = 1, one natural number xx (1≤x≤n1 \le x \le n) follows, the province on which Deadpool acts. He captures it if it was free before, and releases it if it was captured before.

If t=2t = 2, two natural numbers xx, yy (1≤x,y≤n1 \le x, y \le n) follow, the provinces between which the trip is made.

Output

For each trip, print how many provinces under Deadpool's control lie on its path.

Examples2

  1. Example 1

    Input
    6
    3 5
    5 6
    5 1
    3 4
    4 2
    6
    1 5
    1 4
    2 1 2
    2 6 3
    1 4
    2 1 3
    
    Expected output
    3
    2
    1
    
  2. Example 2

    Input
    8
    2 1
    1 7
    2 4
    3 6
    2 3
    6 8
    4 5
    6
    1 7
    1 6
    1 3
    2 8 5
    2 7 6
    2 1 1
    
    Expected output
    3
    5
    1