QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 64 MB Total points: 100

#14647. Uczta

Statistics

Problem Statement

King Bajtazar is organizing a grand feast for his 128th birthday. He wants to seat the invited guests at round tables in such a way that no guest sits alone at a table. He has already selected $n$ people whom he could invite to the feast. Then he ordered them from the most important to the least important and numbered them consecutively from $1$ to $n$ (guest number $1$ is the most important).

The guests are very demanding: each of them gave the king requirements about who may sit on their right side. The king wants every invited guest to have suitable company, so he will not allow any guest’s requirements to be violated. Therefore, it may turn out that it is impossible to invite all $n$ people. Bajtazar asked you (the royal computer scientist) to determine the best possible set of invited guests and an example seating of them at round tables.

When asked what he meant by “the best” set of guests, the king answered surprisingly precisely (for a 127-year-old programming layman):

To compare two sets of guests, I pick the person with the smallest number who belongs to exactly one of the two sets. This person belongs to the better set.

With this order, one can indeed uniquely determine which of the potential guest sets is the best.

Input Format

The first line of input contains an integer $n$ ($2 \le n \le 2000$), the number of guests. Each of the next $n$ lines contains the preference description of person $i$. The description starts with an integer $k_i$ ($k_i \ge 0$). Then $k_i$ pairwise distinct person numbers follow—integers from $1$ to $n$, different from $i$. Each such number denotes a person who may sit to the right of person $i$. The sum of all $k_i$ does not exceed 5000.

In tests worth 50% of the points, the following additional condition holds: if person $a$ allows person $b$ to sit on their right side, then person $b$ also allows person $a$ to sit on their right side. In particular, this means that these two people may sit together at the same two-person table.

In tests worth 20% of the points, it is possible to invite all $n$ people to the feast.

Output Format

The first line of output should contain an integer $s$, the number of tables in the found solution. The next $s$ lines should contain descriptions of the individual tables. A table description starts with an integer $g$, the number of guests seated at it. Then $g$ numbers follow: their guest indices. The guest numbers should be given consecutively in the direction opposite to the clockwise direction. The first printed guest will sit to the right of the last printed guest.

Example

For the input:

6
3 2 6 3
0
1 4
1 1
1 4
1 5

a correct output is:

1
3 1 3 4

In the above example, it is better to invite guests 1, 3, and 4 than guests 1, 4, 5, and 6. According to the king’s criterion, the better set is determined by guest number 3.

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.