Senior Postmen
Time limit1sMemory limit256 MB
The program replays the given stack walk on the street graph and prints each cycle it extracts in order.
- Level
Medium5 of 10
- Topics
- Simulation, Graph, DFS, Stack
- Solved
- No attempts yet
Problem

It is the year 2036 and Europe is crowded with senior citizens. To keep them healthy, the European ministry for majority groups (seniors are a majority) suggests that they deliver the small amount of paper mail that is still being sent, usually to seniors. The suggestion is going to be implemented all over Europe.
The ministry devised a "senior postmen system" in the following way. Europe is divided into mail districts. A mail district has a street network of streets and junctions, and every street can be walked in both directions. In each district, arbitrarily many senior citizens are available to be hired as mailmen. Every morning each mailman receives a bag with mail to be delivered on a tour that covers a part of the street network. Every tour must satisfy the three conditions below, and such a tour is called senior compatible.
- It starts at a junction and ends at that same junction.
- It never passes a junction more than once. The seniors shall not be confused.
- It has no street in common with any other tour, so every street in the district is served by exactly one mailman. The seniors shall not fight with each other.
Together the tours must cover the network: every street belongs to exactly one tour.
Given the street network of a mail district, write a program that computes a set of senior compatible tours covering the network.
Input
The input describes the street network.
The first line contains two integers and (, ), the number of junctions and the number of streets. Junctions are numbered from 1 to .
Each of the following lines contains two integers and (, ), meaning that a street connects junctions and .
Every input satisfies the following.
- Two junctions are connected by at most one street.
- From any junction you can reach every other junction by walking along one or more streets.
- A solution exists, that is, a set of senior compatible tours covering the network can be computed.
Output
More than one set of tours can cover the network, so print only the one that the procedure below produces.
At each junction, consider the neighboring junctions in increasing order of number. At the start every street is unused and the stack holds junction 1 alone. Repeat the following until the stack is empty.
- Let be the junction on top of the stack.
- If still has an unused street, let be the smallest-numbered junction that an unused street leads to from , and mark the street between and as used. If is not on the stack, push onto the stack. If is already on the stack, print and the junctions stacked above it on one line, from the lowest to the highest, then remove from the stack every junction above . The junction stays on the stack.
- If has no unused street, then is the only junction on the stack. Remove . If no unused street is left anywhere, stop. Otherwise put the smallest-numbered junction that still has an unused street on the stack by itself and continue.
Each printed line is one senior compatible tour. On a line, write the junction numbers in the order the mailman passes them, separated by one space, with the junction where the tour starts and ends written first and only once. Print the lines in the order the procedure produces them.
Hint
Because a solution exists, every junction has even degree.
While the procedure runs, the junctions on the stack are pairwise different and the street joining two neighboring ones is already used. So in the graph of unused streets only and have odd degree when . An odd degree is not zero, so as long as the stack holds two or more junctions the top junction still has a street to leave by. The procedure never gets stuck in the middle, and it starts over only when the stack has shrunk to a single junction.
A tour contains at least three junctions, because two junctions are connected by at most one street.