Approach for PRIMEDST

For the question CodeChef: Practical coding for everyone , I read the editorial but just want to check if the approach i thought is correct or not:

Taking all vertices as root in each turn and doing BFS traversal calculating the distance to every other node. Since the root will be initialized to zero, if the distance of other node is a prime number, counting it will give us the probable cases.

This may not be a good solution complexity wise, but is the method correct??

Can someone please look into this code: yd1Se3 - Online C++ Compiler & Debugging Tool - Ideone.com I am just considering each vertex as root, doing BFS traversal and checking if the distance is prime or not. However, code should have given TLE, it gives WA. May be some small test case failing… But is the approach correct??

@piyushkumar Can you please look into this!! I’ll be very thankful for the same.