This page is still under construction.

Parts of this page are still being built. What you see may change.

Magneto Magnets

Time limit1sMemory limit256 MB

Summary
Decide whether all magnets can join into one closed chain with matching polarities at every joint.
Level

Medium5 of 10

Topics
Graph, DFS
Solved
No attempts yet

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 11 to 2525.
  • 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 NN (1≤N≤100)(1 \le N \le 100).

The first line of each test case contains the number of magnets MM (1≤M≤1000)(1 \le M \le 1000). Each of the next MM lines contains two integers, the polarities of the two ends of one magnet. Every polarity is an integer from 11 to 2525.

Output

Print one line for each test case: Case #n: Yes if the required arrangement exists, and Case #n: No otherwise. Here nn is the test case number, counted from 11 in input order. Put exactly one space after Case #n:.

Examples1

  1. Example 1

    Input
    2
    5
    1 2
    2 3
    3 4
    4 5
    5 6
    5
    2 1
    2 2
    3 4
    3 1
    2 4
    
    Expected output
    Case #1: No
    Case #2: Yes