316B1.md (3991B)
1 Source: https://codeforces.com/problemset/problem/316/B1 2 3 B1. EKG 4 5 time limit per test 6 7 0.5 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 In the rush of modern life, people often forget how beautiful the world is. The time to enjoy those around them is so little that some even stand in queues to several rooms at the same time in the clinic, running from one queue to another. 22 23 (Cultural note: standing in huge and disorganized queues for hours is a native tradition in Russia, dating back to the Soviet period. Queues can resemble crowds rather than lines. Not to get lost in such a queue, a person should follow a strict survival technique: you approach the queue and ask who the last person is, somebody answers and you join the crowd. Now you're the last person in the queue till somebody else shows up. You keep an eye on the one who was last before you as he is your only chance to get to your destination) I'm sure many people have had the problem when a stranger asks who the last person in the queue is and even dares to hint that he will be the last in the queue and then bolts away to some unknown destination. These are the representatives of the modern world, in which the ratio of lack of time is so great that they do not even watch foreign top-rated TV series. Such people often create problems in queues, because the newcomer does not see the last person in the queue and takes a place after the "virtual" link in this chain, wondering where this legendary figure has left. 24 25 The Smart Beaver has been ill and he's made an appointment with a therapist. The doctor told the Beaver the sad news in a nutshell: it is necessary to do an electrocardiogram. The next day the Smart Beaver got up early, put on the famous TV series on download (three hours till the download's complete), clenched his teeth and bravely went to join a queue to the electrocardiogram room, which is notorious for the biggest queues at the clinic. 26 27 Having stood for about three hours in the queue, the Smart Beaver realized that many beavers had not seen who was supposed to stand in the queue before them and there was a huge mess. He came up to each beaver in the ECG room queue and asked who should be in front of him in the queue. If the beaver did not know his correct position in the queue, then it might be his turn to go get an ECG, or maybe he should wait for a long, long time... 28 29 As you've guessed, the Smart Beaver was in a hurry home, so he gave you all the necessary information for you to help him to determine what his number in the queue can be. 30 31 Input 32 33 The first line contains two integers *n* (1 ≤ *n* ≤ 103) and *x* (1 ≤ *x* ≤ *n*) — the number of beavers that stand in the queue and the Smart Beaver's number, correspondingly. All willing to get to the doctor are numbered from 1 to *n*. 34 35 The second line contains *n* integers *a*1, *a*2, ..., *a**n* (0 ≤ *a**i* ≤ *n*) — the number of the beaver followed by the *i*-th beaver. If *a**i* = 0, then the *i*-th beaver doesn't know who is should be in front of him. It is guaranteed that values *a**i* are correct. That is there is no cycles in the dependencies. And any beaver is followed by at most one beaver in the queue. 36 37 The input limits for scoring 30 points are (subproblem B1): 38 39 * It is guaranteed that the number of zero elements *a**i* doesn't exceed 20. 40 41 The input limits for scoring 100 points are (subproblems B1+B2): 42 43 * The number of zero elements *a**i* is arbitrary. 44 45 Output 46 47 Print all possible positions of the Smart Beaver in the line in the increasing order. 48 49 Examples 50 51 Input 52 53 ``` 54 6 1 55 2 0 4 0 6 0 56 ``` 57 58 Output 59 60 ``` 61 2 62 4 63 6 64 ``` 65 66 Input 67 68 ``` 69 6 2 70 2 3 0 5 6 0 71 ``` 72 73 Output 74 75 ``` 76 2 77 5 78 ``` 79 80 Input 81 82 ``` 83 4 1 84 0 0 0 0 85 ``` 86 87 Output 88 89 ``` 90 1 91 2 92 3 93 4 94 ``` 95 96 Input 97 98 ``` 99 6 2 100 0 0 1 0 4 5 101 ``` 102 103 Output 104 105 ``` 106 1 107 3 108 4 109 6 110 ``` 111 112 Note 113 114  Picture for the fourth test.