Error:
cannot solve it. So stupid.
Use dfs to solve it. Actually we can use BFS, which is simpler. But I think dfs is something we use mostly. In dfs, iterate all node, then count current node and next node is the same, the tricky part is the return value.
My solution is:
is_cycle(edges, visited, current, previous):
if(current == previous)
return true;
if(visited[current] == true)
return true;
res = false
for(e : edges[current]):
if(e == previous)
continue;
res |= is_cycle(edges, visited, e, current);
return res;