The kingdom
Time limit1sMemory limit256 MB
Follow the fixed DFS, vertex-split, and Euler-circuit steps to output edge-disjoint even-length trails pairing every odd-degree vertex.
- Level
Hard8 of 10
- Topics
- Graph, DFS, Simulation, Implementation
- Solved
- No attempts yet
Problem
Bytetoria has cities and an even number of bidirectional roads. The road network lets you travel between any two cities of the kingdom.
King Byte loves even numbers. When he learned that some cities have an odd number of roads leaving them, he ordered the network expanded at once.
The advisor knows the treasury well. A project of that size would make the Winter Olympic Games, which the people await, impossible to hold. He plans to convince the king that Bytetoria already has enough even properties, and to ask that the work wait until next year.
First he will surprise the king with the fact that the number of odd-degree cities is even. Then he will pair those cities and, for each pair , choose a route from to that uses an even number of roads. A route does not use the same road twice. Distinct routes do not share a road. A route may visit the same city more than once.
The advisor is sure this will convince the king. He cannot pick the routes himself, so he asks you for help.
Input
The first line contains two integers and (). They are the number of cities and the number of roads. is even.
Each of the next lines contains two integers , (, ), meaning a bidirectional road between cities and . At most one road joins any pair of cities.
You may assume that at least one city has odd degree.
Output
Let be the number of odd-degree cities. is even.
If no set of routes matches the advisor's plan, print NIE on a single line.
Otherwise print routes, uniquely determined as follows.
Build a depth-first spanning tree from city , always expanding the unused adjacent city of smallest index. Split each city into copies and , and add a dummy city . Move each original road onto a pair of copies:
When the search at city sees a road to an already visited city whose visit time is smaller than that of , put that road between and . After the subtree of a tree child of parent is finished, if the number of roads already incident to is odd, put the tree road between and ; if that number is even, put it between and .
For every odd-degree city , add a dummy road between and . Find an Euler circuit of this new graph that starts at . Whenever several unused incident roads remain, take the one with the smallest original index. Dummy roads have index ; if those tie, take the one whose other endpoint has the smaller city index.
Removing the dummy roads from the circuit leaves even-length trails. Print them in circuit order.
Each route uses two lines. The first line has the start city , the end city , and the number of roads , where is even. The second line has road indices in order along the route. Roads are numbered from to in input order.