CF1889F.Doremy's Average Tree
NOI/NOI+/CTSC
通过率:0%
时间限制:3.00s
内存限制:1024MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Doremy has a rooted tree of size n whose root is vertex r. Initially there is a number wi written on vertex i. Doremy can use her power to perform this operation at most k times:
- Choose a vertex x (1≤x≤n).
- Let s=∣T∣1∑i∈Twi where T is the set of all vertices in x's subtree.
- For all i∈T, assign wi:=s.
Doremy wants to know what is the lexicographically smallest† array w after performing all the operations. Can you help her?
If there are multiple answers, you may output any one.
† For arrays a and b both of length n, a is lexicographically smaller than b if and only if there exist an index i (1≤i≤n) such that ai<bi and for all indices j such that j<i, aj=bj is satisfied.
Doremy 有一棵大小为 n 的有根树,其根节点为顶点 r。初始时,顶点 i 上写有一个数字 wi。Doremy 可以使用她的能力至多执行 k 次如下操作:
- 选择一个顶点 x(1≤x≤n);
- 设 s=∣T∣1∑i∈Twi,其中 T 表示顶点 x 的子树中所有顶点的集合;
- 对所有 i∈T,令 wi:=s。
Doremy 想知道:在执行完所有操作后,能得到的字典序最小†的数组 w 是什么?你能帮她吗?
若存在多个满足条件的答案,输出任意一个即可。
† 对于两个长度均为 n 的数组 a 和 b,当且仅当存在某个下标 i(1≤i≤n),使得 ai<bi,且对所有满足 j<i 的下标 j 均有 aj=bj 时,称 a 的字典序小于 b。
输入格式
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. The description of the test cases follows.
The first line contains three integers n, r, k (2≤n≤5000, 1≤r≤n, 0≤k≤min(500,n)).
The second line contains n integers w1,w2,…,wn (1≤wi≤106).
Each of the next n−1 lines contains two integers ui, vi (1≤ui,vi≤n), representing an edge between ui and vi.
It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of n does not exceed 50000.
输入包含多个测试用例。第一行包含一个整数 t(1≤t≤104),表示测试用例的数量。随后是各测试用例的描述。
第一行包含三个整数 n、r、k(2≤n≤5000,1≤r≤n,0≤k≤min(500,n))。
第二行包含 n 个整数 w1,w2,…,wn(1≤wi≤106)。
接下来的 n−1 行中,每行包含两个整数 ui、vi(1≤ui,vi≤n),表示节点 ui 与 vi 之间存在一条边。
保证所给的边构成一棵树。
保证所有测试用例中 n 的总和不超过 50000。
输出格式
For each test case, In the first line, output a single integer cnt (0≤cnt≤k) — the number of operations you perform.
Then, in the second line output cnt integers p1,p2,…,pcnt — x is chosen to be pi for i-th operation.
If there are multiple answers, you may output any one.
对于每个测试用例,在第一行输出一个整数 cnt(0≤cnt≤k)—— 表示你执行的操作次数。
然后,在第二行输出 cnt 个整数 p1,p2,…,pcnt —— 第 i 次操作中选择的 x 为 pi。
如有多个正确答案,输出任意一个即可。
输入输出样例
输入#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]. You can choose some vertex x to perform at most one operation.
- If x=1, w=[29,29,29,29,29,29].
- If x=2, w=[1,215,2,215,1,8].
- If x=3, w=[1,9,311,6,311,311].
- If x∈4,5,6, w=[1,9,2,6,1,8].
- If you don't perform any operation, w=[1,9,2,6,1,8].
w is lexicographically smallest when x=2.
在第一个测试用例中:

初始时 w=[1,9,2,6,1,8]。你可以选择某个顶点 x,至多执行一次操作。
- 若 x=1,则 w=[29,29,29,29,29,29]。
- 若 x=2,则 w=[1,215,2,215,1,8]。
- 若 x=3,则 w=[1,9,311,6,311,311]。
- 若 x∈{4,5,6},则 w=[1,9,2,6,1,8]。
- 若不执行任何操作,则 w=[1,9,2,6,1,8]。
当 x=2 时,w 的字典序最小。
输入解题思路,AI测评打分。不知道怎么写?