#K68147. Valid Domino Sequence

    ID: 32800 Type: Default 1000ms 256MiB

Valid Domino Sequence

Valid Domino Sequence

Given a sequence of domino tiles represented as pairs of integers \( (a_i, b_i) \), your task is to determine whether these tiles can be arranged in a line such that the adjacent tiles match. Note that each domino can be flipped; that is, a domino \( (a_i, b_i) \) may be used as either \( (a_i, b_i) \) or \( (b_i, a_i) \).

Formally, an arrangement is valid if for a permutation of all domino tiles \( d_1, d_2, \ldots, d_n \) (possibly flipped), the right-hand number of \( d_i \) equals the left-hand number of \( d_{i+1} \) for all \( 1 \le i < n \).

Determine whether such an arrangement exists. If yes, output YES; otherwise, output NO.

inputFormat

The input is given in the following format:

( n ) ( a_1 ) ( b_1 ) ( a_2 ) ( b_2 ) ... ( a_n ) ( b_n )

  • The first line contains a single integer \( n \) representing the number of domino tiles.
  • Each of the following \( n \) lines contains two space-separated integers \( a_i \) and \( b_i \) representing the numbers on the domino tile.

outputFormat

Output a single line containing either YES if there exists a valid arrangement of the dominoes, or NO otherwise.## sample

4
1 2
2 3
3 4
4 5
YES