QOJ.ac

QOJ

Limite de temps : 8 s Limite de mémoire : 1024 MB Points totaux : 100 Hackable ✓

#17975. Distance Multiplication Maximization

Statistiques

There is a tree (a connected undirected graph with no cycles) consisting of $N$ vertices. The vertices are numbered from $1$ to $N$, and the edges are numbered from $1$ to $(N-1)$.

Write a program that performs the following query.

  • $u$ $v$: For vertex $x$ ($1 \le x \le N$), print the maximum value of $\operatorname{dist}(x, u) \times \operatorname{dist}(x, v)$. ($1 \le u, v \le N$)

Here, $\operatorname{dist}(x, y)$ is defined as the number of edges on the shortest path from vertex $x$ to vertex $y$. For any vertex $x$, $\operatorname{dist}(x, x) = 0$.

Input

The first line of input contains $N$, denoting the number of vertices of the tree. ($2 \le N \le 300\,000$)

The following $(N-1)$ lines contain the information of the tree. The $i$-th of them contains two space-separated integers denoting the two vertices connected by edge $i$.

The next line contains $Q$, denoting the number of queries. ($2 \le Q \le 300\,000$)

Each of the following $Q$ lines contains the information for each query in the same format as written in the problem statement.

Output

Print $Q$ lines containing the answer to each query, one per line.

Examples

Input 1

5
1 2
2 3
2 4
4 5
3
1 2
2 5
3 3

Output 1

6
3
9

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.