QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#21234. Learning Languages

Statistics

Farmer John's $N$ $2 \le N \le 10,000$ cows, conveniently numbered $1..N$, are fluent in some $M$ $1 \le M \le 30,000$ languages, also conveniently numbered from $1..M$. Cow $i$ can speak in $K_{i}$ $1 \le K_{i} \le M$ languages, namely $L_{i1}$, $L_{i2}$,..., $L_{iK_i}$ $1 \le L_{ij} \le M$. FJ's cows aren't THAT smart, so the sum of $K_{i}$ over all cows $i$ is at most 100,000.

Two cows can't directly talk to each other unless both speak a common language. However, cows can pass messages along, translating if necessary. In other words, cows $A$ and $B$ can have a conversation if and only if there exists a sequence of cows $T_{1}$, $T_{2}$, ..., $T_{k}$ such that $A$ and $T_{1}$ share a language, $T_{1}$ and $T_{2}$ share a language, etc., and $T_{k}$ and $B$ share a language.

Farmer John wishes that his cows could be even more social, so he wants all his cows to be able to socialize with any other cow. He can buy books to teach any one of his cows any language he pleases. Being a fairly frugal farmer, FJ wants to purchase the minimum number of books necessary to enable all of his cows to speak to each other. Help him determine:

  • The minimum number of books he must purchase
  • Any set of books assigned to cows in any order which will help him meet this goal; a program will grade your output.

By way of example, suppose there are three cows named Alberta, Bessie, and Contessa along with three languages denoted as #1, #2, and #3. Alberta can speak languages #2 and #3, Bessie can speak language #2, and Contessa can speak language #1. Currently, Alberta and Bessie can talk to each other, but Contessa is left alone.

                   #1   #2   #3
       Alberta           x    x
       Bessie            x
       Contessa     x

FJ wants to fix this situation, so he can buy Contessa a book to teach her language #2. This will ensure all cows speak the same language, so they can all communicate with one another.

Note that an alternate solution exists: instead, FJ could buy Contessa a book to teach her language #3. Not all cows would speak the same language, but this would still be a valid solution because Contessa could communicate through Alberta (who also speaks language #3) if she wants to talk to Bessie. Other alternatives exist, and any valid alternate solution will also be accepted.

Input

  • Line 1: Two space-separated integers: $N$ and $M$
  • Lines $2..N+1$: Line $i+1$ describes the languages that cow $i$ can speak with $(K_i)+1$ space-separated integers: $K_{i}$, $L_{i1}$, $L_{i2}$,...,$L_{iK_i}$.

Output

  • Line 1: A single integer that is the minimum number of books that FJ must purchase.
  • Lines $2..B+1$: Line $i+1$ contains two space-separated integers: the language id # and the id # of the cow to receive book $i$. If multiple solutions exist, print any one.

Examples

Input 1

3 3
2 3 2
1 2
1 1

Output 1

1
2 3

Problem credits: Albert Gu, 2011

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.