Please read the new rule regarding the restriction on the use of AI tools. ×

GreedyXploit's blog

By GreedyXploit, history, 4 years ago, In English
#include<bits/stdc++.h>
using namespace std;
int ct = 0;int ans = 0;
vector<int> a[5000+1];
bool checked[5000+1] = {false};

void dfs(int u )
{
    if(checked[u])
    {
       // cout<<u<<"\n";
        ans++;
        return;
    }
    //cout<<ans<<"\n";
    checked[u] = true;

    for(int i = 0 ; i<a[u].size() ; i++)
    {
        dfs(a[u][i]);
    }

}
int main()
{
    int n ;
    cin>>n;
    for(int i = 1 ; i<n;i++ )
    {
        int k ; 
        cin>>k;
        a[i].push_back(k);

    }
    
    dfs(1);
    //for(int i = 0 ; i<5 ; i++)
    //cout<<checked[i]<<"\n";   
    if(ans > 0)
    cout<<"YES";
    else
    cout<<"NO";
}

QUESTION LINK

THE TEST CASE IN WHICH I FAIL

test case 3 : 3 3 1 2

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?