• CodeForces


    题目链接

    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 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 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(1≤n≤1000) — the number of the naughty students.

    The second line contains n

    integers p1, ..., pn (1≤pi≤n), where pi indicates the student who was reported to the teacher by student i.

    Output

    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.

    Examples

    Input

    3
    2 3 2
    

    Output

    2 2 3 
    

    Input

    3
    1 2 3
    

    Output

    1 2 3 
    

    Note

    The picture corresponds to the first example test case.

    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.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 3getting 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.

    题意分析:

    老师抓住了一个表现不好的学生,会在他的徽章上打个洞,然后这个学生会说罪魁祸首是其他人,老师会找到这个人,并在他的徽章上打个洞,然后这名学生又会说罪魁祸首是其他人。 这样最后会回到第一个人面前,并把他的徽章打两个洞,现在知道每个说的罪魁祸首是谁,想知道如果第一次抓住的人是 i ,最后会在徽章打两个洞的人会是谁。

    解题思路:

    直接模拟找人的过程即可,当有人被第二次访问时直接输出。

    #include <stdio.h>
    #include <string.h> 
    #define N 1020
    int a[N], book[N];
    int main()
    {
    	int n;
    	while (scanf("%d", &n)!=EOF)
    	{
    		for (int i=1; i<=n; i++)
    			scanf("%d", &a[i]);
    		for(int i=1; i<=n; i++)
    		{
    			memset(book, 0, sizeof(book));
    			int x=i;
    			while(1)
    			{
    				book[x]++;
    				if(book[x]==2)
    				{
    					printf("%d ", x);
    					break;
    				}
    				x=a[x];
    			}	
    		} 
    		printf("
    ");
    	}
    	return 0; 
    }
  • 相关阅读:
    第八周作业
    第七周作业
    第六周作业
    第五周作业
    2019第四周作业
    第二周基础作业
    秋季学期学习总结
    人生路上对你影响最大的三位老师
    IDF实验室-简单编程-特殊的日子 writeup
    IDF实验室-简单的ELF逆向 writeup
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852508.html
Copyright © 2020-2023  润新知