题干:

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

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

题目大意:

在图灵学校,如果一个学生表现不好,老师就会在他的徽章上打一个洞。今天,老师逮到了n名学生在搞恶作剧。

这些学生从1到n编号。老师先逮到了a学生然后在他的徽章上打了个洞。但是这个学生说带头的是另一个学生pa

于是老师又抓住学生pa在他的徽章上也打了个洞。这个学生又说其实是学生ppa在带头搞恶作剧。

这个过程一直持续了好一会儿,不过因为这些学生是有限的,最后老师抓住了一个徽章上已经有一个洞的学生。

在给这个倒霉孩子的徽章上又打了个洞以后,老师觉得有点累,需要蒸个桑拿于是他就不再继续了。

你不知道谁是老师逮到的第一个学生,但是你知道所有的数字pi。对于每一个a,如果第一个被逮到的学生是a,你的任务是找到谁会是徽章上面有两个洞的学生。

解题报告:

用数组去存这条有向边就行了(因为边数就一条),所以不需要去建图。(当然这题还有一种O(n)的做法)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n,a[MAX];
bool vis[MAX];
int main()
{cin>>n;  for(int i = 1; i<=n; i++) cin>>a[i];for(int i = 1; i<=n; i++) {int cur = i;memset(vis,0,sizeof vis);while(1) {if(vis[cur]) break;vis[cur] = 1;cur = a[cur];}printf("%d ",cur);}return 0 ;
}

这题还有一个O(n)的解法:

因为不难发现,如果一个节点在一个环上,那么答案就是这个节点。反之,那就是在距离这个节点最近的环首上。对于环的问题我们可以拓扑排序一下,然后用并查集去寻找最近的一个环。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;int a[MAX], in[MAX], f[MAX];int getf(int i) {return (i==f[i] ? i : f[i] = getf(f[i]));
}int main()
{int n;cin>>n;for(int i = 1; i<=n; i++) {cin>>a[i];in[a[i]]++;}queue<int> q;for(int i = 1; i<=n; i++)f[i] = i;for(int i = 1; i<=n; i++)if(in[i]==0)q.push(i);//做拓扑排序,使得剩下的顶点都在环中while(!q.empty()) {int cur = q.front();q.pop();in[a[cur]]--;f[cur] = a[cur];if(in[a[cur]]==0) q.push(a[cur]);}for(int i = 1; i<=n; i++) printf("%d ",getf(i));return 0;
}

