My solution using dfs


#1

int check=0;int node1=0;
void dfs(int node,vector*v,vector&vis)
{
for(int i=0;i<v[node].size();i++)
{
if(v[node][i]==node1)
{check=1;return;}
else
{
vis[v[node][i]]=1;
dfs(v[node][i],v,vis);
}
}
}
int Solution::solve(int A, vector &B, vector &C) {
check=0,node1=0;
vectorv[A+1];
for(int i=0;i<C.size();i++)
{
v[C[i]].push_back(B[i]);
}

for(int i=1;i<=A;i++)
{ vector<int>vis(A+1,0);
    if(vis[i]==0&&v[i].size())
    {
        vis[i]=1;
        node1=i;
        dfs(i,v,vis);
        if(check)return 0;
    }
}
return 1;

}