QOJ.ac

QOJ

Limite de temps : 6 s Limite de mémoire : 2048 MB Points totaux : 100

#2637. 无因子树

Statistiques

无因子树(Factor-free tree)是一种有根二叉树,其中每个节点包含一个正整数,该整数与其所有祖先节点的值互质。若两个正整数的最大公约数为 1,则称它们互质。

有根二叉树的中序遍历序列可以通过递归方式生成:先遍历左子树,然后访问根节点,最后遍历右子树。图 F.1 展示了一棵无因子树的中序遍历序列。

图 F.1:样例 1 的示意图。该树是无因子树;例如,标记为“5”的节点的值与其所有祖先节点(标记为“9”、“8”和“7”)的值互质。

给定一个序列 $a_1, a_2, \dots, a_n$,判断它是否为某棵无因子树的中序遍历序列,如果是,请构造出这样一棵树。

输入格式

输入包含: 一行一个整数 $n$ ($1 \le n \le 10^6$),表示序列的长度。 一行 $n$ 个整数 $a_1, \dots, a_n$ ($1 \le a_i \le 10^7$,对于每个 $i$),表示序列的元素。

输出格式

如果存在一棵以给定序列为中序遍历序列的无因子树,请输出 $n$ 个值。对于序列中的每个值,输出其父节点的 1-based 索引;如果是根节点,则输出 0。如果存在多个有效答案,输出其中任意一个即可。

如果不存在这样的树,则输出 impossible

样例

样例输入 1

6
2 7 15 8 9 5

样例输出 1

2 0 4 2 4 5

样例输入 2

6
2 7 15 8 9 6

样例输出 2

impossible

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.