Group Division
InterviewTime limit1sMemory limit1024 MB
Assign each chair to group 1 or 2 so that every prefix is balanced within 1 and paired chairs match, choosing the lexicographically smallest sequence.
- Level
Medium6 of 10
- Topics
- Greedy, Union-find, Implementation, Math
- Solved
- No attempts yet
Problem
Math teacher Maria plans to divide her students into two groups during the next lesson. So she now faces a common problem: how do you divide students into two groups in a sensible way? There are students in the class, and the classroom has chairs, numbered from to . When a student arrives in the classroom, they always sit on the free chair that is furthest to the left. So if a total of people show up on a given day, they always sit on chairs .
To divide the students into two groups, Maria has a strategy she wants to use. Before the lesson starts, she chooses a sequence of ones and twos. When the lesson has started, she goes to each student and makes the student sitting on chair end up in group . For the group division to be sensible, two requirements must hold for :
- Maria does not know how many students will come, but no matter how many show up, the difference in size between the two groups must be at most .
- There are pairs of chairs that have the same color. If students sit on such a pair of chairs, they must end up in the same group.
Your task is, given and the pairs of chairs, to find the sequence that comes first in alphabetical order and satisfies the requirements above. If there is no valid , your program must print .
By alphabetical order we mean that two sequences are compared by first checking the first character, if it is the same checking the second, and so on. For example, the sequence 1122 comes before 1211, but after 1112.
Input
The first line contains two integers , the number of chairs in the classroom, and , the number of chair pairs with the same color. Then follow lines, each consisting of two integers, the pairs of chairs that have the same color (1-indexed). Each chair has the same color as at most one other chair.
Output
Print a sequence of ones or twos (without spaces), or if there is no valid solution.