Test cases not getting passed completely

I tried to approach the problem by applying dfs on every element which is not visited and making a stack through which we can check the next element is already traversed or not and one more variable is also introduced which is cycle (which will check the sense of traversal clockwise or anticlockwise).
Ples chk my code


last 2 test cases ,i am not able to pass.Rest are being passed.