#K75647. Consecutive Domino Chain
Consecutive Domino Chain
Consecutive Domino Chain
You are given a set of domino tiles and a list of allowed swap operations. Each domino tile is represented as a pair of integers \((a, b)\). A domino tile \((a, b)\) can be placed consecutively with another domino \((c, d)\) if and only if \(b = c\). You may perform the given swaps (each swap exchanges two domino tiles' positions) before forming the chain. Your task is to determine the maximum number of domino tiles that can be arranged in a consecutive chain after performing all the allowed swaps.
Input/Output Details:
- The first line of input contains two integers \(n\) and \(m\), where \(n\) is the number of domino tiles and \(m\) is the number of swaps.
- The next \(n\) lines each contain two integers representing the domino tile values.
- The following \(m\) lines each contain two integers \(i\) and \(j\) (0-indexed) indicating that the domino at index \(i\) should be swapped with the domino at index \(j\).
After processing the swaps, determine the largest possible chain length where consecutive dominoes satisfy the condition \(b = c\). Print the chain length as a single integer.
Note: Each swap operation is performed exactly once in the order they are given.
inputFormat
The input is read from standard input (stdin). It consists of multiple lines:
- The first line contains two space-separated integers n and m, where n is the number of domino tiles and m is the number of swap operations.
- The next n lines each contain two integers representing a domino tile.
- The following m lines each contain two integers i and j representing a swap between the domino at index i and the domino at index j (0-indexed).
outputFormat
Output a single integer to standard output (stdout): the maximum number of consecutive domino tiles that can be arranged after performing the provided swaps.## sample
4 1
1 2
2 3
3 4
5 1
0 3
4