(SPOILER!)Cycle Detection Algorithm:


#1

bool func(vector v[], bool vis1[], bool vis[], int curr)
{
if(vis[curr] == 1 && vis1[curr] == 0)
return 0;
if(vis[curr] && vis1[curr] == 1)
return 1;
vis[curr] = 1;
vis1[curr] = 1;

bool temp = 0;

for(auto it = v[curr].begin();it!=v[curr].end();it++)
    temp|=func(v, vis1, vis, *it);
vis1[curr] = 0;
return temp;

}

bool dfs(vector v[], int n)
{
bool vis[n+1];
memset(vis,0,sizeof(vis));
bool vis1[n+1];
memset(vis1, 0, sizeof(vis1));

bool temp = 0;

for(int i=1;i<n+1;i++)
{
    if(vis[i])
        continue;
    temp|=func(v, vis1, vis, i);
    if(temp)
        return 1;
}

return 0;

}

int Solution::solve(int A, vector &b, vector &c) {

int m = b.size();
int n = 0;
for(int i=0;i<m;i++)
    n = max({n, b[i], c[i]});
vector<int> v[n+1];
for(int i=0;i<m;i++)
    v[b[i]].push_back(c[i]);
return 1^dfs(v, n);    

}


#2

Otherwise , we could have also found number of strongly connected components using tarjan’s algorithm and if that is not equal to number of nodes in graph, we return TRUE for cycle presence.