As Sheldor the conqueror, you're supposed to conquer the kingdom of Wonder-blunder-berg , which happens to be a collection of cities connected with roads. This is the Adjacency List of the road network of WonderBlunderBerg showing which city has a direct road to which other city.
As you planned before , you are first going to destroy all the bridges in the kingdom. That splits the kingdom into several components .
Among those components, what is the number of cities in the component with the maximum number of cities?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The problem asks us to first destroy all the bridges of the kingdom and then find the largest component of the resulting graph. For this, we start by identifying the bridges of the graph. Have a look at the solution of the problem cut the bridges and conquer to know how we find the bridge edges of a graph. (Explaining the code for finding bridges would make the solution really lengthy @Agnishom Chattopadhyay have explained very nicely in the above linked solution).
To be precise, the bridge edges comes out to be : {169,20} , {5,225} , {15,247} , {120,154} , {245,212} , {1,245} (The pairs here is not ordered i.e {a,b} and {b,a} both needs to be removed for all {a,b} )
Having found the bridge edges, remove the bridge edges from the adjacency list.
Now, we have a graph and the problem asks to find the size of largest component in it. We can find the size of each component by any graph exploration algorithm depth first search or breadth first search . The following explains to use depth first search.
So, how exactly do we do this? Its simple. Whenever we start exploring a node, we end up exploring all the nodes accessible from that node. In other words, we explore all the nodes in the component of our starting node. Just keep a counter "presentCount" and increase it when you encounter a new node. Set the value of "presentCount" to 0 at the start of exploration of each new component. Also keep a variable "ans" which is here to keep track of the largest component size.
This way, we visit all components and pick up the largest component size as our answer.