CF1767F.Two Subtrees

NOI/NOI+/CTSC

通过率:0%

时间限制:9.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a rooted tree consisting of nn vertices. The vertex 11 is the root. Each vertex has an integer written on it; this integer is valival_i for the vertex ii.

You are given qq queries to the tree. The ii-th query is represented by two vertices, uiu_i and viv_i. To answer the query, consider all vertices ww that lie in the subtree of uiu_i or viv_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.

给你一棵包含 nn 个顶点的有根树,其中顶点 11 为根。每个顶点上写有一个整数;顶点 ii 上的整数记为 valival_i。

你将收到 qq 个关于该树的查询。第 ii 个查询由两个顶点 uiu_i 和 viv_i 表示。为了回答该查询,请考虑所有位于 uiu_i 的子树中或 viv_i 的子树中的顶点 ww(若某个顶点同时属于两个子树,则它被计算两次)。对这两个子树中的所有顶点,列出其上所写的全部整数,并找出出现次数最多的那个整数。如果存在多个整数具有相同的最大出现次数,则答案为其中最小的那个整数。

输入格式

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

The second line contains nn integers val1,val2,…,valnval_1, val_2, \dots, val_n (1≤vali≤2⋅1051 \le val_i \le 2 \cdot 10^5) — the numbers written on the vertices.

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

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

Then qq lines follow. The ii-th of them containing two numbers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n) — the roots of subtrees in the ii-th query.

第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)——树中顶点的数量。

第二行包含 nn 个整数 val1,val2,…,valnval_1, val_2, \dots, val_n(1≤vali≤2⋅1051 \le val_i \le 2 \cdot 10^5)——写在各顶点上的数值。

接下来 n−1n - 1 行,每行包含两个整数 xx 和 yy(1≤x,y≤n1 \le x, y \le n),表示顶点 xx 与 yy 之间的一条边。这些边构成一棵树。

接下来一行包含一个整数 qq(1≤q≤2⋅1051 \le q \le 2 \cdot 10^5)——需要处理的查询数量。

随后是 qq 行,其中第 ii 行包含两个数 uiu_i 和 viv_i(1≤ui,vi≤n1 \le u_i, v_i \le n)——第 ii 个查询中两棵子树的根节点。

输出格式

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

说明/提示

In the 11-st query, the pair of subtrees consists of vertices [2,4,7,8][2, 4, 7, 8], and the numbers written on them are 1,2,2,4{1, 2, 2, 4}. The number 22 occurs twice, all other numbers — at most once, so the answer is 22.

In the 22-nd query, the pair of subtrees consists of vertices [3,5,6,7,7,8,8][3, 5, 6, 7, 7, 8, 8], and the numbers written on them are 3,3,3,2,2,4,4{3, 3, 3, 2, 2, 4, 4}. The number 33 has the maximum number of occurrences.

In the 44-th query, the pair of subtrees consists of vertices [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8][1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8], and the numbers written on them are 2,2,1,1,3,3,2,2,3,3,3,3,2,2,4,4{2, 2, 1, 1, 3, 3, 2, 2, 3, 3, 3, 3, 2, 2, 4, 4}. The numbers 22 and 33 are the most common, the minimum of them is 22.

在第 11 次查询中,该子树对包含顶点 [2,4,7,8][2, 4, 7, 8],其上所写的数字为 {1,2,2,4}\{1, 2, 2, 4\}。数字 22 出现了两次,其余所有数字至多出现一次,因此答案为 22。

在第 22 次查询中,该子树对包含顶点 [3,5,6,7,7,8,8][3, 5, 6, 7, 7, 8, 8],其上所写的数字为 {3,3,3,2,2,4,4}\{3, 3, 3, 2, 2, 4, 4\}。数字 33 的出现次数最多。

在第 44 次查询中,该子树对包含顶点 [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8][1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8],其上所写的数字为 {2,2,1,1,3,3,2,2,3,3,3,3,2,2,4,4}\{2, 2, 1, 1, 3, 3, 2, 2, 3, 3, 3, 3, 2, 2, 4, 4\}。数字 22 和 33 出现次数并列最多,其中较小者为 22。

输入解题思路,AI测评打分。不知道怎么写?

首页