Pair all translators so that each pair shares a language, printing the lexicographically smallest pairing or 'impossible'.
Medium7GraphGreedySortingNo attempts yetTime limit5sMemory limit512 MBThe annual International Convention for Phonetic Communication is here again. The attendees come from all over the world and do not all speak the same languages, so the organizers hired translators.
When the convention ends, the organizers want to thank the translators for their work with a dinner at a nearby restaurant. The restaurant only has small tables that seat two people, so the translators have to be split into pairs. The organizers want the translators to enjoy the evening, so the two translators at one table must have a language that both of them speak. Write a program that pairs up all of the translators so that the two people at each table share a language.
The first line contains the number of languages spoken at the convention N and the number of hired translators M. (2≤N≤100, 1≤M≤200)
Each of the next M lines describes one translator and contains the numbers of the two different languages that this translator speaks. Language numbers are integers from 0 to N−1.
Translators are numbered from 0 to M−1 in the order they are given, so the translator on the first of these lines has number 0.
No two translators speak the same two languages. The translators are chosen so that any two languages spoken at the convention can be translated into each other, although it may take several translators.
If every translator can be paired, print M/2 lines with the numbers of the two translators in each pair. Print the smaller number of a pair first, and order the lines by increasing first number. If several pairings are possible, print the one whose sequence of printed numbers, read in output order, comes first in lexicographic order.
If the translators cannot all be paired, print impossible on the first line.