#K68852. Unique Pairs Sequence
Unique Pairs Sequence
Unique Pairs Sequence
You are given a list of unique pairs of integers: \((x,y)\). Your task is to determine whether it is possible to rearrange these pairs into a sequence such that for every two consecutive pairs in the sequence, either the x-coordinate or the y-coordinate is the same.
Formally, given a list of pairs \(P = [(x_1, y_1), (x_2, y_2), \dots, (x_n, y_n)]\), can you rearrange the pairs such that for every adjacent pair \((x_i, y_i)\) and \((x_{i+1}, y_{i+1})\), either \(x_i = x_{i+1}\) or \(y_i = y_{i+1}\)?
If such a sequence exists, print Possible
; otherwise, print Not Possible
.
inputFormat
The first line contains the integer T
denoting the number of test cases. For each test case, the first line contains an integer N
denoting the number of pairs. This is followed by N
lines, each containing two integers representing the \(x\) and \(y\) coordinates of a pair.
outputFormat
For each test case, output a single line with either Possible
if the pairs can be rearranged to meet the conditions, or Not Possible
otherwise.
2
3
1 2
2 2
3 2
4
1 1
2 2
3 1
4 2
Possible
Not Possible
</p>