bashrc's blog

By bashrc, 11 years ago, In English

I have been stuck on this problem for very long. I believe we need to simply maintain a dag at all times,and report any edge that will cause a cycle. For a undirected graph,the problem can be solved by using disjoint set data structure. Can we modify this solution to directed graphs? Here is my code which is giving me wrong answer ( Because I initially assumed the graph would be undirected,before realizing my mistake after reading the problem statement again)

EDIT : After reading goo.gl_SsAhv and himanshu's reply I have modified the code. However it is giving TLE. What else I can optimize?

EDIT2: Got AC by some minor modifications (Optimizing by some constant factor) in the above code :) . Thanx all.

Full text and comments »

  • Vote: I like it
  • +3
  • Vote: I do not like it