QOJ.ac

QOJ

时间限制: 2.0 s 内存限制: 512 MB 总分: 100

#16952. Color the Tree

统计

Christina 有一棵拥有 $n$ 个节点的有根树。最初,除了根节点被染成红色外,所有节点都被染成绿色。Christina 认为,如果满足以下两个规则,这棵树就是美丽的

  • 根节点被染成红色。
  • 如果某个节点被染成红色,那么它与根节点之间最短路径上的所有节点也必须是红色。

Christina 对这棵树重复执行以下操作:选择一个节点并改变其颜色(如果它是红色的,则染成绿色;如果它是绿色的,则染成红色)。在执行操作时,必须满足以下规则:

  • 树必须保持美丽。
  • 节点的染色状态必须是唯一的。这意味着在过去的任何时刻,都不存在所有节点的颜色与当前完全相同的情况。

你的任务是帮助 Christina 构建一个符合上述规则的、尽可能长的操作序列。

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 20$) — 树中节点的数量。

第二行包含 $n - 1$ 个整数 $p_i$ ($1 \le p_i \le i$ 对于 $1 \le i \le n - 1$),表示树中节点的父节点。树中的节点编号为 $1$ 到 $n$,根节点的编号为 $1$,对于 $2 \le i \le n$,第 $i$ 个节点的父节点为 $p_{i-1}$。

输出格式

第一行输出一个整数 $m$ — 最大操作次数。

第二行输出 $m$ 个整数 $o_i$ ($2 \le o_i \le n$)。$o_i$ 表示在对应操作中改变颜色的节点编号。

如果存在多个可能的最长序列,输出其中任意一个即可。

样例

输入样例 1

4
1 1 1

输出样例 1

7
4 3 4 2 4 3 4

输入样例 2

4
1 2 3

输出样例 2

3
2 3 4

输入样例 3

6
1 1 2 2 2

输出样例 3

17
3 2 3 6 3 5 3 6 3 4 3 6 3 5 3 6 3

输入样例 4

5
1 2 1 1

输出样例 4

11
5 4 5 2 5 4 5 3 5 4 5

输入样例 5

5
1 1 2 3

输出样例 5

8
3 5 2 5 3 4 3 5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.