Magneto Magnets
Time limit1sMemory limit256 MB
Decide whether all magnets can join into one closed chain with matching polarities at every joint.
Problem
In 2050 a research team on Earth decodes a message from the Magneto civilization. The Magnetos want to hand over StarGate technology that travels faster than light, but first they want to check whether earthlings deserve it, so they sent the following problem.
One component of a StarGate is a straight line built by joining Magneto magnets end to end. A Magneto magnet is not like an Earth magnet.
- Every magnet has two opposite ends, and the polarity of each end is one integer from to .
- The two ends of one magnet may carry the same polarity.
- Two ends with the same polarity attract each other. Two ends with different polarities also attract each other, but the magnet is destroyed the moment they touch.
So when the magnets are joined into a line, the two ends that touch must carry the same polarity.
The Magnetos send several magnet configurations and ask, for each one, whether all of its magnets can be used in a single line so that the first end and the last end of the line carry the same polarity. You may choose the order of the magnets and the orientation of each magnet freely.
Decide for each configuration whether such an arrangement exists.
Input
The first line contains the number of test cases .
The first line of each test case contains the number of magnets . Each of the next lines contains two integers, the polarities of the two ends of one magnet. Every polarity is an integer from to .
Output
Print one line for each test case: Case #n: Yes if the required arrangement exists, and Case #n: No otherwise. Here is the test case number, counted from in input order. Put exactly one space after Case #n:.