Help in codeforces problem !!

I have been trying to find the testcases for which my code fails.

Question-> Problem - E - Codeforces

and my Solution-> Submission #39536044 - Codeforces

I have really tried many testcases. Plzz see if you can help in finding the testcases or some fault in my logic!!

Are you using dsu? I don’t understand C++ much.

link text

According to this guy dsu can work but when we are doing union we need a dfs too.

i made edges from source to vertices with indegree 0.
After that i used dfs to find all possible vertices where we can reach from source.

Now if some vertices are still left, it means they are involved in a cycle and so no vertex has indegree 0.

So now i used union find to find if there is an edge from some component to the root of some other component. If such an edge exists then we do not have to include that component.