Time limit: 1 second Memory limit: 256 MB After much searching Marcello decided to…
- User
Time limit: 1 second Memory limit: 256 MB After much searching Marcello decided to invite Alberto to his house to celebrate together. He's going to show off his backyard water system to Alberto, but since he's too late, he's asking for your help!
Marcelo provides you with a map of the water supply network of Abanama. This network can be represented as a directed and connected graph (regardless of the direction of the edges) where each vertex has exactly one output pipe (the other end of the output pipe may be connected to the vertex itself). The way of moving water is that if per minute t t some water in a vertex like u u should be in min t + 1 t+1 all this water through the head outlet pipe u u is transferred to the other end of the tube.
Marcello first pours some water into all vertices that do not have an inlet pipe (assuming that the time spent doing this is negligible) and waits until the water feature is fixed, i.e. when from then on any vertex that in the distant future (e.g. 1 0 100 10 100 minutes) at least once it passes through the water so that it still has water. (The chieftains may have water in addition to them) Also, he wants Alberto to see the water feature fixed when he comes to his house. Help Marcello figure out that the water feature is fixed for at least a few minutes after he pours water into the vertices.
input In the first line of the input number n n is said to represent the number of vertices of the water supply network. 2 ≤ n ≤ 100
000 2≤n≤100 000 In the second input line n n is the number that is the number i iam ( O i O i ) is the vertex number that the exit pipe from the vertex i i is connected to it. (Water from the top i i to the top O i O i pours) 1 ≤ O i ≤ n 1≤o i ≤n It is guaranteed that there is initially water in at least one of the vertices.
output Print the minimum minute after which the water feature is always constant in the only output line (suppose Marcelo pours water on the vertices without inlet pipes at the zeroth minute), if there is no such time, output the statement no party.
Example Sample entry 1 5 2 3 1 3 2 Plain text Sample output 1 no party Plain text Since the set of vertices that contain water is never fixed, the answer is no party.
Sample input 2 7 2 3 1 1 2 1 6 Plain text Sample output 2 2 Plain text After two minutes, there will be water in vertices number 1, 2, and 3, and after that, water will remain in the same vertices, so the answer will be equal to two.
Sample entry 3 8 2 3 1 3 1 2 1 7 Plain text Sample output 3 1 Plain text At the beginning (minute 0) the vertices 4 , 5 , 6 , 8 4,5,6,8 are full of water, then after a minute the vertices 1 , 2 , 3 , 7 1,2,3,7 and from then on always vertices 1 , 2 , 3 1,2,3, resulting in the first place where the vertices 1 , 2 , 3 1, 2, 3 have come, which means 1 minute is the desired time. write code in java17
- Assistant