Topological sort and cycle detection problem

Here’s a link to the problem:

http://codeforces.com/contest/510/problem/C

Here’s my solution:

http://codeforces.com/contest/510/submission/26237075

Where am I going wrong? I tried a lot of different things, but can’t figure it out.

~Thanks

So even though I could not figure out what was wrong with the original code, I figured out a fix.

The fix was to make 3 states instead of two for the vis array, namely not visited, visiting and visited.

(Instead of just 2, visited and not visited)