Algorithm


B. Badge
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Let's assume that all these students are numbered from 11 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 pa��.

After that, the teacher came to student pa�� and made a hole in his badge as well. The student in reply said that the main culprit was student ppa���.

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.

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.

You don't know the first student who was caught by the teacher. However, you know all the numbers pi��. 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.

Input

The first line of the input contains the only integer n (1n10001≤�≤1000) — the number of the naughty students.

The second line contains n integers p1�1, ..., pn�� (1pin1≤��≤�), where pi�� indicates the student who was reported to the teacher by student i.

Output

For every student a from 11 to n print which student would receive two holes in the badge, if a was the first student caught by the teacher.

Examples
input
Copy
3
2 3 2
output
Copy
2 2 3 
input
Copy
3
1 2 3
output
Copy
1 2 3 
Note

The picture corresponds to the first example test case.

When a=1�=1, the teacher comes to students 11223322, in this order, and the student 22 is the one who receives a second hole in his badge.

When a=2�=2, the teacher comes to students 223322, and the student 22 gets a second hole in his badge. When a=3�=3, the teacher will visit students 332233 with student 33 getting a second hole in his badge.

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.

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int const N = 1e3 + 1;
int n, p[N];
bool vis[N];

int main() {
  scanf("%d", &n);
  for(int i = 0; i < n; ++i)
    scanf("%d", p + i), --p[i];

  for(int i = 0; i < n; ++i) {
    memset(vis, false, sizeof vis);

    vis[i] = true;
    int cur = p[i];
    while(!vis[cur]) {
      vis[cur] = true;
      cur = p[cur];
    }

    printf("%d ", cur + 1);
  }
  puts("");

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
2 3 2

Output

x
+
cmd
2 2 3
Advertisements

Demonstration


Codeforces Solution-B. Badge-Solution in C, C++, Java, Python,Badge-Solution,Codeforces Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+