CF2205C.Simons and Posting Blogs

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This life is like a TV show; we're swept along as the plotlines go.

— SHUN, TRAP

There are nn blogs. The ii-th blog mentioned lil_i users in order as an array ai=[ai,1,ai,2,…,ai,li]a_i=[a_{i,1},a_{i,2},\ldots,a_{i,l_i}].

You are going to post all nn blogs. Let us maintain a sequence QQ that describes the list of users you have recently mentioned. You need to perform the following operation exactly nn times:

  • Choose an unposted blog ii (1≤i≤n1\le i\le n), then post it. This will cause the following operations for each 1≤j≤li1\le j\le l_i in order:
    • If ai,ja_{i,j} already exists in QQ, then move ai,ja_{i,j} to the beginning of QQ.
    • Otherwise, insert ai,ja_{i,j} at the beginning of QQ.

Find the lexicographically smallest∗^{\text{∗}} QQ after all nn operations.

∗^{\text{∗}}An array xx is lexicographically smaller than an array yy if and only if one of the following holds:

  • xx is a prefix of yy, but x≠yx \ne y; or
  • in the first position where xx and yy differ, the array xx has a smaller element than the corresponding element in yy.

人生就像一档电视节目;我们随剧情发展而被裹挟前行。

—— SHUN,《TRAP》链接

共有 nn 篇博客。第 ii 篇博客按顺序提到了 lil_i 个用户,记为数组 ai=[ai,1,ai,2,…,ai,li]a_i = [a_{i,1}, a_{i,2}, \ldots, a_{i,l_i}]。

你将依次发布全部 nn 篇博客。我们维护一个序列 QQ,用于记录你最近提及过的用户列表。你需要恰好执行 nn 次如下操作:

  • 任选一篇尚未发布的博客 ii(其中 1≤i≤n1 \le i \le n),然后将其发布。该操作将按顺序对每个 1≤j≤li1 \le j \le l_i 执行以下子操作:
    • 若 ai,ja_{i,j} 已存在于 QQ 中,则将 ai,ja_{i,j} 移至 QQ 的开头;
    • 否则,将 ai,ja_{i,j} 插入至 QQ 的开头。

求所有 nn 次操作完成后,字典序最小的∗^{\text{∗}} 序列 QQ。

∗^{\text{∗}} 数组 xx 字典序小于数组 yy,当且仅当满足以下任一条件:

  • xx 是 yy 的真前缀(即 xx 是 yy 的前缀但 x≠yx \ne y);或
  • 在 xx 与 yy 首次出现差异的位置上,xx 中对应位置的元素严格小于 yy 中对应位置的元素。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10001 \le t \le 1000). The description of the test cases follows.

The first line contains a single integer nn (1≤n≤30001\le n\le 3000) — the number of blogs.

Then nn lines follow, the ii-th line starting with an integer lil_i (1≤li≤30001\le l_i\le 3000), describing the number of users mentioned in the ii-th blog, which is followed by lil_i integers ai,1,ai,2,…,ai,lia_{i,1},a_{i,2},\ldots,a_{i,l_i} (1≤ai,j≤1061\le a_{i,j}\le 10^6) — the list of users mentioned in the ii-th blog.

It is guaranteed that the sum of nn over all test cases does not exceed 30003000.

Denote ∑i=1nli\sum\limits_{i=1}^n l_i as LL. It is guaranteed that the sum of LL over all test cases does not exceed 30003000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10001 \le t \le 1000)。随后是各测试用例的描述。

第一行包含一个整数 nn(1≤n≤30001\le n\le 3000)—— 博客的数量。

接下来是 nn 行,其中第 ii 行以一个整数 lil_i(1≤li≤30001\le l_i\le 3000)开头,表示第 ii 个博客中提及的用户数量;随后是 lil_i 个整数 ai,1,ai,2,…,ai,lia_{i,1},a_{i,2},\ldots,a_{i,l_i}(1≤ai,j≤1061\le a_{i,j}\le 10^6)—— 第 ii 个博客中提及的用户列表。

