Badge CodeForces - 1020B

本文解析了CodeForces1020B题目,通过深度优先搜索算法解决了一个关于学生间的互相指认问题,最终确定每个学生作为起点时谁会被打上第二个洞的标记。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Badge - CodeForces 1020B - Virtual Judgeicon-default.png?t=M0H8https://vjudge.net/problem/CodeForces-1020B

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 p_apa​.

After that, the teacher came to student p_apa​ and made a hole in his badge as well. The student in reply said that the main culprit was student p_{p_a}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 p_ipi​. 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 \le n \le 10001≤n≤1000) — the number of the naughty students.

The second line contains nn integers p_1p1​, ..., p_npn​ (1 \le p_i \le n1≤pi​≤n), where p_ipi​ 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

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 = 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 <iostream>
using  namespace std;
#include<string>
#pragma warning (disable:4996)
int arr[1000000];
void dfs(int a);
int book[100000];
int main() {
	int n;
	cin >> n;
	for (int cnt = 1; cnt <= n; cnt++) {
		cin >> arr[cnt];
	}
	for (int cnt = 1; cnt <= n; cnt++) {
		dfs(cnt);
		cout << (cnt != n ? " " : "");
		fill(book, book + n + 1, 0);
	}
	return 0;
}
void dfs(int a) {
	if (book[a] == 2) {
		cout << a;
		return;
	}
	book[a]++;
	dfs(arr[a]);
}

 深度搜索,除此之外注意下要把book归零,否则在测试点7报错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cjz-lxg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值