水题----B - Badge 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 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 pa
.
After that, the teacher came to student pa
and made a hole in his badge as well. The student in reply said that the main culprit was student 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 pi
. 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
.
Input
The first line of the input contains the only integer n
(1≤n≤1000
) — the number of the naughty students.
The second line contains n
integers p1, ..., pn (1≤pi≤n), where pi indicates the student who was reported to the teacher by student i
.
Output
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.
Examples
3
2 3 2
2 2 3
3
1 2 3
1 2 3
Note
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.
题解:输入写成数组,然后循环,循环到谁num++,如果num==2输出
ac代码:
#include<iostream>
#include<cstring>
using namespace std;
int a[1100];
int main()
{
    int fl,n,x;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        int b[1100]={0};
        b[i]++;
        x=a[i];
        for(;;)
        {
            b[x]++;
            if(b[x]==2) break;
            x=a[x];
        }
        if(i==1) cout<<x;
        else cout<<' '<<x;
    }
    cout<<endl;
    return 0;
}
水题----B - Badge CodeForces - 1020B的更多相关文章
- 水题 Codeforces Round #302 (Div. 2) A Set of Strings
		
题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...
 - 水题 Codeforces Round #300 A Cutting Banner
		
题目传送门 /* 水题:一开始看错题意,以为是任意切割,DFS来做:结果只是在中间切出一段来 判断是否余下的是 "CODEFORCES" :) */ #include <cs ...
 - Codeforces Testing Round #8 B. Sheldon and Ice Pieces 水题
		
题目链接:http://codeforces.com/problemset/problem/328/B 水题~ #include <cstdio> #include <cstdlib ...
 - Codeforces Round #356 (Div. 2)B. Bear and Finding Criminals(水题)
		
B. Bear and Finding Criminals time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
 - Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
		
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
 - Educational Codeforces Round 7 B. The Time 水题
		
B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...
 - Educational Codeforces Round 7 A. Infinite Sequence 水题
		
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...
 - Codeforces Gym 100531G Grave 水题
		
Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...
 - Codeforces Round #290 (Div. 2)  A. Fox And Snake 水题
		
A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...
 
随机推荐
- Day11-微信小程序实战-交友小程序-附近的人(地图的形式)及位置获取
			
回顾:在下面的tabbar中,我们已经实现了首页 消息 我的,就剩下”附近“页面了 ”附近“的页面主要是用地图来进行展示的(可以显示我的位置,也可以显示周围附近的人的位置) (在地图里面点击它的头像的 ...
 - DOM-BOM-EVENT(7)
			
7.事件深入 7.1.事件捕获 事件流分为事件冒泡和事件捕获两种,事件冒泡指事件从里往外传播,而事件捕获刚好相反,指事件从外向內传播 <!DOCTYPE html> <html la ...
 - 云服务器解析域名去掉Tomcat的8080端口号显示
 - Kubernetes 两步验证 - 使用 Serverless 实现动态准入控制
			
作者:CODING - 王炜 1. 背景 如果对 Kubernetes 集群安全特别关注,那么我们可能想要实现这些需求: 如何实现 Kubernetes 集群的两步验证,除了集群凭据,还需要提供一次性 ...
 - scheduler的调度规则
			
对爬虫的请求进行调度管理 允许接收requests并且会调度一个request去下载,且具有去重机制 优先级和队列不会被调度器执行(调度器不管优先级的问题),用户使用字段给每个Request对象,可以 ...
 - html转义字符大全_网页html特殊符号,特殊字符查看对照表(整理)
			
在HTML中,某些字符是预留的.比如不能使用小于号(<)和大于号(>),这是因为浏览器会误认为它们是标签.如果希望正确地显示预留字符,我们必须在 HTML 源代码中使用字符实体HTML中一 ...
 - HotSpot的类模型(3)
			
上一篇 HotSpot的类模型(2) 介绍了类模型的基础类Klass的重要属性及方法,这一篇介绍一下InstanceKlass及InstanceKlass的子类. 2.InstanceKlass类 每 ...
 - Maven一键部署Springboot到Docker仓库,为自动化做准备
			
1 前言 前面<Springboot整合MongoDB的Docker开发,其它应用也类似>讲解了如何做Docker开发.如何把Springboot应用打包成一个镜像,但它是手动的,本文将讲 ...
 - day05 垃圾回收机制(超小白讲解)
			
垃圾回收机制 在学习这个抽象概念前,老习惯,灵魂二问 什么是?为什么要有? 引言:在程序运行到变量定义时,会在内存空间中存放变量值,然而内存空间是有限的,变量是无限的. Q:如何在有限的内存里存里存放 ...
 - python基础之打/解包及运算符与控制流程
			
python基础之打/解包及运算符与控制流程 python中的解压缩(即序列类型的打包和解包) python提供了两个设计元祖和其他序列类型的处理的便利,也就是自动打包与自动解包功能,比如: data ...