#K48112. Domino Chain Formation

    ID: 28349 Type: Default 1000ms 256MiB

Domino Chain Formation

Domino Chain Formation

You are given a set of n domino pieces. Each domino is represented by two numbers (a, b). Your task is to determine whether it is possible to arrange all the domino pieces in a sequence such that for every two consecutive dominoes the number on the right end of the preceding domino is equal to the number on the left end of the following domino.

This problem can be modeled using graph theory. Each domino represents an edge connecting two vertices. The arrangement is possible if and only if the graph is connected (ignoring isolated vertices) and it has an Eulerian path. Recall that for an undirected graph, a necessary and sufficient condition for the existence of an Eulerian path is that the graph is connected and either no vertices or exactly two vertices have an odd degree. In mathematical terms, let \(d(v)\) denote the degree of vertex \(v\). Then the configuration allows an Eulerian path if and only if:

[ \text{Number of vertices with } d(v) \text{ odd} = 0 \quad \text{or} \quad 2. ]

If the above condition is met, output Yes, otherwise output No.

inputFormat

The input is given via standard input (stdin) and has the following format:

  • The first line contains an integer n (1 ≤ n ≤ 105) representing the number of domino pieces.
  • Each of the next n lines contains two integers a and b separated by a space, indicating the numbers on the two ends of a domino piece.

It is guaranteed that all numbers are integers within a reasonable range.

outputFormat

Output a single line to standard output (stdout) containing either Yes if the dominoes can be arranged to form a continuous chain, or No otherwise.

## sample
3
1 2
2 3
3 1
Yes

</p>