【CodeForces - 1020B】Badge(模拟,图,环)相关推荐

  1. 简单模拟图卷积(numpy实现)

    简介 GCN 是一类非常强大的用于图数据的神经网络架构.事实上,即使是随机初始化的两层 GCN 也可以生成图网络中节点的有用特征表征.给定图 G = (V, E),V代表节点,E代表节点相连的边,GC ...

  2. 用matlab对基带信号进行调频,基于SIMULINK的模拟锁相环调频调制器研究与设计

    设计应用飞基于SIMULINK的模拟锁相环调频调制器研究与设计 广州大学物理与电子工程学院黄俊奇 [要]利用賴相坏能够对偿号进行自动跟踪和捕提>代通信屎统广'泛是将锁相坯应用总调频技.杷0生柞盔 ...

  3. 2008年最精彩科技图片:电流运动模拟图居首

    2008年最精彩科技图片:电流运动模拟图居首 北京时间1月5日消息,据英国<新科学家>杂志报道,在刚刚过去的2008年,科学家们拍摄或者绘制了许多精彩绚丽的科技图片,这其中包括模拟电流运动 ...

  4. 用matlab绘制克莱因瓶,克莱因瓶怎么装水动态四维模拟图 克莱因瓶为什么装不满怎么倒水...

    很多人和小编一样提到数理化就不自觉地发憷,但其实几何学中有很多奇特的现象非常耐人寻味,就连觉得自己是物理小白的人都会产生探究的想法,而今天要说到的克莱因瓶,就因为其独特的造型让人十分好奇,究竟它存在的 ...

  5. 【免费教程】地下水模拟及环评之水文地质基础与建模流程、数据要求专题

    地下水 地下水(ground water),是指赋存于地面以下岩石空隙中的水,狭义上是指地下水面以下饱和含水层中的水.在国家标准<水文地质术语>(GB/T 14157-93)中,地下水是指 ...

  6. CodeForces - 1491E Fib-tree(模拟)

    题目链接:点击查看 题目大意:给出一棵树,问是否为斐波那契树.斐波那契树的定义是,树的大小为斐波那契数列的其中一项,且可以通过删除掉一条边使其拆分的两个子树也为斐波那契树 题目分析:需要观察到,大小为 ...

  7. React AntV/G2Plot环形图Pie添加点击事件,即点击图环触发获取相关数据。

    步骤: 1.添加相关依赖,引入AntV/G2Plot图表组件 2.添加配置项 3.添加点击事件方法(关键部分:在onReady={onReadyPie},onReady是图表渲染完成执行回调方法,在该 ...

  8. CodeForces - 1422D(建图+最短路)

    CodeForces - 1422D 题意: 二维平面给一个起点和一个终点,给m个传送门,开始位于起点,每次可以走上下左右四个方向,花费为1,当位于和给的传送门有相同的x或者y,可以直接传送到传送门, ...

  9. 用 IQ分布模拟图来测试浏览器的性能

    今天天气太凉快,跟这个日历上属于夏天的那一页显得格格不入!就连我我床下那台废弃的ThinkPad,居然也十分透凉气,那外壳连我的体温高都没有,于是,我就开始想一个方法,让我那个废弃的电脑发热,顺便用它 ...

最新文章

  1. 剑指Offer——平衡二叉树
  2. C/C++如何传递二维数组?
  3. 知识图谱学习笔记-风控知识图谱设计
  4. “面试不败计划“:hibernate和mybatis比较
  5. HISTORY OF ETHEREUM SECURITY VULNERABILITIES, HACKS AND THEIR FIXES
  6. sudo: apt-get:找不到命令_Linux重复执行历史命令方法详解
  7. 【Linux】一步一步学Linux——adduser命令(83)
  8. python后端数据发送到前端_Python Django 前后端数据交互 之 后端向前端发送数据...
  9. linux设备驱动模块引用和依赖
  10. Android 源码获取-----在Windows环境下通过Git得到Android源代码
  11. webConfig中System.Web 和 System.WebServer节点读取
  12. Windows 命令行改主机名、加域、退域、更改IP
  13. 苹果软件App上架问题
  14. 关于Kubernetes规划的灵魂N问
  15. 2023最新SSM计算机毕业设计选题大全(附源码+LW)之java文创产品推荐系统设计与实现95ml5
  16. vue中使用antv/g6 绘制关系图、结构图
  17. pytest告警信息的处理方法
  18. 电脑硬盘锁怎么解除linux,硬盘锁了怎么办_硬盘锁怎么解除
  19. 通过exif.js简单处理手机照片拍摄图片旋转问题
  20. Juniper SRX Junos升级

热门文章

  1. .netframework3.5 中TimeZoneInfo 类的使用
  2. [Java]中[this][super]用法总结
  3. 南师大632c语言程序设计,单片机c语言学习心得632.docx
  4. axios某一接口失败后不调用_axios 源码系列之如何取消请求
  5. Linux 4.15 rc7,Linux学习之十五(sed命令)-2017-4-23
  6. flock lock ex php,php – flock有可能用LOCK_EX返回false吗?
  7. css中设置图片旋转45度,css 实现缓和变量,鼠标悬停时元素旋转45度动画
  8. python 前端学习_python学习之路7 前端学习3
  9. sigprocmask 阻塞进程
  10. 测试鼠标双击_鼠标环境可靠性测试是什么