| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 3 초 | 2048 MB | 15 | 8 | 8 | 53.333% |
Well-hidden in the atrium of an abandoned house, you have found an ancient book that uncovers the most well-kept secret of the city of Bonn. Deep below the city, there is a system of $N$ caves, connected by $M$ water channels. Within each water channel there's a one-directional magical current that can quickly transport a boat along the channel. The cave system currently has exactly one exit that is located in cave $N-1$.
You are very excited about your discovery and cannot wait to explore the caves! However, the cave system is inhabited by a troll who likes to have some fun with uninvited visitors. The troll has some limited magical power – which he can use at most once during your visit – to modify the cave system and make it harder for you to reach the exit.
Your visit to the cave system will consist of a sequence of rounds. Each round will be as follows:
Additionally, whenever you are in the same cave as the exit, you will immediately use it to leave the cave system. Note that this can even happen during a round if you are in cave $0$ and the troll decides to use his magical power.
Your goal is to leave the cave system as quickly as possible to be in time for the closing ceremony of the EGOI. The troll's goal is exactly the opposite; he wants to keep you in his caves for as long as possible. The troll always knows your location and he will pick the moment at which to use his magical power in a way that serves his goal the best.
Separately for each cave $c$ ($0 \leq c \leq N-2$) consider the scenario in which you start in cave $c$. For each of these scenarios, determine the smallest number of moves in which you can definitely reach an exit from cave $c$, no matter when the troll chooses to use his power.
Assuming the spell is not used, every cave is reachable from cave $0$, and cave $N-1$ is reachable from every cave.
The first line of the input contains two integers, $N$ and $M$, where $N$ is the number of caves and $M$ is the number of water channels. The next $M$ lines of the input each contain two integers, $a_i$ and $b_i$, representing a channel that right now can be used to travel from cave $a_i$ to cave $b_i$. There is no channel connecting a cave to itself. For each pair of caves there is at most one channel in each direction.
Output a line with $N-1$ integers, where the $i$th integer, $0 \leq i \leq N-2$, is the smallest number of moves within which you can definitely reach an exit if starting from cave $i$.
Note that you do not output the time for cave $N - 1$ (as you would just exit this cave immediately).
| 번호 | 배점 | 제한 |
|---|---|---|
| 1 | 12 | $M=N-1$, $a_i = i$ and $b_i = i + 1$ for all $i$. In other words, the cave system forms a path $0 \rightarrow 1 \rightarrow 2 \rightarrow \ldots \rightarrow N-1$ |
| 2 | 15 | For each $0 \le i \le N-2$, there is a direct channel from cave $i$ to cave $N-1$. Note there can be additional channels. |
| 3 | 20 | $N, M \leq 2\,000$ |
| 4 | 29 | After leaving any cave, it is not possible to travel back to it (until the direction reversal). In other words, the channels form a directed acyclic graph. |
| 5 | 24 | No additional constraints |
5 6 0 1 1 2 1 3 2 4 3 4 0 3
2 2 2 1
7 10 2 6 5 3 4 2 1 6 2 3 3 6 4 5 0 4 4 1 0 1
2 1 2 3 2 4
For the first sample, consider the case in which you start in cave 1. Since you do not know when the direction reversal will happen, you should start moving towards the exit at cave 4. You could do that via either cave 2 or cave 3. Going via cave 3 is the better option here since in case the direction reversal happens while you are there, you will now have a channel you can use to travel from cave 3 directly to cave 0 where you'll exit the cave system.
More precisely, there are only three possibilities for when the troll will decide to use his magical power:
In the first option you only had to make one move, in each of the other options you made two moves. This means the answer for this case is $\max(1,2,2) = 2$.
Note that if you choose to go from cave 1 to cave 2, the troll can force you to make three moves.
The first and second samples satisfy the constraints of test groups 3, 4 and 5. The third sample satisfies the constraints of all test groups. The fourth sample satisfies the constraints of test groups 3 and 5, and is illustrated below.