Even Separation
Time limit1sMemory limit256 MB
Split the vertices of an undirected graph into two parts so that every vertex has even degree in the subgraph induced on its own part.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Math, Implementation
- Solved
- No attempts yet
Problem
The good king of the Even Kingdom is dying, and two of his sons, Alfred and Brian, have an equal right to rule the kingdom. The king has decided to split the kingdom into two parts so that each son can rule his part alone. Of course, the split must be even.
The kingdom has cities, some pairs of which are connected by bidirectional roads. No road connects a city to itself, and no two cities are connected by more than one road. After the split, each city belongs either to Alfred or to Brian. Every road connecting cities that belong to different brothers is then destroyed. The split is even if, after those roads are destroyed, every city is directly connected to an even number of other cities, that is, if every vertex of the graph representing the road network has even degree.
Time is short. Help the good king find an even separation of his kingdom.
Some pairs of cities may be unreachable from each other by roads to begin with, and in an even separation some cities within one part may be unreachable from each other by the remaining roads.
Input
The first line contains two space-separated integers and , the number of cities and roads in the Even Kingdom (, ).
The next lines describe the roads. The -th of these lines contains two space-separated integers, the numbers of the cities connected by the -th road. Cities are numbered starting from 1. No pair of cities is connected by more than one road, and no road connects a city to itself.
Output
If a separation is possible, print one line of characters. The -th character is "A" if the -th city belongs to Alfred and "B" if it belongs to Brian. If there are several possible answers, print any of them.
If no separation is possible, print one line containing "IMPOSSIBLE".
Hint
One of the brothers may receive no cities at all. Nobody ever said it had to be fair.