CF2193H.Remove the Grail Tree
提高+/省选-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The Great Grail Tree has stood in the kingdom for 315 years. It takes up a lot of space, so King Ila decided to get rid of it as soon as possible. The tree itself is an acyclic, connected, undirected graph with n vertices, each having its own value av. The tree can be removed in the following way:
- Let Sv be the sum of the values of all remaining neighbors of v. If v has no remaining neighbors, then Sv is 0. Choose a vertex v such that av and Sv differ in parity (i.e., either av is even and Sv is odd, or av is odd and Sv is even). If there are no such vertices, stop the process.
- Remove vertex v and all edges connected to it from the tree.
Your task is to determine whether there exists a sequence of removals that will lead to the complete removal of the Grail Tree (i.e., no vertices will remain in the tree). If such a sequence exists, output the sequence of n vertices in the order of their removal. If there are multiple answers, output any of them.
伟大的圣杯树已在王国中矗立了 315 年。它占据大量空间,因此伊拉国王决定尽快将其彻底移除。该树本身是一棵具有 n 个顶点的无环、连通、无向图,每个顶点 v 均具有一个权值 av。树可通过如下方式被移除:
- 设 Sv 为顶点 v 当前所有剩余邻接顶点的权值之和;若 v 没有剩余邻接顶点,则 Sv=0。选择一个顶点 v,使得 av 与 Sv 的奇偶性不同(即:av 为偶数而 Sv 为奇数,或 av 为奇数而 Sv 为偶数)。若不存在满足条件的顶点,则停止该过程。
- 将顶点 v 及其所有关联边从树中移除。
你的任务是判断是否存在一种移除序列,使得圣杯树被完全移除(即:树中不再剩余任何顶点)。若存在这样的序列,请输出一个长度为 n 的顶点序列,表示各顶点被移除的顺序;若存在多个可行答案,输出任意一个即可。
输入格式
Each test consists of several test case. The first line contains one integer t (1≤t≤104) — the number of test cases. The description of the test cases follows.
The first line contains the number n (1≤n≤2⋅105) — the number of vertices in the Grail Tree.
The second line describes the array a (1≤ai≤109) — the values of the vertices in the tree.
Next, there are n−1 lines, each containing 2 numbers v and u (1≤v,u≤n,v=u), indicating that vertices v and u are connected by an edge in the tree.
It is guaranteed that the sum of n across all test cases does not exceed 2⋅105.
每个测试包含若干测试用例。第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。随后是各测试用例的描述。
第一行包含一个整数 n(1≤n≤2⋅105)—— 圣杯树(Grail Tree)中的顶点数量。
第二行描述数组 a(1≤ai≤109)—— 树中各顶点的权值。
接下来有 n−1 行,每行包含两个数 v 和 u(1≤v,u≤n,v=u),表示顶点 v 和 u 在树中由一条边相连。
保证所有测试用例中 n 的总和不超过 2⋅105。
输出格式
For each test case, output "YES" if it is possible to completely remove the Grail Tree. Otherwise, output "NO". If the answer is "YES", output any sequence of removals.
You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
对于每个测试用例,如果能够完全移除圣杯树,则输出 “YES”;否则输出 “NO”。若答案为 “YES”,则还需输出任意一种移除序列。
你可以以任意大小写形式输出每个字母(小写或大写)。例如,字符串 “yEs”、“yes”、“Yes” 和 “YES” 均被视为有效的肯定回答。
输入输出样例
输入#1
5 3 1 2 4 1 2 2 3 4 3 4 2 1 1 2 2 3 3 4 6 9 6 5 1 7 4 1 2 2 3 2 4 3 5 4 6 5 2 1 1 1 2 2 1 3 2 2 4 5 4 5 1 5 3 7 9 1 2 2 3 3 4 4 5
输出#1
NO YES 2 3 1 4 NO YES 1 5 2 3 4 YES 2 4 1 5 3
输入解题思路,AI测评打分。不知道怎么写?