Graph Centroid is a vertex at equal distance or at distance less than or equal (N/2) where N is the size of the connected components connected through this vertex?! [Needs Correction?!]
Here's a problem at CodeForces that asks to find whether each vertex is a centroid, but after removing and replacing exactly one edge at a time.
I need help to refine this PseudoCode / Algorithm.
Loop all Vertices: Loop all edges: Position each edge in every empty edge position between two unconnected nodes Count Size of each Connected Component (*1). If all sizes are less than or equal N/2, Then return true
The problem is that this algorithm will run in at least O(N*M^2)). It's not acceptable.
I looked up the answers, but I couldn't come up with the high level abstraction of the algorithm used by others. Could you please help me understand how these solutions work?
(*1) DFS Loop