QOJ.ac

QOJ

حد الوقت: 2 s حد الذاكرة: 1024 MB مجموع النقاط: 100

#18820. Tree and Query 17

الإحصائيات

There is a tree consisting of $N$ vertices. The vertices are numbered from 1 to $N$, and vertex 1 is the root. Each vertex $i$ stores an integer $A[i]$. Initially, $A[i]$ is 0.

The following queries must be processed:

  • 1 u: Add 1 to $A[i]$ for all vertices $i$ in the subtree rooted at vertex $u$.
  • 2 u v: Add 1 to $A[i]$ for all vertices $i$ on the unique shortest path from vertex $u$ to vertex $v$. $u$ and $v$ may be the same.

After each query, output the vertex $x$ that minimizes $\sum_{y = 1}^{N} A[y] \times dist(x, y)$. $dist(x, y)$ equals the number of edges on the path from $x$ to $y$. If there are multiple such vertices $x$, output the one closest to the root (i.e., with smallest distance from root). It can be proven that such a vertex is always unique.

Input

The first line contains $N$. ($2 \le N \le 100\,000$)

The next $N-1$ lines contain the edges of the tree. Each line consists of two integers $u$ and $v$ separated by a space, meaning there is an edge connecting vertex $u$ and vertex $v$. ($1 \le u, v \le N, u \neq v$)

The next line contains $Q$, the number of queries to be processed. ($1 \le Q \le 100\,000$).

The next $Q$ lines each contain a query, given in one of the following formats:

  • 1 u ($1 \le u \le N$)
  • 2 u v ($1 \le u, v \le N$)

Output

For each query, output the required value in order, one per line, for a total of $Q$ lines.

Examples

Input 1

7
1 6
1 7
7 3
3 2
7 5
5 4
4
1 2
1 4
1 6
2 6 7

Output 1

2
7
7
1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.