CF1889F.Doremy's Average Tree

普及/提高-

通过率:0%

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 ( 1xn1 \leq x \leq n ).
  2. Let s=1TiTwis = \frac{1}{|T|}\sum_{i \in T} w_i where TT is the set of all vertices in xx 's subtree.
  3. For all iTi \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 ( 1in1 \leq i \le n ) such that ai<bia_i < b_i and for all indices jj such that j<ij<i , aj=bja_j=b_j is satisfied.

输入格式

The input consists of multiple test cases. The first line contains a single integer tt ( 1t1041\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 ( 2n50002 \le n \le 5000 , 1rn1 \le r \le n , 0kmin(500,n)0 \le k \le \min(500,n) ).

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

Each of the next n1n-1 lines contains two integers uiu_i , viv_i ( 1ui,vin1 \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 5000050\,000 .

输出格式

For each test case, In the first line, output a single integer cntcnt ( 0cntk0 \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.

输入输出样例

  • 输入#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,6}x \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 .

首页