QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 100

#318. Inspection

Statistics

The railway network of the Byteotian Railways (BR) consists of bidirectional tracks connecting certain pairs of stations. Each pair of stations is connected by at most one segment of tracks. Furthermore, there is a unique route from every station to every other station. (The route may consist of several segments of tracks, but it may not pass through any station more than once.)

Byteasar is an undercover inspector of the BR. His job is to pick one of the stations (denote it by $S$) for centre of his operations and to travel to all other stations. His journey should be as follows:

  • Byteasar starts in station $S$.
  • Next, he picks one of the stations he did not yet control and goes to it along the shortest path (by train, of course), inspects the station, and then goes back to $S$.
  • The crooked employees of BR warn one another of Byteasar's comings. To deceive them, Byteasar picks the next station for control in such a way that he sets off from the station in different direction than the last time, i.e., along a different segment of tracks leaving from $S$.
  • Each station (except $S$) is inspected exactly once.
  • After inspecting the last station Byteasar does not come back to $S$.

The travel time along every segment of tracks takes the same amount of time: one hour.

Byteasar intends to consider all the stations as the initial station $S$. For each of them he wants to know the order of inspecting the remaining stations that minimises the total travel time, provided that it is possible at all for that particular $S$.

Input Format

The first line of the standard input contains a single integer $n$ ($1 ≤ n ≤ 1\,000\,000$) that denotes the number of stations. These are numbered from $1$ to $n$. The following $n-1$ lines specify the track segments, one per line. Each of them holds two integers $a$,$b$ ($1 ≤ a,b ≤ n$, $a≠b$), separated by a single space, indicating that there is a track segment connecting the stations $a$ and $b$. Each track segments appears exactly once in the description.

In tests worth at least $30\%$ of the points it holds additionally that $n ≤ 2\,000$.

Output Format

Your program should print $n$ lines on the standard output, each holding a single integer. The one in the $i$-th line should be the minimum number of hours Byteasar has to spend travelling to inspect the stations when $S=i$ - if inspecting them all is possible for $S=i$; if it is not, the $i$-th line should hold the number $-1$.

Example

Sample Input

9
3 6
2 4
2 6
2 5
1 7
2 7
8 9
7 8

Sample Output

-1
23
-1
-1
-1
-1
-1
-1
-1
problem_318_1.gif

The figure illustrates the railway network as specified in the example. It is possible to inspect all the stations only for S=2. One optimal order of inspection is: 7, 4, 8, 6, 1, 5, 3, 9. It results in 23 hours of travel.

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.