QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 256 MB Puntuación total: 100 Dificultad: [mostrar]

#488. Approximate Bounding Box

Estadísticas

Given a tree with $n$ nodes. There are $m$ queries. Each query provides a set of distinct nodes $V$. You need to calculate:

  1. The sum of distances between all pairs of nodes in $V$, i.e., $$\sum_{u,v \in V} dis(u,v)$$
  2. The minimum distance between any pair of nodes in $V$, i.e., $$\min_{u,v \in V, u \neq v} dis(u,v)$$
  3. The maximum distance between any pair of nodes in $V$, i.e., $$\max_{u,v \in V} dis(u,v)$$

Input

The first line contains a positive integer $n$, representing the number of nodes in the tree. The next $n-1$ lines each contain two integers $u_i, v_i$, representing an edge (nodes are numbered $1, 2, 3, \dots, n$). The next line contains a positive integer $m$, representing the number of queries. For each query, the first line contains an integer $k$, representing the number of nodes in the query set; the second line contains $k$ integers in the range $[1, n]$, representing the nodes in the set.

Output

Output $m$ lines. For each query, output three integers on a single line, representing the answers in the order specified above.

Examples

Input 1

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

Output 1

3 3 3
2 2 2
8 2 3

Constraints

  • For 20% of the data, $1 \le n, m \le 1000$;
  • For another 20% of the data, $k \le 3$;
  • For another 30% of the data, the tree is guaranteed to be a complete binary tree rooted at 1;
  • For 100% of the data, $1 \le n, m \le 5 \times 10^5$, $2 \le k \le n$, $\sum k \le 5 \times 10^5$.

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.