#P3876. Valid Sequence with Distinct Constraints
Valid Sequence with Distinct Constraints
Valid Sequence with Distinct Constraints
You are given an integer n
representing the length of a sequence and an integer m
representing the number of distinct constraints. The sequence consists of digits chosen from the set \(\{0,1,2,3\}\). A sequence is considered legal if it satisfies the following two conditions:
- For every pair of adjacent elements, the ordered pair does not belong to the forbidden set \(\{00, 11, 22, 33, 02, 20, 23, 32, 13, 31\}\). In other words, if the consecutive elements are \(a\) and \(b\) then \((a,b)\) must not be any of the above pairs.
- There are
m
constraints. Each constraint is given as a set of positions \(\{p_1, p_2, \dots, p_L\}\) (the positions are 1-indexed) indicating that the digits at those positions in the sequence must all be distinct. For example, the constraint \(\{1, 5, 11\}\) requires that the 1st, 5th, and 11th elements are pairwise different.
Your task is to determine whether there exists a legal sequence satisfying these conditions. If such a sequence exists, output YES
on the first line followed by any valid sequence on the second line (the sequence should be printed as n
space-separated digits). Otherwise, output NO
.
Note: All formulas are represented in LaTeX format. The forbidden adjacent pairs are given by \(\{00, 11, 22, 33, 02, 20, 23, 32, 13, 31\}\).
inputFormat
The input begins with a line containing two integers n
and m
separated by a space.
If m > 0
, each of the next m
lines describes a constraint. Each constraint line starts with an integer L
, indicating the number of positions in this constraint, followed by L
integers (1-indexed) representing the positions whose digits must all be distinct.
Example:
3 1 3 1 2 3
outputFormat
If a legal sequence exists, output YES
on the first line and then a valid sequence of n
space-separated digits on the second line. If no such sequence exists, output NO
.
sample
3 0
YES
0 1 0
</p>