Jack's Socks
InterviewTime limit1sMemory limit512 MB
Given an undirected graph of similar socks, decide whether a perfect matching exists and is unique, and print that matching if it is.
- Level
Medium6 of 10
- Topics
- Graph, DFS, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
Jack is a scientist, which means he does not pay much attention to what he wears. He does not know the names of more than six colors and cannot tell subtly different shades apart. Today he is leaving for a conference, and he has just pulled a heap of unpaired socks out of the washing machine and must pair them up.
Jack can tell whether two socks look similar, and he is only willing to put two socks together as a pair if they seem similar to him. However, the similarity relation is not necessarily transitive: Jack may find sock A similar to B and B similar to C, yet be able to tell A and C apart and consider them not similar.
Jack wants to know whether there is exactly one way to pair up all of his socks so that every pair consists of two similar socks. Write a program that decides this and, when the pairing is unique, prints it.
Input
The first line contains a positive integer , the number of test cases. Then test cases follow.
The first line of each test case contains two integers and , separated by a single space (, ). is even and is the number of socks, which are numbered from to . Each of the next lines contains two different integers and (), separated by a single space, meaning that socks and are similar. Every similar pair is listed exactly once: if appears, then neither nor appears again.
Output
For each test case, decide whether there is exactly one way to pair all of the socks so that each pair consists of two similar socks.
If there is no such pairing, or more than one, print a single line containing NO.
Otherwise print YES on the first line, followed by lines describing the unique pairing. Each line contains one pair with , and the pairs must be sorted in increasing order of their first element: for any two consecutive pairs and , .