【CodeForces - 1020B】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

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;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440754.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Matlab样条拟合曲面,样条曲面拟合及其Matlab实现

1符号和定义先讨论一元情形,给定区间[a,b]的一个分划,ax00,x(xi,xik)0,x[xi,xik],i-k1,…,n-1(iii)若xjixijh,则Ni,k(x)k(x-xih-k12)其中k(x)k1j0(-1)jCjk1(xk12-j)k/k!为以xjj-k12(j0,1,…,k1)为结点的k次等距B样条.Ni,k(x)的其他性质参见[1,2,3,4]满足(3)的k次样条插值曲线s(…

【Hihocoder - 1723】子树统计(线性基合并)

题干&#xff1a; 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵N个节点的有根树&#xff0c;树上每个节点有一个非负整数权重Wi。定义节点集合S{i1, i2, ……, ir}的总权重为&#xff1a;(⊕是异或运算) 每次询问一棵子树内所有可能出现的总权重数量&a…

php postgresql多条,PHPPostgreSQL函数列表 - phpStudy

echo $r["fun"]. .$r["title"]; ?>PostgreSQL函数列表pg_close - 关闭一个 PostgreSQL 连接pg_connect - 打开一个 PostgreSQL 连接pg_convert - 将关联的数组值转换为适合 SQL 语句的格式。pg_copy_from - 根据数组将记录插入表中pg_copy_to - 将一个…

【CodeForces - 485D】Maximum Value (枚举,用数组离散化,数学,取模运算,因子,筛法)

题干&#xff1a; You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj. Input The first line contains integer n — the length of the seq…

【POJ - 2485 】Highways (最小生成树,Prim算法,瓶颈生成树)

题干&#xff1a; The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. Theyre planning to build some highways so that it…

js获取php单选按钮值,js如何获取radio单选框选中的值

js中一般使用遍历的方法获取radio被选中的值&#xff0c;遍历判断每个Radio是否被选中,如果是,再取其值.123function usubmit(action){var radionum document.getElementById("userlist").userid;for(var i0;iif(radionum[i].checked){userid radionum[i].value}}w…

【EOJ Monthly 2019.02 - F】方差(数学,前缀和,积的前缀和)

题干&#xff1a; 单测试点时限: 2.0 秒 内存限制: 256 MB “放弃不难&#xff0c;但坚持一定很酷。” QQ 小方已经在体育馆苦练一天射箭了&#xff0c;但他还在坚持。 QQ 小方每天都要在朋友圈晒自己的训练记录。他一共进行了 n 次射箭&#xff0c;成绩分别是 x1,x2,⋯,x…

oracle gtx,ORACLE

Category Archives: ORACLE所有Oracle技术文章看到群里有人提出一个需求&#xff0c;一张表数据量很大&#xff0c;只想导出其中一部分列。 无论是老版本exp还是数据泵expdp&#xff0c;Oracle都提供了QUERY的功能&#xff0c;这使得查询表中部分记录的功能可以实现&#xff0c…

【牛客 - 371牛客OI周赛7-提高组A】小睿睿的等式(dp,暴力 )

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/371/A 来源&#xff1a;牛客网 小睿睿在游戏开始时有n根火柴棒&#xff0c;他想知道能摆成形如“ABn”的等式且使用的火柴棒数也恰好等于n/k的等式有多少种(BAn与ABn看作一种) 注&#xff1a; “”与…

oracle用户新增数据文件,[数据库]20200722_Oracle添加表空间、用户,用户授权

[数据库]20200722_Oracle添加表空间、用户&#xff0c;用户授权0 2020-07-25 17:00:30--创建表空间CREATE TABLESPACE aifu --表空间名 aifu LOGGING DATAFILE D:\dev_config\OracleTableSpaces\aifu.DBF SIZE 5M --数据文件路径 D:\dev_config\OracleTableSpaces AUTOEXTEND O…

【牛客 - 371牛客OI周赛7-提高组B】小睿睿的询问(RMQ,ST表维护下标)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/371/B 来源&#xff1a;牛客网 小睿睿的n个妹纸排成一排&#xff0c;每个妹纸有一个颜值val[i]。有m个询问&#xff0c;对于每一个询问&#xff0c;小睿睿想知道区间[L,R]颜值最高而编号最小的妹纸是…

matlab语言 列车平稳性指标,铁道车辆平稳性分析报告.docx

....铁道车辆平稳性分析车辆平稳性评价指标1.1 sperling平稳性指标欧洲铁路联盟以及前社会主义国家铁路合作组织均采用平稳性指数来评定车辆的运行品质。等人在大量单一频率振动的实验基础上提出影响车辆平稳性的两个重要因素。其中一个重要因素是位移对时间的三次导数,亦即(加…

【牛客OI周赛7-普及组ABCD 非官方题解】暴力,二分,KMP,尺取(STL或Hash)

A&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/372/A 来源&#xff1a;牛客网 某天&#xff0c;一只可爱的肥橘喵在路上走&#xff0c;突然遇到了一个怪人&#xff0c;那怪人自称PM6&#xff0c;“小肥喵&#xff0c;这里有一道水题&#xff0c;答对…

php函数汉语,PHP汉字截取函数:UTF-8、GB2312双支持

经测试代码如下&#xff1a;/*Utf-8、gb2312都支持的汉字截取函数cut_str(字符串,截取长度,开始长度,编码);编码默认为 utf-8开始长度默认为 0*/function cut_str($string,$sublen,$start 0,$code UTF-8){if($code UTF-8){$pa "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\x…

【牛客 - 327牛客寒假算法基础集训营2 I】处女座的测验(二)(积性函数性质,数论,素数唯一性分解,STL)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/327/I 来源&#xff1a;牛客网 现在处女座顺利的完成了测验&#xff0c;处女座想要知道知道自己输出的结果是否正确。他希望知道自己有自己输出的数中有多少对是不满足要求的。 更具体的&#xff0c…

linux监控命令执行,你可能不知道的 即时监控 Linux 使用者执行指令的三种方法...

原标题&#xff1a;你可能不知道的 即时监控 Linux 使用者执行指令的三种方法这里介绍如何在 Linux 系统上以管理者权限即时监控一般使用者所执行的任何指令。Linux 的 root 管理者可对系统进行任何的管理与操作&#xff0c;如果想要即时监控特定使用者在主机上所执行的指令&am…

【POJ - 2728】Desert King (最有比率生成树,分数规划)

题干&#xff1a; David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be…

linux非权限安装bioperl,bioperl的安装

1. 对于UNIX用户(1)下载BioPerl的源代码&#xff0c;并解压。我提供两个网址&#xff1a;(2)进入该目录&#xff0c;然后执行下列命令&#xff1a;$ perl Build.PL$ ./Build test# ./Build install (注意&#xff1a;执行./Build install时必须要有系统管理员权限)BioPerl和许多…

【牛客 - 327G】处女座与复读机(可编辑距离问题,dp)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/327/G 来源&#xff1a;牛客网 一天&#xff0c;处女座在牛客算法群里发了一句“我好强啊”&#xff0c;引起无数的复读&#xff0c;可是处女座发现复读之后变成了“处女座好强啊”。处女座经过调查…

linux远程打开windows程序,为新手讲解Linux和Windows系统的远程桌面访问知识

很多新手都是使用Linux和Windows双系统的&#xff0c;它们之间的远程桌面访问是如何连接的&#xff0c;我们就为新手讲解Linux和Windows系统的远程桌面访问知识&#xff0c;包括所使用的软件及方法。本文所使用的Linux版本是深度操作系统&#xff0c;如果要安装该版本请参考U盘…