CF1779F.Xorcerer's Stones

省选/NOI-

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Misha had been banned from playing chess for good since he was accused of cheating with an engine. Therefore, he retired and decided to become a xorcerer.

One day, while taking a walk in a park, Misha came across a rooted tree with nodes numbered from 11 to nn. The root of the tree is node 11.

For each 1≤i≤n1\le i\le n, node ii contains aia_i stones in it. Misha has recently learned a new spell in his xorcery class and wants to test it out. A spell consists of:

  • Choose some node ii (1≤i≤n1 \leq i \leq n).
  • Calculate the bitwise XOR xx of all aja_j such that node jj is in the subtree of ii (ii belongs to its own subtree).
  • Set aja_j equal to xx for all nodes jj in the subtree of ii.

Misha can perform at most 2n2n spells and he wants to remove all stones from the tree. More formally, he wants ai=0a_i=0 to hold for each 1≤i≤n1\leq i \leq n. Can you help him perform the spells?

A tree with nn nodes is a connected acyclic graph which contains n−1n-1 edges. The subtree of node ii is the set of all nodes jj such that ii lies on the simple path from 11 (the root) to jj. We consider ii to be contained in its own subtree.

米沙因被指控使用引擎作弊而被永久禁止下国际象棋。因此,他选择退役,并决定成为一名异或法师(xorcerer)。

一天,米沙在公园散步时,遇到了一棵以节点 11 为根的有根树,树上共有 nn 个节点,编号从 11 到 nn。

对每个 1≤i≤n1 \le i \le n,节点 ii 中含有 aia_i 颗石子。米沙最近在异或魔法课上学会了一个新咒语,想借此一试身手。该咒语的操作步骤如下:

  • 任选一个节点 ii(1≤i≤n1 \leq i \leq n);
  • 计算所有满足“节点 jj 属于节点 ii 的子树”条件的 aja_j 的按位异或(bitwise XOR)值 xx(注意:节点 ii 自身属于其子树);
  • 将节点 ii 的子树中所有节点 jj 对应的 aja_j 值全部设为 xx。

米沙最多可施放 2n2n 次该咒语,目标是清空整棵树上的所有石子——即令所有 1≤i≤n1 \leq i \leq n 均满足 ai=0a_i = 0。你能帮他设计出施法方案吗?

含 nn 个节点的树是一个包含 n−1n-1 条边的连通无环图。节点 ii 的子树定义为所有满足“从根节点 11 到节点 jj 的简单路径经过节点 ii”的节点 jj 所构成的集合。我们约定:节点 ii 自身属于其子树。

输入格式

The first line contains a single integer nn (2≤n≤2⋅1052 \leq n \leq 2\cdot 10^5) — the size of the tree

The second line contains an array of integers a1,a2,…,ana_1,a_2,\ldots, a_n (0≤ai≤310 \leq a_i \leq 31), describing the number of stones in each node initially.

The third line contains an array of integers p2,p3,…,pnp_2,p_3,\ldots, p_n (1≤pi≤i−11 \leq p_i \leq i-1), where pip_i means that there is an edge connecting pip_i and ii.

第一行包含一个整数 nn(2≤n≤2⋅1052 \leq n \leq 2\cdot 10^5)——树的大小。

第二行包含一个整数数组 a1,a2,…,ana_1,a_2,\ldots, a_n(0≤ai≤310 \leq a_i \leq 31),表示每个节点初始时的石子数量。

第三行包含一个整数数组 p2,p3,…,pnp_2,p_3,\ldots, p_n(1≤pi≤i−11 \leq p_i \leq i-1),其中 pip_i 表示存在一条连接节点 pip_i 与节点 ii 的边。

输出格式

If there is not a valid sequence of spells, output −1-1.

Otherwise, output a single integer qq (0≤q≤2n0 \leq q \leq 2n) in the first line — the number of performed spells.

In the second line output a sequence of integers v1,v2,…,vqv_1,v_2,\ldots,v_q (1≤vi≤n1 \leq v_i \leq n) — the ii-th spell will be performed on the subtree of node viv_i. Please note that order matters.

If multiple solutions exist, output any. You don't have to minimize the number of operations.

如果不存在合法的法术序列,则输出 −1-1。

否则,在第一行输出一个整数 qq(0≤q≤2n0 \leq q \leq 2n)——表示执行的法术数量。

在第二行输出一个整数序列 v1,v2,…,vqv_1,v_2,\ldots,v_q(1≤vi≤n1 \leq v_i \leq n)——第 ii 个法术将施加于节点 viv_i 的子树上。请注意,顺序至关重要。

若存在多个解,输出任意一个即可。你无需最小化操作次数。

输入输出样例

  • 输入#1

    2
    13 13
    1

    输出#1

    1
    1
  • 输入#2

    7
    5 2 8 3 4 1 31
    1 1 2 2 3 3

    输出#2

    -1
  • 输入#3

    9
    3 31 1 2 7 30 7 3 1
    1 1 1 2 5 5 3 4

    输出#3

    6
    3 2 3 1 2 2

说明/提示

Please refer to the following pictures for an explanation of the third test. Only the first 44 spells are shown since the last 22 do nothing. The first picture represents the tree initially with the number of stones for each node written above it in green. Changes applied by the current spell are highlighted in red.

请参考以下图片以了解第三个测试用例的说明。仅展示了前 44 个法术,因为最后 22 个法术不产生任何效果。第一张图片表示初始树结构,每个节点上方以绿色数字标出其石子数量。当前法术所引起的变化以红色高亮显示。

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

首页