Alternating Current
Time limit3sMemory limit1024 MB
Each of M circular arcs must get a direction (clockwise or counter-clockwise) so that every segment lies in at least one arc of each direction, or report impossible.
- Level
Medium7 of 10
- Topics
- Graph, BFS, Implementation, Greedy
- Solved
- No attempts yet
Problem
Fredrik has a model railway consisting of segments connected in a circle. The segments are numbered in clockwise order. Electricity is supplied through curved wires running along the circle, and every segment is covered by at least one wire.
Fredrik wants to install a switch on every segment. The switches need alternating current. In Fredrik's view, alternating current means current flowing in both directions. Each wire carries current in one direction only, clockwise or counter-clockwise, and Fredrik can choose the direction of every wire. The directions must be chosen so that every segment is covered by at least one wire with clockwise current and at least one wire with counter-clockwise current.
Find such a choice of directions if one exists.

The figure above shows a solution for the first sample. The curved arrows outside the railway are the wires, and each arrow direction is Fredrik's choice of current direction. Reversing every arrow gives another valid solution.
This idea makes sense because the railway is Swedish, and in Sweden every switch (in Swedish växlar) uses alternating current (in Swedish växelström).
Input
The first line contains two integers and , the number of segments and the number of wires.
Each of the next lines contains two integers and with . The wire covers segments . If is smaller than , the interval wraps around the circle and covers segments . If , the wire covers only that single segment.
Output
Output a single line with characters, each or 1. The th character must be 0 if the current in the th input wire flows clockwise, and 1 if it flows counter-clockwise. If several solutions exist, output any of them.
If no valid solution exists, output impossible.