Pages

Thursday, 8 December 2016

HACKER RANK: Journey to the Moon

Problem Linkhttps://www.hackerrank.com/challenges/journey-to-the-moon

Summary:
There are  trained astronauts numbered from  to . But those in charge of the mission did not receive information about the citizenship of each astronaut. The only information they have is that some particular pairs of astronauts belong to the same country.
Your task is to compute in how many ways they can pick a pair of astronauts belonging to different countries. Assume that you are provided enough pairs to let you identify the groups of astronauts even though you might not know their country directly. For instance, if  are astronauts from the same country; it is sufficient to mention that  and  are pairs of astronauts from the same country without providing information about a third pair .
Print An integer that denotes the number of permissible ways to choose a pair of astronauts.
For Example:
Sample Input:
4 2
0 1
2 3


Sample Output:
4

Topic:
Graph Theory, BFS(Breadth First Search)

Logic:
I have create an adjacency list.
Then Using BFS I have count total number of sub graphs in a graph such that there is no edges between sub graphs and also count total number of nodes in each sub graph.

Then compute number of ways 2 astronauts from different country can be sent to Moon.
For example:
Like From America There are 3 trained astronauts.
From India There are 4 trained astronauts.
From Japan There are 2 trained astronauts.
From China, Australia and Russia There are Only One astronaut.
So number_of_ways = 3*(4+2+1+1+1) + 4*(2+1+1+1) + 2*(1+1+1) + 2 + 1 = 56

No comments:

Post a Comment