Race Map Labeling
Time limit2sMemory limit256 MB
Simulate the smallest-first label-flipping process on the graph and print every place with its final label.
- Level
Medium5 of 10
- Topics
- Simulation, Graph
- Solved
- No attempts yet
Problem
The organizing committee of a motorcycle race labels the contest map. The map has places numbered through . Every road is bidirectional and joins two different places.
Each place gets the label for regular or for service. If roads reach place , then at most of its neighbors may carry the same label as place . Several labelings usually satisfy that rule, so only the labeling produced by the following procedure counts as correct.
- Give every place the label .
- While some place breaks the rule, take the smallest-numbered such place and flip its label: becomes , and becomes .
- Repeat step 2 until no place breaks the rule.
Each flip raises the number of roads whose two ends carry different labels by at least , so the procedure always stops after finitely many flips.
Input
The first line contains the number of places . ()
Each of the next lines describes one place, line describing place . () A line has this form.
number_of_neighbors: neighbor1 neighbor2 ... neighborm
number_of_neighbors is the number of roads reaching place , followed by the numbers of the neighboring places separated by spaces. The line 2: 1 2 means two neighbors, numbered and .
No road joins a place to itself, and two places are joined by at most one road. If appears in the neighbor list of place , then appears in the neighbor list of place . The line of a place with no neighbor ends right after the colon.
Output
Print on the first line. Then print lines, one per place starting from place , each carrying the label the procedure produced. A line has this form.
label number_of_neighbors: neighbor1 neighbor2 ... neighborm
label is for a regular place and for a service place. Print the neighbor list in the order it was given in the input. Separate tokens with one space, and put the colon directly after number_of_neighbors. The line of a place with no neighbor ends right after the colon.