1020B.md (2516B)
1 Source: https://codeforces.com/problemset/problem/1020/B 2 3 B. Badge 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of $n$ students doing yet another trick. 22 23 Let's assume that all these students are numbered from $1$ to $n$. The teacher came to student $a$ and put a hole in his badge. The student, however, claimed that the main culprit is some other student $p_a$. 24 25 After that, the teacher came to student $p_a$ and made a hole in his badge as well. The student in reply said that the main culprit was student $p_{p_a}$. 26 27 This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge. 28 29 After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna. 30 31 You don't know the first student who was caught by the teacher. However, you know all the numbers $p_i$. Your task is to find out for every student $a$, who would be the student with two holes in the badge if the first caught student was $a$. 32 33 Input 34 35 The first line of the input contains the only integer $n$ ($1 \le n \le 1000$) — the number of the naughty students. 36 37 The second line contains $n$ integers $p_1$, ..., $p_n$ ($1 \le p_i \le n$), where $p_i$ indicates the student who was reported to the teacher by student $i$. 38 39 Output 40 41 For every student $a$ from $1$ to $n$ print which student would receive two holes in the badge, if $a$ was the first student caught by the teacher. 42 43 Examples 44 45 Input 46 47 ``` 48 3 49 2 3 2 50 ``` 51 52 Output 53 54 ``` 55 2 2 3 56 ``` 57 58 Input 59 60 ``` 61 3 62 1 2 3 63 ``` 64 65 Output 66 67 ``` 68 1 2 3 69 ``` 70 71 Note 72 73 The picture corresponds to the first example test case. 74 75  76 77 When $a = 1$, the teacher comes to students $1$, $2$, $3$, $2$, in this order, and the student $2$ is the one who receives a second hole in his badge. 78 79 When $a = 2$, the teacher comes to students $2$, $3$, $2$, and the student $2$ gets a second hole in his badge. When $a = 3$, the teacher will visit students $3$, $2$, $3$ with student $3$ getting a second hole in his badge. 80 81 For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge.