#P17194. [KOI 2026 #2] 分发零食

[KOI 2026 #2] 分发零食

Problem Description

There are NN students and NN snacks. Both students and snacks are numbered 1,2,⋯ ,N1,2,\cdots,N, respectively.

Each student likes at least one of these NN snacks. More specifically, student ii (1≤i≤N1 \le i \le N) likes CiC_i snacks, whose indices are Ai,1,Ai,2,⋯ ,Ai,CiA_{i,1},A_{i,2},\cdots,A_{i,C_i}.

At the beginning, there is exactly one copy of each of the NN types of snacks in the room. Now we distribute the snacks to the students using the following process:

  • Choose a suitable order, and each time let one student enter the room.
  • A student who enters the room will take away all remaining snacks in the room that they like. If there are no snacks left in the room that they like, they take nothing.

Please choose a suitable order for the NN students to enter the room, and determine whether it is possible to make every student take exactly one snack. If it is possible, output any order that satisfies the requirement.

Input Format

The first line contains an integer NN, which represents the number of students and the number of snacks.

The next NN lines describe the snacks liked by the NN students. On line ii (1≤i≤N1 \le i \le N), it gives the space-separated integer CiC_i followed by CiC_i integers Ai,1,Ai,2,⋯ ,Ai,CiA_{i,1},A_{i,2},\cdots,A_{i,C_i}.

Output Format

If there is no order that allows all students to take exactly one snack, output -1 on the first line.

If, when students enter the room in the order of indices P1,P2,⋯ ,PNP_1,P_2,\cdots,P_N, every student can take exactly one snack, then output NN space-separated integers P1,P2,⋯ ,PNP_1,P_2,\cdots,P_N on the first line.

If there are multiple feasible outputs, any one of them will be accepted.

3
2 1 2
2 2 3
1 2
3 1 2
2
2 1 2
2 1 2
-1
4
1 3
1 2
3 4 2 3
2 1 2
1 2 3 4

Hint

Explanation of Sample 1

The snacks liked by each student are as follows:

  • Student 11 likes snack 11 and snack 22.
  • Student 22 likes snack 22 and snack 33.
  • Student 33 likes snack 22.

If students 33, 11, and 22 enter the room in this order, then each student will take exactly one snack.

  • At the beginning, there is one copy each of snack 11, snack 22, and snack 33 in the room.
  • After student 33 enters, they take snack 22. After that, snacks 11 and 33 remain in the room.
  • After student 11 enters, they take snack 11. After that, only snack 33 remains in the room.
  • After student 22 enters, they take snack 33.

Similarly, even if students 33, 22, and 11 enter the room in this order, all students will still take exactly one snack.

Explanation of Sample 2

Both students like all 22 types of snacks, so no matter in what order they enter the room, the first student to enter will take all snacks.

Constraints

  • All given values are integers.
  • 1≤N≤200 0001 \le N \le 200\,000
  • For each integer ii (1≤i≤N1 \le i \le N), 1≤Ci≤N1 \le C_i \le N
  • C1+C2+⋯+CN≤500 000C_1+C_2+\cdots+C_N \le 500\,000
  • For each integer ii (1≤i≤N1 \le i \le N) and integer jj (1≤j≤Ci1 \le j \le C_i), 1≤Ai,j≤N1 \le A_{i,j} \le N
  • For each integer ii (1≤i≤N1 \le i \le N), the CiC_i integers Ai,1,Ai,2,⋯ ,Ai,CiA_{i,1},A_{i,2},\cdots,A_{i,C_i} are pairwise distinct.

Subtasks

  1. (66 points) For each integer ii (1≤i≤N1 \le i \le N), Ci=1C_i=1.
  2. (1111 points) If there exists an order that allows all students to take exactly one snack, then the order 1,2,⋯ ,N1,2,\cdots,N also satisfies the requirement.
  3. (88 points) N≤5N \le 5.
  4. (1212 points) N≤18N \le 18.
  5. (1818 points) N≤300N \le 300.
  6. (2020 points) N≤5 000N \le 5\,000.
  7. (2525 points) No additional constraints.

Translated by ChatGPT 5