0
点赞
收藏
分享

微信扫一扫

B - Badge CF1020b模拟过程即可

MaxWen 2022-06-29 阅读 69

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
3
2 3 2
Output
2 2 3
Input
3
1 2 3
Output
1 2 3
思路;第一次做没看懂,菜是原罪 = =。从第一个人被老师打洞开始,他会指向别人,第i人会指向ai,这样相互指向别人,最后遇到被打二个洞情况,结束。

#include<stdio.h>
#include<string.h>
int vis[1010];
int c[1010];
int a[1010];
int main()
{
int n;
while(~scanf("%d",&n))
{
int i,p,cnt=0;
for(i=1; i<=n; i++)
scanf("%d",&a[i]);

memset(c,0,sizeof(c));
for(i=1; i<=n; i++)
{
memset(vis,0,sizeof(vis));//这里及时清空数组,用作计数
vis[i]++;
p=a[i];//从第i个开始,模拟指向别人
while(1)
{
vis[p]++;
if(vis[p]==2)
{
c[++cnt]=p;
break;
}
p=a[p];//模拟指向别人
}
}
for(i=1; i<=cnt; i++)
if(i==cnt)
printf("%d\n",c[i]);
else printf("%d ",c[i]);

}
return 0;
}


举报

相关推荐

0 条评论