QOJ.ac

QOJ

时间限制: 3 s 内存限制: 1024 MB 总分: 100

#5200. BinCoin

统计

BinCoin 公司有 $n$ 名员工,编号从 $1$ 到 $n$。该公司的隶属结构是一棵有根树。换句话说:

  • 公司里有一名 CEO,即主要负责人。
  • 其他每名员工都有且仅有一名直接上级。
  • 隶属结构中不存在环。

此外,由于 BinCoin 的 CEO 对所有二进制事物有着难以解释的热爱,公司的隶属结构是一棵二叉有根树。这意味着每名员工直接管理的下属人数要么是零,要么是二。

在 CEO 看来,在这家公司工作和在矿井里工作一样危险。因此,员工有时需要签署免责声明。这个过程按以下方式进行:最初,CEO 拿着日志,然后递归地执行以下程序:

  • 如果持有日志的员工没有任何下属,他们会在日志中签署免责声明,并将其交还给他们的上级。如果该员工是 CEO(没有上级),则程序停止。
  • 否则:
    • 他们从两名直接下属中随机选择一名,并将日志交给其中一人;
    • 当他们拿回日志时,他们会签署它;
    • 然后他们将其交给另一名直接下属;
    • 当他们再次拿回日志时,他们将其交还给他们的上级。如果该员工是 CEO(没有上级),则程序停止。

所有的随机选择都是相互独立的。

有一天,CEO 意识到他们记不清隶属树了。幸运的是,他们有包含 $k$ 条记录的日志。每条记录都是员工签署日志时的顺序序列。请帮助 CEO 恢复隶属树。

输入格式

第一行包含两个整数 $n$ 和 $k$ —— 员工人数和日志中的记录数($1 \le n \le 999; 50 \le k \le 100$)。

接下来的 $k$ 行,每行包含一个 $1$ 到 $n$ 的整数排列 —— 对应记录中员工签署的顺序。

保证输入是按照题目描述中通过真实随机选择获得的结果。

输出格式

输出 $n$ 个整数 $p_i$。如果 $i$ 是 CEO,则 $p_i$ 应为 $-1$。否则,$p_i$ 应为第 $i$ 名员工的直接上级的编号。

你的输出应对应一棵二叉有根树。如果有多棵树满足输入,你可以输出其中任意一棵。

样例

样例输入 1

3 50
1 2 3 1 2 3 3 2 1 1 2 3
3 2 1 1 2 3 1 2 3 1 2 3
1 2 3 3 2 1 1 2 3 3 2 1
1 2 3 3 2 1 1 2 3 3 2 1
1 2 3 1 2 3 3 2 1 1 2 3
3 2 1 1 2 3 3 2 1 1 2 3
1 2 3 3 2 1 1 2 3 1 2 3
1 2 3 1 2 3 3 2 1 1 2 3
3 2 1 3 2 1 1 2 3 3 2 1
1 2 3 3 2 1 3 2 1 1 2 3
1 2 3 3 2 1 1 2 3 3 2 1
3 2 1 3 2 1 1 2 3 1 2 3
3 2 1 3 2 1

样例输出 1

2 -1 2

样例输入 2

5 60
2 4 3 5 1 1 5 2 4 3 1 5 2 4 3
1 5 2 4 3 1 5 3 4 2 1 5 3 4 2
1 5 3 4 2 1 5 3 4 2 1 5 3 4 2
3 4 2 5 1 2 4 3 5 1 1 5 2 4 3
3 4 2 5 1 2 4 3 5 1 2 4 3 5 1
1 5 2 4 3 3 4 2 5 1 3 4 2 5 1
1 5 2 4 3 2 4 3 5 1 1 5 2 4 3
1 5 3 4 2 3 4 2 5 1 1 5 3 4 2
1 5 2 4 3 1 5 3 4 2 1 5 2 4 3
2 4 3 5 1 2 4 3 5 1 2 4 3 5 1
2 4 3 5 1 2 4 3 5 1 1 5 2 4 3
1 5 3 4 2 1 5 2 4 3 3 4 2 5 1
1 5 3 4 2 3 4 2 5 1 3 4 2 5 1
1 5 2 4 3 2 4 3 5 1 1 5 2 4 3
1 5 3 4 2 2 4 3 5 1 2 4 3 5 1
1 5 2 4 3 1 5 2 4 3 1 5 2 4 3
1 5 2 4 3 1 5 2 4 3 3 4 2 5 1
3 4 2 5 1 3 4 2 5 1 1 5 2 4 3
1 5 3 4 2 1 5 3 4 2 2 4 3 5 1
3 4 2 5 1 1 5 2 4 3 3 4 2 5 1

样例输出 2

5 4 4 5 -1

说明

为了适应页面排版,样例中的多行连续数据被合并为一行。实际输入遵循输入格式描述。

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.