Pleasant Tour
Time limit2sMemory limit512 MB
Given a tree of N rides, find an order visiting every ride once where each walking distance is no greater than the one before, using distance and subtree-count queries.
- Level
Hard8 of 10
- Topics
- Tree, Divide and conquer, DFS
- Solved
- No attempts yet
Problem
The largest amusement park in Jakarta has rides, numbered from 0 to . The rides are connected by two-way paths, so for any two rides there is exactly one path between them. The paths are numbered from 0 to . Path connects rides and , and walking along it takes one hour. To avoid crowding, every ride is connected to at most 3 paths.
You want to build a tour that visits every ride exactly once. Walking through several paths to get from one ride to the next is tiresome. To make the tour pleasant, the travel time from each ride to the next must not be longer than the travel time of the step before it. In other words, find a permutation of the integers from 0 to such that for every , the time to walk from to is not greater than the time to walk from to .
You do not have the full map. You can ask the information center up to questions. Each question has two parameters and , where . A question is one of the following two kinds.
- How many hours does it take to walk from ride to ride ? When , the answer is 0.
- Given and , how many rides satisfy this condition: to walk from ride to ride , you must pass through ride ? Ride is included. When , the answer is .
Constraints
- .
- .
- Any two rides can reach each other through the paths.
- Each ride is connected to at most 3 paths. (That is, it is an endpoint of at most 3 paths.)