#C3965. Domino Line Formation
Domino Line Formation
Domino Line Formation
You are given a collection of domino pieces. Each domino is represented by two integers. Your task is to determine whether it is possible to arrange all the dominoes in a line such that the adjacent ends of consecutive dominoes match.
This problem can be modeled using graph theory. Consider each integer as a vertex and each domino as an undirected edge connecting the two vertices. A necessary and sufficient condition for the existence of a valid domino chain (an Eulerian trail) is that:
\( \text{Either all vertices have even degree, or exactly two vertices have odd degree} \)
Additionally, the graph must be connected (ignoring isolated vertices).
You will be given several test cases. For each test case, you need to output Yes
if the domino pieces can be chained together as described, and No
otherwise.
inputFormat
The first line of input contains an integer t representing the number of test cases. Each test case is described as follows:
- The first number is an integer n representing the number of domino pieces.
- This is followed by 2n integers. Every two integers represent a domino piece.
All input is read from standard input (stdin).
outputFormat
For each test case, output a single line containing either Yes
if the domino pieces can form a valid chain or No
otherwise. All output is printed to standard output (stdout).
3
3 1 2 2 3 3 1
4 1 2 2 3 3 4 4 1
2 1 2 3 4
Yes
Yes
No
</p>