CF2252F.Spectral Components

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a tree consisting of nn vertices. Each vertex ii is painted with a color cic_i.

For each distinct color cc present in the tree, let mcm_c be the total number of vertices of color cc. You are also given an array kk of length nn, where kck_c (1≤kc≤mc1 \le k_c \le m_c) represents the target component size for color cc.

For every color cc independently, your task is to choose a connected subgraph (a component) consisting of exactly kck_c vertices. The vertices you choose for the component do not necessarily have to be of color cc.

The cost of a chosen component is the sum of the shortest distances from every vertex of color cc to the chosen component. (The distance from a vertex vv to a component SS is defined as the minimum number of edges on a simple path from vv to any vertex uu in SS).

For each color cc from 11 to nn, find the minimum possible cost of a valid component of size kck_c. If there are no vertices of color cc in the tree, output −1-1 for that color.

给你一棵包含 nn 个顶点的树。每个顶点 ii 被染成颜色 cic_i。

对于树中出现的每种不同颜色 cc,令 mcm_c 表示颜色为 cc 的顶点总数。同时给你一个长度为 nn 的数组 kk,其中 kck_c(满足 1≤kc≤mc1 \le k_c \le m_c)表示颜色 cc 对应的目标连通子图(即连通分量)大小。

对每种颜色 cc 独立地,你的任务是选出一个恰好包含 kck_c 个顶点的连通子图(即一个连通分量)。该子图中所选顶点的颜色不一定要均为 cc。

所选连通分量的代价定义为:所有颜色为 cc 的顶点到该连通分量的最短距离之和。(顶点 vv 到连通分量 SS 的距离定义为:从 vv 到 SS 中任意顶点 uu 的简单路径上的最少边数。)

对每种颜色 cc(c=1,2,…,nc = 1, 2, \dots, n),求出大小为 kck_c 的合法连通分量的最小可能代价。若树中不存在颜色为 cc 的顶点,则对该颜色输出 −1-1。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single 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 c1,c2,…,cnc_1, c_2, \ldots, c_n (1≤ci≤n1 \le c_i \le n) — the colors of the vertices.

The third line contains nn integers k1,k2,…,knk_1, k_2, \ldots, k_n (1≤ki≤n1 \le k_i \le n) — the target component sizes for each color. It is guaranteed that if color cc appears mc>0m_c \gt 0 times in the tree, then 1≤kc≤mc1 \le k_c \le m_c.

Each of the next n−1n - 1 lines contains two integers uu and vv (1≤u,v≤n1 \le u, v \le n), representing an edge between vertices uu and vv. It is guaranteed that the given edges form a valid tree.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

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

第二行包含 nn 个整数 c1,c2,…,cnc_1, c_2, \ldots, c_n(1≤ci≤n1 \le c_i \le n)——各顶点的颜色。

第三行包含 nn 个整数 k1,k2,…,knk_1, k_2, \ldots, k_n(1≤ki≤n1 \le k_i \le n)——每种颜色对应的目标连通块大小。保证:若颜色 cc 在树中出现 mc>0m_c > 0 次,则必有 1≤kc≤mc1 \le k_c \le m_c。

接下来的 n−1n - 1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n),表示顶点 uu 与 vv 之间存在一条边。保证所给边构成一棵合法的树。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output nn integers. The cc-th integer should be the minimum possible cost of a valid component of size kck_c for color cc, or −1-1 if color cc is not present in the tree.

对于每个测试用例,输出 nn 个整数。其中第 cc 个整数应为颜色 cc 的大小为 kck_c 的合法连通块的最小可能代价;若树中不包含颜色 cc,则输出 −1-1。

输入输出样例

  • 输入#1

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

    输出#1

    1 3 -1 -1 -1
    3 0 -1 -1 -1 -1
    3 2 -1 -1 -1 -1

说明/提示

In the first testcase, the tree has 55 vertices. Color 11 appears 33 times (vertices 1,2,41, 2, 4). Color 22 appears 22 times (vertices 3,53, 5). Colors 33, 44, and 55 do not appear, so their output is −1-1. For color 11 (k1=2k_1 = 2), we can choose the component S=2,4S = {2, 4}. The distance from vertex 11 to SS is 11. The distances from vertices 22 and 44 to SS are 00. The total cost is 1+0+0=11 + 0 + 0 = 1. For color 22 (k2=1k_2 = 1), the optimal component is the single vertex S=2S = {2}. The distance from 33 to 22 is 11, and from 55 to 22 is 22. The total cost is 33.

In the second testcase, the tree is a star graph with center 11 (color 22) and 55 leaves (color 11). For color 11 (k1=3k_1 = 3), the optimal strategy is to include the center and two leaves, for instance, S=1,2,3S = {1, 2, 3}. The distances from the color 11 leaves to SS are 00 (for 2,32, 3) and 11 (for 4,5,64, 5, 6), yielding a minimum cost of 33. For color 22 (k2=1k_2 = 1), the only vertex is the center itself. Choosing S=1S = {1} gives a cost of 00.

In the third testcase, the tree is a line graph 1−2−3−4−5−61-2-3-4-5-6 with alternating colors. For color 22 (vertices 2,4,62, 4, 6), we need a component of size 33. The optimal component is S=3,4,5S = {3, 4, 5}. The distances from the vertices of color 22 to SS are 11 (from 22, via edge 2−32-3), 00 (from 44, since it is in SS), and 11 (from 66, via edge 6−56-5). The total cost is 22.

在第一个测试用例中,树包含 55 个顶点。颜色 11 出现了 33 次(顶点 1,2,41, 2, 4),颜色 22 出现了 22 次(顶点 3,53, 5),颜色 33、44 和 55 均未出现,因此它们的输出为 −1-1。对于颜色 11(k1=2k_1 = 2),我们可以选择连通子图 S={2,4}S = \{2, 4\}。顶点 11 到 SS 的距离为 11;顶点 22 和 44 到 SS 的距离均为 00;总代价为 1+0+0=11 + 0 + 0 = 1。对于颜色 22(k2=1k_2 = 1),最优连通子图为单个顶点 S={2}S = \{2\};顶点 33 到 22 的距离为 11,顶点 55 到 22 的距离为 22;总代价为 33。

在第二个测试用例中,该树是一颗以顶点 11(颜色 22)为中心、含 55 片叶子(颜色 11)的星形图。对于颜色 11(k1=3k_1 = 3),最优策略是选取中心及其中两片叶子,例如 S={1,2,3}S = \{1, 2, 3\}。所有颜色 11 的叶子到 SS 的距离分别为:顶点 2,32, 3 的距离为 00,顶点 4,5,64, 5, 6 的距离为 11,从而得到最小总代价 33。对于颜色 22(k2=1k_2 = 1),唯一顶点即为中心本身;选取 S={1}S = \{1\} 可得代价 00。

在第三个测试用例中,该树是一条路径图 1−2−3−4−5−61-2-3-4-5-6,其顶点颜色交替排列。对于颜色 22(顶点 2,4,62, 4, 6),我们需要一个大小为 33 的连通子图。最优连通子图为 S={3,4,5}S = \{3, 4, 5\}。颜色 22 各顶点到 SS 的距离分别为:顶点 22 经边 2−32-3 到 SS 的距离为 11,顶点 44 属于 SS 故距离为 00,顶点 66 经边 6−56-5 到 SS 的距离为 11;总代价为 22。

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

首页