#P8503. 「CGOI-2」No mind to think

「CGOI-2」No mind to think

Background

"My king, this child... is not pure... it..."

"Hmm. A vessel must not have the ability to communicate with people, otherwise it may develop thoughts through communication. They should only have the instinct to hunt, and talent for battle. Just like my guards."

"Those are nothing but puppets..."

"A puppet is still better than this guy who can think. Take it away some other day, it is really too noisy... I am tired, I will go for a walk."

The invincible, brave, sexy, mysterious, charming, imposing, diligent, strong, gorgeous, passionate, terrifying, pretty, powerful Gray Prince Zote cursed as he rolled out of the White Palace.

Problem Description

Hallownest has nn stag stations and nn rails. The ii-th rail connects stations uiu_i and viv_i. Initially, each rail is one-way: the first time you traverse rail ii, you can only go from uiu_i to viv_i. After the first traversal, this rail becomes two-way, so you can travel both from uiu_i to viv_i and from viv_i to uiu_i.

Now the White King is at station 11. He will traverse some rails to reach station 22, then from station 22 traverse some rails to reach station 33... and so on, until station xx. Since the White King needs to walk through the whole kingdom as soon as possible to investigate the infection, he asks you: for each integer xx in [2,n][2,n], what is the minimum number of rails that must be traversed?

Input Format

The first line contains an integer nn, indicating the number of stations and rails.

The next nn lines each contain two integers. On the ii-th line, ui,viu_i, v_i means that the first time rail ii is traversed, you can only go from uiu_i to viv_i, and after that it can be traversed in both directions.

Output Format

Output n1n-1 lines, each containing one number. The ii-th number indicates the minimum number of rails traversed when x=i+1x=i+1.

7
1 7
7 6
1 2
2 3
3 4
4 5
5 6
1
2
3
4
5
10
6
1 4
4 2
2 6
6 1
6 3
1 5
2
4
7
9
11
18
14 15
8 12
5 4
10 14
15 17
7 5
3 9
9 18
11 13
1 2
16 10
5 11
5 6
6 8
2 3
2 7
18 16
7 10
1
2
6
7
8
10
13
19
22
26
30
35
40
41
45
49
54

Hint

Sample Explanation

For Sample 1, the map is as follows:

Sample 1 Map

For x=2,3,4,5,6x=2,3,4,5,6, the shortest paths all follow 1234561\to 2\to3\to4\to5\to6, and the answers are 1,2,3,4,51,2,3,4,5.

When x=7x=7, if we still follow the path above, we cannot go directly from station 66 to station 77 via the rail 767\to 6, because this rail is still one-way. Detouring back requires traversing 66 more rails, for a total of 1111 rails.

But if we first traverse 767\to6 once, i.e. follow the path 17671234561\to7\to6\to7\to1\to2\to3\to4\to5\to6, then when we arrive at 66 we can go directly to 77. In total, only 1010 rails are needed, and it also satisfies visiting nodes 171\sim 7 in order, making it better than the previous plan.


Constraints

This problem uses bundled testdata.

ID nn Score
0 6\le6 10pts
1 18\le18 20pts
2 3×103\le3\times10^3 32pts
3 5×105\le5\times10^5 38pts

For 100%100\% of the data, 3n5×1053\le n\le5\times10^5.

The testdata guarantees that starting from station 11, any station is reachable, and there are no multiple edges, self-loops, or 2-cycles.

Translated by ChatGPT 5