之前写过DFS的题,但是或多或少有参考别人的思路,最近开始专攻DFS,下面这道题就是DFS练习第一题CF 1020B,之后训练完后抽时间写一篇DFS专项。
 
B. Badge
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

AC代码:

 1 #include<bits/stdc++.h>
2 using namespace std;
3 void dfs(int idx,int pe[],int count[]);
4 int n,flag = 1;
5 int main(){
6 cin>>n;
7 int pe[n],count[n];
8 memset(count,0,sizeof(count));
9 for(int i = 0;i<n;i++)
10 cin>>pe[i];
11 for(int i = 0;i<n;i++){
12 dfs(i,pe,count);
13 flag = 1;
14 memset(count,0,sizeof(count));
15 }
16 return 0;
17 }
18 void dfs(int idx,int pe[],int count[]){
19 count[idx] += 1;
20 if(count[idx]==2){
21 printf("%d ",idx+1);
22 flag = 0;
23 }
24 if(idx>=n)
25 return ;
26 for(;flag;)
27 dfs(pe[idx]-1,pe,count);
28 return;
29 }

这题思路很简单,就是1~n名同学一个个全局访问,对每次全局访问都进行二次搜索访问直到某名同学访问到了两次,才进行下一次全局访问。

DFS思路比较简单就是实现比较困难,这题实现问题就在三点

  1.dfs变量

  2.dfs扩展方式

  3.如何结束搜索

当然这三点可以推广到所有DFS之中。

代码详解:

1.变量解释

   n ->  全部人数

   flag -> 结束标记

   count数组  -> 每个人被访问到的次数

   pe数组 -> 每个人指向的下一个搜索访问

mian函数

int main(){
cin>>n;
int pe[n],count[n];
memset(count,0,sizeof(count)); //初始化count数组为0
for(int i = 0;i<n;i++)
cin>>pe[i];
for(int i = 0;i<n;i++){
dfs(i,pe,count); // 全局搜索
flag = 1; //第i次搜索完成重新标记flag使下次搜索顺利进行
memset(count,0,sizeof(count)); //再次重新初始化
}
return 0;
}

DFS:

void dfs(int idx,int pe[],int count[]){  //DFS标准void操作   搜索变量为下标idx,传递两个数组的值
count[idx] += 1;            //将搜索到的人在计数数组中加1
if(count[idx]==2){           //结束判断
printf("%d ",idx+1);       //输出
flag = 0;              //标记为已输出
}
if(idx>=n)    //防超出判断
return ;
for(;flag;)       //仅依靠flag进行判断是否进行下次搜索
dfs(pe[idx]-1,pe,count);    //  实际上的搜索扩展
return;
}

完全自己敲出DFS思路,如果有问题还请大家指正~~~

CF 1020B Badge的更多相关文章

  1. Codeforces - 1020B Badge(找环)

    题意: 以每个点为起点,找到第一个出现两次的点 解析: 我是先找出来所有的环  环上的点找出来的肯定是自己 bz[i]  = i; 然后去遍历不在环上的点j  如果通过这个点找到一个已经标记的的点i ...

  2. Codeforces Round #503 (by SIS, Div. 2)B 1020B Badge (拓扑)

    题目大意:每个同学可以指定一个人,然后构成一个有向图.1-n次查询,从某个人开始并放入一个东西,然后循环,直到碰到一个人已经放过了,就输出. 思路:直接模拟就可以了,O(n^2) 但是O(n)也可以实 ...

  3. 水题----B - Badge CodeForces - 1020B

    In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. A ...

  4. 【完全开源】知乎日报UWP版:增加Live磁贴、Badge、以及Toast通知

    目录 说明 实现方法 APP生命期 后台任务 说明 之前网上有人建议增加磁贴(tile).徽章(badge)功能.利用周末的时间,将这两个功能添加上去了.如果将磁贴固定到开始屏幕,磁贴就会循环播放首页 ...

  5. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  6. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  7. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  8. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  9. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  10. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

随机推荐

  1. DOS下的一些命令

    DOS一些命令 1.查看无线密码    netsh wlan show profiles   #显示所有本地电脑加入过的wlan    netsh wlan show profiles name=&q ...

  2. win/ubuntu/centos 安装后台监控工具btop

    之前linux平台进行后台监控一直是简单的看top,但界面太难看而且需要记的缩写太多而且不直观(对于我来说),后面有尝试替换htop,扩展支持了鼠标操作以及直观监控,但是界面还是难看,今天查找了一下发 ...

  3. C# List GroupBy and Sum

    List<PartRequest> partRequests = new List<PartRequest>(); partRequests.Add(new PartReque ...

  4. VUE3声明插件TypeScript类型

    declare module '*.vue' { import type { DefineComponent } from 'vue'; // eslint-disable-next-line @ty ...

  5. cv::eigen

    void TestEigen() { cv::Mat m = (cv::Mat_<float>(3, 3) << 1, 2, 3, 2, 5, 6, 3, 6, 7); cv: ...

  6. C语言学习--指针函数与函数指针

    #include<stdio.h> #include<string.h> //指针函数: 是一个函数, 但是这个函数的返回值类型是一个指针 //函数指针: 是一个指针, 这个指 ...

  7. C/C++/中宏特殊字符的含义及用法总结(“#”、“##”、"#@"、“\”等等)

    在C/C++中,宏定义是由define完成的,宏定义中有几种常见的特殊字符需要我们了解,常用的特殊字符有以下几种: #:在宏展开的时候会将#后面的参数替换成字符串: 字符串化##:将前后两个的单词拼接 ...

  8. 关于uniapp 打自定义基座部分手机不能自动安装的问题

    部分手机指的是-------没错就辣鸡 vivo手机 解决办法----进入手机文件管理-----根目录-----会有一个你打包的app.apk文件,手动安装一下就好了 搜索 复制

  9. 计数 dp 部分例题(一~五部分)

    一.状态设计和简化(状態をまとめる) 例题1:Unhappy Hacking 题意 有一个空串,可以进行下面三种操作: 在末尾加入一个 \(0\). 在末尾加入一个 \(1\). 删去末尾的数,如果串 ...

  10. 每一行输出5个字符之后换行 js

    方法1  使用正则 (如果是汉字,此正则,每五个字换行,如果是英文字符,每十个英文,换行) let arrX ="这是个测试字符串,这是个测试字符串" arrX = arrX.re ...