|시간 제한||메모리 제한||제출||정답||맞은 사람||정답 비율|
|2 초||512 MB||0||0||0||0.000%|
The table above shows the content of an array A, with the indices in the top row. The array stores the pointers of a linear linked list. In this task, a pointer is simply an integer value. The pointer in the first node is stored in A, that is, the value A indicates the location of the second node. The pointer in the second node is stored in A[A], whereas the pointer in the third node is stored in A[A[A]], and so on. If the pointer has value −1, it indicates end of the linked list. In the above example, value of A is 2, and thus the second pointer is stored in A. The value of A is 4 and thus the third pointer is stored in A. The value of A is -1, indicating that there is no more subsequent node. The sequence of pointers is shown below and the linked list has 3 nodes.
A = 2 → A = 4 → A = −1
You have received the entries of an array as described above, and you are also told that one of the entries is being replaced. However, the location of that entry, and its new value are not known. Note that it is possible that the new value is the same as the original value, that is, the array remains unchanged. As a forensic expert, you want to recover the original array. To achieve that, you want to modify a single entry, so that the modified array represents a linked list with the largest number of nodes. Consider the above example:
Among all possible changes to one entry, including not changing any entry, the recovered list with 5 nodes is the largest possible. Thus, it is likely that the original entry of A is 7. Here, your task is to compute the size of the largest possible recovered linked list.
Your program must read from the standard input. The input consists of two lines. The only integer in the first line is N, the size of the array. The second line gives the N integers in the array, starting from A. Each integer in the array is larger or equal to -1, and smaller than N.
Your program must write to the standard output the number of nodes in the largest recovered linked list.
10 2 5 4 4 -1 1 -1 3 0 8
4 0 0 0 0
Changing A to −1 give a linked list of 1 node.
6 1 2 3 4 5 -1
The input (without any change) has the largest linked list.
10 2 5 4 4 0 1 -1 3 0 8
Changing A to 6 gives a linked list of 4 nodes.
10 2 5 4 4 -1 1 -1 6 0 8
Changing A to 7 gives a linked list of 5 nodes.