Under construction

Still being built. Things may change

Emergency Contact Network

Time limit1sMemory limit128 MB

Summary
Design a fastest call schedule so a class leader's contact network reaches every student, respecting call directions and one-call-per-student, minimizing total minutes.
Level

Medium7 of 10

Topics
Tree, Greedy, Graph
Solved
No attempts yet

Problem

A class has a contact network so that every student can be reached during a school-break emergency. Student 1 is the class leader, and the other students are numbered from 2 to n. If student a knows student b's phone number, the network has a directed arrow from a to b.

The contact process starts with the class leader. Once a student has been contacted, that student may call students whose phone numbers they know. Every student except the leader must receive exactly one call. Each call takes 1 minute, and if one person must call several students, the calls are made one after another.

The picture above is one contact network. Each numbered rectangle is a student, and each arrow shows a direction in which a call can be made.

The table below shows two possible plans for this network. Plan A contacts everyone in 4 minutes, while Plan B takes 5 minutes.

Plan APlan B
SenderReceiverMinuteSenderReceiverMinute
141121
132132
123143
353353
462464
574574
693585
684695

Write a program that outputs a fast emergency contact plan that reaches every student using the given network.

Input

The first line contains the number of students n. The integer n is at most 100.

Each of the next n lines contains one student's number followed by the numbers of the students whose phone numbers that student knows, separated by spaces. If the student knows no phone numbers, the line contains only that student's number.

Output

On the first line, print the total time in minutes needed to contact every student.

Then print one line for minute 1, minute 2, and so on up to the total time. Each line lists the calls made during that minute as sender-number receiver-number pairs. If several calls are made in the same minute, print all pairs on that line separated by spaces.

The plan you print must follow the directions of the contact network, and every student except the leader must receive exactly one call. The plan should be fast enough to be no slower than the reference limit.

Examples1

  1. Example 1

    Input
    9
    1 2 3 4
    2
    3 5
    4 6
    5 7 8 9
    6 8 9
    7
    8
    9
    
    Expected output
    4
    1 4
    1 3 4 6
    1 2 3 5 6 9
    5 7 6 8