之前写过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 (1n10001≤n≤1000) — the number of the naughty students.

The second line contains nn integers p1p1, ..., pnpn (1pin1≤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思路,如果有问题还请大家指正~~~

 

posted on 2019-12-17 21:20  白子捡一地。  阅读(203)  评论(0编辑  收藏  举报