#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 stag stations and rails. The -th rail connects stations and . Initially, each rail is one-way: the first time you traverse rail , you can only go from to . After the first traversal, this rail becomes two-way, so you can travel both from to and from to .
Now the White King is at station . He will traverse some rails to reach station , then from station traverse some rails to reach station ... and so on, until station . 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 in , what is the minimum number of rails that must be traversed?
Input Format
The first line contains an integer , indicating the number of stations and rails.
The next lines each contain two integers. On the -th line, means that the first time rail is traversed, you can only go from to , and after that it can be traversed in both directions.
Output Format
Output lines, each containing one number. The -th number indicates the minimum number of rails traversed when .
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:

For , the shortest paths all follow , and the answers are .
When , if we still follow the path above, we cannot go directly from station to station via the rail , because this rail is still one-way. Detouring back requires traversing more rails, for a total of rails.
But if we first traverse once, i.e. follow the path , then when we arrive at we can go directly to . In total, only rails are needed, and it also satisfies visiting nodes in order, making it better than the previous plan.
Constraints
This problem uses bundled testdata.
| ID | Score | |
|---|---|---|
| 0 | 10pts | |
| 1 | 20pts | |
| 2 | 32pts | |
| 3 | 38pts |
For of the data, .
The testdata guarantees that starting from station , any station is reachable, and there are no multiple edges, self-loops, or 2-cycles.
Translated by ChatGPT 5