CF1889F.Doremy's Average Tree

NOI/NOI+/CTSC

通过率:0%

时间限制:3.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

Doremy has a rooted tree of size nn whose root is vertex rr. Initially there is a number wiw_i written on vertex ii. Doremy can use her power to perform this operation at most kk times:

  1. Choose a vertex xx (1≤x≤n1 \leq x \leq n).
  2. Let s=1∣T∣∑i∈Twis = \frac{1}{|T|}\sum_{i \in T} w_i where TT is the set of all vertices in xx's subtree.
  3. For all i∈Ti \in T, assign wi:=sw_i := s.

Doremy wants to know what is the lexicographically smallest†^\dagger array ww after performing all the operations. Can you help her?

If there are multiple answers, you may output any one.

†^\dagger For arrays aa and bb both of length nn, aa is lexicographically smaller than bb if and only if there exist an index ii (1≤i≤n1 \leq i \le n) such that ai<bia_i \lt b_i and for all indices jj such that j<ij \lt i, aj=bja_j=b_j is satisfied.

Doremy 有一棵大小为 nn 的有根树,其根节点为顶点 rr。初始时,顶点 ii 上写有一个数字 wiw_i。Doremy 可以使用她的能力至多执行 kk 次如下操作:

  1. 选择一个顶点 xx(1≤x≤n1 \leq x \leq n);
  2. 设 s=1∣T∣∑i∈Twis = \frac{1}{|T|}\sum_{i \in T} w_i,其中 TT 表示顶点 xx 的子树中所有顶点的集合;
  3. 对所有 i∈Ti \in T,令 wi:=sw_i := s。

Doremy 想知道:在执行完所有操作后,能得到的字典序最小†^\dagger的数组 ww 是什么?你能帮她吗?

若存在多个满足条件的答案,输出任意一个即可。

†^\dagger 对于两个长度均为 nn 的数组 aa 和 bb,当且仅当存在某个下标 ii(1≤i≤n1 \leq i \le n),使得 ai<bia_i \lt b_i,且对所有满足 j<ij \lt i 的下标 jj 均有 aj=bja_j = b_j 时,称 aa 的字典序小于 bb。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041\le t\le 10^4) — the number of test cases. The description of the test cases follows.

The first line contains three integers nn, rr, kk (2≤n≤50002 \le n \le 5000, 1≤r≤n1 \le r \le n, 0≤k≤min⁡(500,n)0 \le k \le \min(500,n)).

The second line contains nn integers w1,w2,…,wnw_1,w_2,\ldots,w_n (1≤wi≤1061 \le w_i \le 10^6).

Each of the next n−1n-1 lines contains two integers uiu_i, viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n), representing an edge between uiu_i and viv_i.

It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of nn does not exceed 50 00050\,000.

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

第一行包含三个整数 nn、rr、kk(2≤n≤50002 \le n \le 5000,1≤r≤n1 \le r \le n,0≤k≤min⁡(500,n)0 \le k \le \min(500,n))。

第二行包含 nn 个整数 w1,w2,…,wnw_1,w_2,\ldots,w_n(1≤wi≤1061 \le w_i \le 10^6)。

接下来的 n−1n-1 行中,每行包含两个整数 uiu_i、viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n),表示节点 uiu_i 与 viv_i 之间存在一条边。

保证所给的边构成一棵树。

保证所有测试用例中 nn 的总和不超过 50 00050\,000。

输出格式

For each test case, In the first line, output a single integer cntcnt (0≤cnt≤k0 \le cnt \le k) — the number of operations you perform.

Then, in the second line output cntcnt integers p1,p2,…,pcntp_1,p_2,\ldots,p_{cnt} — xx is chosen to be pip_i for ii-th operation.

If there are multiple answers, you may output any one.

对于每个测试用例,在第一行输出一个整数 cntcnt(0≤cnt≤k0 \le cnt \le k)—— 表示你执行的操作次数。

然后,在第二行输出 cntcnt 个整数 p1,p2,…,pcntp_1,p_2,\ldots,p_{cnt} —— 第 ii 次操作中选择的 xx 为 pip_i。

如有多个正确答案,输出任意一个即可。

输入输出样例

  • 输入#1

    4
    6 1 1
    1 9 2 6 1 8
    1 2
    1 3
    2 4
    3 6
    3 5
    7 7 2
    3 1 3 3 1 1 2
    7 1
    7 2
    7 4
    1 5
    2 3
    4 6
    6 5 1
    3 1 3 1 1 3
    5 3
    5 1
    5 6
    3 4
    1 2
    3 2 1
    1000000 999999 999997
    2 1
    1 3

    输出#1

    1
    2
    2
    1 4
    1
    5
    1
    1

说明/提示

In the first test case:

At first w=[1,9,2,6,1,8]w=[1,9,2,6,1,8]. You can choose some vertex xx to perform at most one operation.

  • If x=1x=1, w=[92,92,92,92,92,92]w=[\frac{9}{2},\frac{9}{2},\frac{9}{2},\frac{9}{2},\frac{9}{2},\frac{9}{2}].
  • If x=2x=2, w=[1,152,2,152,1,8]w=[1,\frac{15}{2},2,\frac{15}{2},1,8].
  • If x=3x=3, w=[1,9,113,6,113,113]w=[1,9,\frac{11}{3},6,\frac{11}{3},\frac{11}{3}].
  • If x∈4,5,6x \in {4, 5, 6}, w=[1,9,2,6,1,8]w=[1,9,2,6,1,8].
  • If you don't perform any operation, w=[1,9,2,6,1,8]w=[1,9,2,6,1,8].

ww is lexicographically smallest when x=2x=2.

在第一个测试用例中:

初始时 w=[1,9,2,6,1,8]w=[1,9,2,6,1,8]。你可以选择某个顶点 xx,至多执行一次操作。

  • 若 x=1x=1,则 w=[92,92,92,92,92,92]w=[\frac{9}{2},\frac{9}{2},\frac{9}{2},\frac{9}{2},\frac{9}{2},\frac{9}{2}]。
  • 若 x=2x=2,则 w=[1,152,2,152,1,8]w=[1,\frac{15}{2},2,\frac{15}{2},1,8]。
  • 若 x=3x=3,则 w=[1,9,113,6,113,113]w=[1,9,\frac{11}{3},6,\frac{11}{3},\frac{11}{3}]。
  • 若 x∈{4,5,6}x \in \{4, 5, 6\},则 w=[1,9,2,6,1,8]w=[1,9,2,6,1,8]。
  • 若不执行任何操作,则 w=[1,9,2,6,1,8]w=[1,9,2,6,1,8]。

当 x=2x=2 时,ww 的字典序最小。

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

首页