A11872 | Badge
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $p_a$ .
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}$ .
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_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$ .
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$ .
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}$ .
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_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$ .
输入格式
The first line of the input contains the only integer $n$ ( $1 \le n \le 1000$ ) — the number of the naughty students.
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$ .
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$ .
输出格式
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.
输入输出样例
输入 #1
3 2 3 2
输出 #1
2 2 3
输入 #2
3 1 2 3
输出 #2
1 2 3
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 $3$ 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.
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 $3$ 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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted