1 solutions

  • 1
    @ 2025-8-12 11:25:12
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+10;
    int a[N],st[N];
    int main()
    {
    	int n,x;
    	cin>>n>>x;//x表示最先知道秘密的人
    	for(int i=1;i<=n;i++)
    	{
    		cin>>a[i];
    	}
    	st[x]=1; //第一个人已经知道秘密 
    	int cnt=1; //第一个人
    	//i=a[x]=>表示秘密由x传到了a[x],也就是i 
    	//i=a[i]表示i将秘密传给了下一个人(秘密继续往下传递)
    	//st[i]!=-1 表示编号i还不知道秘密,可以继续传递 
    	for(int i=a[x];st[i]!=1;i=a[i])
    	{
    		cnt++;
    		st[i]=1;	
    	} 
    	cout<<cnt;
    	return 0;
    }
    
    
    • 1

    Information

    ID
    2374
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    3
    Tags
    (None)
    # Submissions
    14
    Accepted
    8
    Uploaded By