#P3229. [HNOI2013] 旅行
[HNOI2013] 旅行
Description
In the distant country of HX, there lives a traveler named Xiao L. He wants to ride his bicycle across the entire country. There are cities in this country, numbered from to .
Some cities have no attraction that Xiao L wants to visit, while some cities have exactly one such attraction. Every city is one of these two types. As an information science enthusiast, he wrote a program that arranged a shortest route to reach all the attractions he wants to visit (the city numbers along the route are in an arbitrary order). He reaches city first, city as the -th city, and finally ends the trip at city . Xiao L wants to finish this trip in exactly months (), so he needs a sensible plan.
When he arrives at a city, if that city has an attraction he wants to visit, he gains point of happiness; otherwise, he gains point of fatigue due to the tiring journey. One month is enough for him to travel through any number of cities, but he also wants to take some time to rest. He always rests in the last city he reaches in that month (if this city has an attraction, Xiao L will always visit the attraction before resting). Of course, Xiao L wants to have some travel tasks every month. Even if none of the cities he reaches that month has an attraction, he will still arrive at least one new city in that month.
Xiao L cannot arrange the travel plan by himself, so he asks for your help. You need to provide a sequence , where is the city number where Xiao L rests in the -th month. Since he must finish his trip in the last month, must equal . For example, let , , , and . This means: in month he reaches cities and in order and rests in city ; in month he reaches cities and in order and rests in city ; in month he reaches city and rests in city .
There are many such feasible sequences. Let be the absolute value of the difference between the happiness and fatigue obtained in month for a given plan. For the -th feasible plan, let be the maximum of . Xiao L wants to choose a plan that minimizes among all feasible plans.
In fact, there may be multiple plans achieving the same minimal . Since Xiao L loves programming, he has a bit of OCD (and believes that it makes him look cool). Among all the plans with minimal , he wants the lexicographically smallest sequence.
Tips: To compare two sequences lexicographically, compare the first position where they differ, e.g., .
Input Format
The first line contains two space-separated positive integers , denoting the number of cities and the number of months.
The next lines each contain two space-separated integers and . Here is the city number he reaches in the -th step, and is or . If , then city has no attraction Xiao L wants to visit; if , then city has such an attraction. The are pairwise distinct and satisfy , i.e., is a permutation of .
Output Format
Output exactly one line containing space-separated positive integers , which is the planned sequence of rest-city numbers for Xiao L’s trip.
8 3
2 0
3 1
4 1
1 0
5 0
6 1
7 1
8 0
1 6 8
Hint
In the first month, he gains points of happiness and points of fatigue. In the second month, he gains point of happiness and point of fatigue. In the third month, he gains point of happiness and point of fatigue. The maximum difference between fatigue and happiness over the months is , which is the minimal possible value among all plans.
Feasible plans include:
- 1 6 8
- 3 6 8
- 3 1 8
Among these, 1 6 8 is lexicographically smallest.
Constraints: , .
Translated by ChatGPT 5
京公网安备 11011102002149号