So I came across this question on spoj Problem. And I was very keen to solve it using graph theory(just becuase my graph theory is weak). My approach is as followes- Take Bob as vertex 1 and all other friends as their IDs … I do a simple BFS (calculating distances) from BOb… Now every node that is at distance 2 is a friend of friend… I am getting a wrong answer all the time… Here i s my code… Please help… Link to my solution Solution