CF2193H.Remove the Grail Tree

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The Great Grail Tree has stood in the kingdom for 315315 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 nn vertices, each having its own value ava_v. The tree can be removed in the following way:

  • Let SvS_v be the sum of the values of all remaining neighbors of vv. If vv has no remaining neighbors, then SvS_v is 00. Choose a vertex vv such that ava_v and SvS_v differ in parity (i.e., either ava_v is even and SvS_v is odd, or ava_v is odd and SvS_v is even). If there are no such vertices, stop the process.
  • Remove vertex vv 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 nn vertices in the order of their removal. If there are multiple answers, output any of them.

伟大的圣杯树已在王国中矗立了 315315 年。它占据大量空间,因此伊拉国王决定尽快将其彻底移除。该树本身是一棵具有 nn 个顶点的无环、连通、无向图,每个顶点 vv 均具有一个权值 ava_v。树可通过如下方式被移除:

  • 设 SvS_v 为顶点 vv 当前所有剩余邻接顶点的权值之和;若 vv 没有剩余邻接顶点,则 Sv=0S_v = 0。选择一个顶点 vv,使得 ava_v 与 SvS_v 的奇偶性不同(即:ava_v 为偶数而 SvS_v 为奇数,或 ava_v 为奇数而 SvS_v 为偶数)。若不存在满足条件的顶点,则停止该过程。
  • 将顶点 vv 及其所有关联边从树中移除。

你的任务是判断是否存在一种移除序列,使得圣杯树被完全移除(即:树中不再剩余任何顶点)。若存在这样的序列,请输出一个长度为 nn 的顶点序列,表示各顶点被移除的顺序;若存在多个可行答案,输出任意一个即可。

输入格式

Each test consists of several test case. The first line contains one 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 the number nn (1≤n≤2⋅1051\le n\le 2\cdot 10^5) — the number of vertices in the Grail Tree.

The second line describes the array aa (1≤ai≤1091\le a_i\le 10^9) — the values of the vertices in the tree.

Next, there are n−1n - 1 lines, each containing 2 numbers vv and uu (1≤v,u≤n,v≠u1\le v, u\le n, v\neq u), indicating that vertices vv and uu are connected by an edge in the tree.

It is guaranteed that the sum of nn across 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)—— 圣杯树(Grail Tree)中的顶点数量。

第二行描述数组 aa(1≤ai≤1091\le a_i\le 10^9)—— 树中各顶点的权值。

接下来有 n−1n - 1 行,每行包含两个数 vv 和 uu(1≤v,u≤n, v≠u1\le v, u\le n,\, v\neq u),表示顶点 vv 和 uu 在树中由一条边相连。

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

输出格式

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测评打分。不知道怎么写?

首页