Steel Ball Run

시간 제한4초메모리 제한512 MB

요약
트리에서 칩이 놓인 정점 집합이 삽입과 삭제로 바뀔 때마다, 모든 칩을 한 정점으로 모으는 최소 이동 횟수를 구한다.
난이도

어려움10점 중 8점

유형
트리, DFS, 세그먼트 트리, 수학
정답자
아직 제출이 없습니다

문제

You are given a tree with n vertices. Each vertex can contain a chip. Initially, all vertices are empty.

You have to handle two types of queries:

  1. Place a chip in a vertex
  2. Remove the chip from a vertex

After each query, you have to print the span of the current configuration of chips.

The span is defined as the minimum number of operations required to move all chips to the same vertex. In one operation, you can move a chip from its vertex to any adjacent vertex. Of course, during this process one vertex can contain multiple chips.

Note that these operations are needed only for the definition of span, and in fact, they are not performed.

입력

The first line contains a single integer n (1 ≤ n ≤ 105), the number of vertices in the tree.

The next n−1 lines describe tree edges, one per line. The i-th of them contains two integers u and v (1 ≤ u, v ≤ n), which are indices of the vertices connected by the i-th edge.

It is guaranteed that these edges form a tree.

The next line contains a single integer q (1 ≤ q ≤ 105), the number of queries.

The next q lines describe queries, one per line. Each query is described as “c v” (1 ≤ v ≤ n). The character c is equal to “+” for the first type of query and “-” for the second type. The integer v is the index of the vertex to which the query applies. It is guaranteed that, if the query is of the first type, there is no chip in the v-th vertex, and if the query is of the second type, there is a chip in the v-th vertex. It is also guaranteed that after each query there is at least one chip in the tree.

출력

For each query, print a line with a single integer: the span of the tree after applying this query.

예제2

  1. 예제 1

    입력
    3
    1 2
    2 3
    4
    + 1
    + 3
    + 2
    - 1
    
    예상 출력
    0
    2
    2
    1
    
  2. 예제 2

    입력
    6
    1 2
    2 3
    3 4
    4 5
    2 6
    5
    + 1
    + 4
    + 5
    - 5
    + 6
    
    예상 출력
    0
    3
    4
    3
    4