CodeForce1020B - Badge(深度优先搜索||模拟)

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 nn students doing yet another trick.

Let's assume that all these students are numbered from 11 to nn. The teacher came to student aa and put a hole in his badge. The student, however, claimed that the main culprit is some other student papa.

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

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 pipi. Your task is to find out for every student aa, who would be the student with two holes in the badge if the first caught student was aa.

Input

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

The second line contains nn integers p1p1, ..., pnpn (1≤pi≤n1≤pi≤n), where pipi indicates the student who was reported to the teacher by student ii.

Output

For every student aa from 11 to nn print which student would receive two holes in the badge, if aa 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 

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

When a=2a=2, the teacher comes to students 22, 33, 22, and the student 22 gets a second hole in his badge. When a=3a=3, the teacher will visit students 33, 22, 33 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.

深搜代码:

#include<bits/stdc++.h>
using namespace std;
struct ok//其实用数组做就行,当时也没改.
{
    int x;
    int index;
}p[1000+66];
int b[1000+66]={0};
void dfs(int xx)//深搜
{
    //cout<<xx<<endl;
    b[xx]++;
    if(b[xx]==2)//深搜结束条件.
    {
        cout<<xx<<" ";
        return;
    }
    dfs(p[xx].x);//深搜下一个
}
int main()
{
    int n,i,j,a,c;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>p[i].x;
        //p[i].index=i;
    }
    for(i=1;i<=n;i++)
    {
        memset(b,0,sizeof(b));//初始化更新
        b[i]=1;
        dfs(p[i].x);
        b[i]=0;//回溯一步
    }
}

其实当时想多了,直接模拟就行。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include<algorithm>
#include <set>
#include <queue>
#include <stack>
#include<vector>
#include<map>
#include<ctime>
#define ll long long
#define INF 0x7fffffff
using namespace std;
int a[1000+100];
int num[1000+100];
int main()
{

    int n;
    //cout<<sizeof(a);
    while(cin>>n)
    {
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;++i)cin>>a[i];
        for(int i=1;i<=n;++i)
        {
            memset(num,0,sizeof(num));
            int pre=i;
            while(1)
            {
                num[pre]++;
                if(num[pre]>1)break;
                pre=a[pre];
            }
            cout<<pre<<" ";
        }
        cout<<endl;

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/81635049
今日推荐