We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Treat each pair of people given as a point on an undirected graph. For example, if (1,3) is given, then there's an undirected edge between 1 and 3. After constructing a graph for all the given pairs of people, by performing DFS on the graph, and by keeping count of the number of components in the graph and the number of points in each component, we obtain the number of countries (i.e, the number of components) and the number of people in each country (i.e, the number of points in a given component)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Journey to the Moon
You are viewing a single comment's thread. Return to all comments →
Treat each pair of people given as a point on an undirected graph. For example, if (1,3) is given, then there's an undirected edge between 1 and 3. After constructing a graph for all the given pairs of people, by performing DFS on the graph, and by keeping count of the number of components in the graph and the number of points in each component, we obtain the number of countries (i.e, the number of components) and the number of people in each country (i.e, the number of points in a given component)