Codeforces Round 1083 (Div. 2)
C
Simons and Posting Blogs
Codeforces
1500
原题
← BD →
翻译中…

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

翻译中…

There are $n$ blogs. The $i$-th blog mentioned $l_i$ users in order as an array $a_i=[a_{i,1},a_{i,2},\ldots,a_{i,l_i}]$.

翻译中…

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

翻译中…
  • Choose an unposted blog $i$ ($1\le i\le n$), then post it. This will cause the following operations for each $1\le j\le l_i$ in order:
  • If $a_{i,j}$ already exists in $Q$, then move $a_{i,j}$ to the beginning of $Q$.
    • Otherwise, insert $a_{i,j}$ at the beginning of $Q$.
翻译中…

Find the lexicographically smallest$^{\text{∗}}$ $Q$ after all $n$ operations.

翻译中…

$^{\text{∗}}$An array $x$ is lexicographically smaller than an array $y$ if and only if one of the following holds: $x$ is a prefix of $y$, but $x \ne y$; or in the first position where $x$ and $y$ differ, the array $x$ has a smaller element than the corresponding element in $y$.

翻译中…

Input

翻译中…

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

翻译中…

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

翻译中…

Then $n$ lines follow, the $i$-th line starting with an integer $l_i$ ($1\le l_i\le 3000$), describing the number of users mentioned in the $i$-th blog, which is followed by $l_i$ integers $a_{i,1},a_{i,2},\ldots,a_{i,l_i}$ ($1\le a_{i,j}\le 10^6$) — the list of users mentioned in the $i$-th blog.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $3000$.

翻译中…

Denote $\sum\limits_{i=1}^n l_i$ as $L$. It is guaranteed that the sum of $L$ over all test cases does not exceed $3000$.

翻译中…

Output

翻译中…

Denote $m$ as the number of users mentioned in at least one blog. For each test case, output $m$ integers $Q_1,Q_2,\ldots,Q_m$ — the lexicographically smallest $Q$.

翻译中…

Note

翻译中…

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

翻译中…
  • Post the first blog, and $Q$ will become $[6,4,3,2,1]$.
  • Post the third blog, and $Q$ will become $[3,2,9,1,6,4]$.
  • Post the second blog, and $Q$ will become $[1,5,2,3,9,6,4]$.
翻译中…

There is another method to post blogs:

翻译中…
  • Post the third blog, and $Q$ will become $[3,2,9,1]$.
  • Post the first blog, and $Q$ will become $[6,4,3,2,1,9]$.
  • Post the second blog, and $Q$ will become $[1,5,2,6,4,3,9]$.
翻译中…

We can see that $[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 $1$, so $[1,5,2,3,9,6,4]$ is the lexicographically smallest array $Q$.

翻译中…

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

翻译中…
  • Post the first blog, and $Q$ will become $[6,1]$.
  • Post the second blog, and $Q$ will keep itself $[6,1]$.
翻译中…

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

翻译中…

样例

Input #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
Output #1
1 5 2 3 9 6 4
6 1
1 6
1 3 2 4
1 4 3 2 5 6 7
InputCopy OutputCopy
题目信息
题号
2205C
时间限制
2 s
内存限制
256 MB
标签
贪心
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。