Bakery Line Order
Time limit1sMemory limit128 MB
Given a friendship graph, find any arrival order in which people join a line by a friend-insertion rule so the final line is exactly 1..N, or report impossible.
- Level
Hard8 of 10
- Topics
- Graph, Greedy, Simulation
- Solved
- No attempts yet
Problem
Kruhko the baker makes very tasty bread. Before the bakery opens, people form a long line using the following rule.
When a new person arrives, they first check whether any of their friends are already standing in line. If at least one friend is present, the newcomer stands immediately in front of the friend who is closest to the bakery entrance. If none of their friends are in line, the newcomer stands at the end of the line.
People are numbered 1, 2, ..., N, and the given friendship relation is mutual.
For example, suppose N = 4, the friendships are 1-2, 1-3, and 3-4, and the arrival order is (2, 4, 3, 1). Then the line is formed as follows.
2arrives. The line is(2).4arrives. Since4is not friends with2,4stands at the end. The line is(2, 4).3arrives. Since3is friends with4,3stands immediately in front of4. The line is(2, 3, 4).1arrives.1is friends with both2and3, but2is closer to the entrance, so1stands immediately in front of2. The line is(1, 2, 3, 4).
Find any arrival order that makes the final line exactly (1, 2, ..., N).
Input
The first line contains two integers N and M, the number of people and the number of friendships.
2 ≤ N ≤ 300 000, 1 ≤ M ≤ 1 000 000
Each of the next M lines contains two integers a and b, meaning that people a and b are friends. Both integers are between 1 and N, inclusive.
Output
Print a valid arrival order as numbers separated by spaces. If more than one order is possible, print any one of them.
If no such order exists, print only -1.