保证所有测试用例的 nn 之和不超过 30003000。

记 ∑i=1nli\sum\limits_{i=1}^n l_i 为 LL。保证所有测试用例的 LL 之和不超过 30003000。

输出格式

Denote mm as the number of users mentioned in at least one blog. For each test case, output mm integers Q1,Q2,…,QmQ_1,Q_2,\ldots,Q_m — the lexicographically smallest QQ.

记 mm 为在至少一篇博客中被提及的用户数量。对于每个测试用例,输出 mm 个整数 Q1,Q2,…,QmQ_1,Q_2,\ldots,Q_m —— 字典序最小的 QQ。

输入输出样例

  • 输入#1

    5
    3
    5 1 2 3 4 6
    3 2 5 1
    4 1 9 2 3
    2
    2 1 6
    1 6
    1
    3 6 1 1
    5
    4 2 3 3 4
    5 1 2 4 3 1
    2 4 1
    3 3 3 1
    5 4 3 2 2 2
    5
    4 2 3 1 4
    5 2 5 5 6 5
    5 3 4 7 5 5
    8 3 6 4 3 1 1 5 4
    2 1 1

    输出#1

    1 5 2 3 9 6 4
    6 1
    1 6
    1 3 2 4
    1 4 3 2 5 6 7

说明/提示

In the first test case, you can post the blogs as follows:

  • Post the first blog, and QQ will become [6,4,3,2,1][6,4,3,2,1].
  • Post the third blog, and QQ will become [3,2,9,1,6,4][3,2,9,1,6,4].
  • Post the second blog, and QQ will become [1,5,2,3,9,6,4][1,5,2,3,9,6,4].

There is another method to post blogs:

  • Post the third blog, and QQ will become [3,2,9,1][3,2,9,1].
  • Post the first blog, and QQ will become [6,4,3,2,1,9][6,4,3,2,1,9].
  • Post the second blog, and QQ will become [1,5,2,6,4,3,9][1,5,2,6,4,3,9].

We can see that [1,5,2,3,9,6,4][1,5,2,3,9,6,4] is lexicographically smaller than the other one. If we do not post the second blog at the end, the first element of the array will not be 11, so [1,5,2,3,9,6,4][1,5,2,3,9,6,4] is the lexicographically smallest array QQ.

In the second test case, you can post the blogs as follows:

  • Post the first blog, and QQ will become [6,1][6,1].
  • Post the second blog, and QQ will keep itself [6,1][6,1].

In the third test case, you have to post the only blog, and QQ will become [1,6][1,6].

在第一个测试用例中,你可以按如下方式发布博客:

  • 发布第一篇博客,此时 QQ 变为 [6,4,3,2,1][6,4,3,2,1]。
  • 发布第三篇博客,此时 QQ 变为 [3,2,9,1,6,4][3,2,9,1,6,4]。
  • 发布第二篇博客,此时 QQ 变为 [1,5,2,3,9,6,4][1,5,2,3,9,6,4]。

还存在另一种发布博客的方法:

  • 发布第三篇博客,此时 QQ 变为 [3,2,9,1][3,2,9,1]。
  • 发布第一篇博客,此时 QQ 变为 [6,4,3,2,1,9][6,4,3,2,1,9]。
  • 发布第二篇博客,此时 QQ 变为 [1,5,2,6,4,3,9][1,5,2,6,4,3,9]。

我们可以看出,[1,5,2,3,9,6,4][1,5,2,3,9,6,4] 在字典序上小于另一个结果。如果我们不将第二篇博客放在最后发布,则数组的第一个元素将不为 11,因此 [1,5,2,3,9,6,4][1,5,2,3,9,6,4] 是字典序最小的数组 QQ。

在第二个测试用例中,你可以按如下方式发布博客:

  • 发布第一篇博客,此时 QQ 变为 [6,1][6,1]。
  • 发布第二篇博客,此时 QQ 保持不变,仍为 [6,1][6,1]。

在第三个测试用例中,你必须发布唯一的一篇博客,此时 QQ 将变为 [1,6][1,6]。

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

首页