P2661 Information passing



Topic description

There are n classmates (numbered 1 to n) playing a game of information passing. In the game, everyone has a fixed information transfer object, among which, the information transfer object of the student numbered i is the student numbered Ti.

When the game starts, everyone only knows their birthday. In each subsequent round, everyone will simultaneously tell their current birthday information to their respective recipients of information (note: someone may be able to obtain information from several people, but each person will only tell the information to one person, namely himself information transfer object). The game is over when someone learns their birthday from someone else's mouth. How many rounds are there in the game?

Input and output format

Input format:

Enter a total of 2 lines.

Line 1 contains a positive integer n representing n persons.

Line 2 contains n positive integers T1, T2, ..., Tn separated by spaces, where the ith integer Ti is numbered i

The information transfer object of the classmate is the classmate numbered Ti, Ti≤n and Ti≠i

The data guarantees that the game will definitely end.

Output format:

The output consists of 1 line, including 1 integer, indicating how many rounds the game can be played in total.

Input and output example

Input Example #1: Copy
5
2 4 2 3 1
Output Sample #1: Copy
3

illustrate

Example 1 Explanation

The flow of the game is shown in the figure. After the 3rd round of the game, player 4 will hear player 2 tell him to

own birthday, so the answer is 3. Of course, after the third round of the game, players No. 2 and No. 3 can read their own messages

The source knows his birthday, which also meets the conditions for the end of the game.

For 30% of the data, n ≤ 200;

For 60% of the data, n ≤ 2500;

For 100% of the data, n ≤ 200000.

Originally, I didn't understand why I was the smallest ring in this question, but only after I drew on the book did I understand that the number of nodes in the ring is the number of rounds;

#include<bits/stdc++.h>
using namespace std;
int to[200002]={0};//point to the next point
bool visit[200002]={0},novisit[200002]={0};//visit is used to record each search through this point, novisit is used to record once
int step[200002]={0},n,minn=1000000; //Visit the points that have been checked
void dfs(int i,int num)
{
    if(novisit[i]) return;
    if(visit[i])
    {
        minn=min(minn,num-step[i]);//Subtract the number of steps passed through this point last time from the number of steps passing through this point, which is the size of the smallest ring
    }
    else
    {
        visit[i]=1;
        step[i]=num;
        dfs (to [i], num + 1);
        novisit[i]=1;

    }
}
intmain()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    cin>>to[i];
    for(int i=1;i<=n;i++)
    {
        dfs(i,0);
    }
    cout<<minn<<endl;
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324441443&siteId=291194637