Comments

Wow, thanks a lot. I understood it. Also I was thinking instead of reversing( or getting the last vertex of bfs) we could sort the nodes in each component based on the degree of that node. Will this work?

Upd : Yea Its working, Thank you Igorjan94 :)

From guitarhero_0221 's comment test case, I realized that we can't directly remove any node from the non-complete-component. We should first check if removing a node from this component still preserve the connections between other nodes in the same component.

But, why does reversing the component fix the solution?

Sorry I meant "complete components" instead of Strongly connected.

I just realized step 4 is wrong.

But step 5 is still correct because in the counter-example you have given a tree that is not a complete component. So the answer is 1 from step 2.

I still claim that my approach is correct ignoring step 4.

Why is my solution getting WA for problem E?

My Logic:

  1. Find all connected components. If the number of connected components = 1 then return 0
  2. For each component, I will check if it is strongly connected. If it is not strongly connected then the answer is 1, because we can choose a node in this component then it will still remain connected after 1 operation and also it will get connected to all other components.
  3. Now we are left with all strongly connected components.
  4. If the number of strongly connected components is> 2 then the answer is 2 because after one operation we get to the same situation as the second point.
  5. Else (no. of comps ==2) then anser is minimum size of two components.

Please correct me if my approach is not correct.