题库练习 Two Subtrees
← 上一题 下一题 →

A15652 | Two Subtrees

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

You are given a rooted tree consisting of $n$ vertices. The vertex $1$ is the root. Each vertex has an integer written on it; this integer is $val_i$ for the vertex $i$ .

You are given $q$ queries to the tree. The $i$ -th query is represented by two vertices, $u_i$ and $v_i$ . To answer the query, consider all vertices $w$ that lie in the subtree of $u_i$ or $v_i$ (if a vertex is in both subtrees, it is counted twice). For all vertices in these two subtrees, list all integers written on them, and find the integer with the maximum number of occurrences. If there are multiple integers with maximum number of occurrences, the minimum among them is the answer.

输入格式

The first line contains one integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the number of vertices in the tree.

The second line contains $n$ integers $val_1, val_2, \dots, val_n$ ( $1 \le val_i \le 2 \cdot 10^5$ ) — the numbers written on the vertices.

Then $n - 1$ lines follow, each containing two integers $x$ and $y$ ( $1 \le x, y \le n$ ) representing an edge between vertices $x$ and $y$ . These edges form a tree.

The next line contains one integer $q$ ( $1 \le q \le 2 \cdot 10^5$ ) — the number of queries to process.

Then $q$ lines follow. The $i$ -th of them containing two numbers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le n$ ) — the roots of subtrees in the $i$ -th query.

输出格式

For each query, print one integer — the number that has the maximum amount of occurrences in the corresponding pair of subtrees (if there are multiple such numbers, print the minimum one among them).

输入输出样例

输入 #1
8
2 1 3 2 3 3 2 4
1 2
1 3
2 4
3 5
3 6
3 7
7 8
6
2 7
3 7
2 3
1 1
5 6
1 3
输出 #1
2
3
3
2
3
3
C++ 编辑器
输入
输出