my code :http://codeforces.com/contest/161/submission/31368822

my approach : i am taking 1 as the root and then calculate the nodes at every distance k (1 <= k <= 500) in it’s subtree and then for each node i am calculating the number of nodes at every distance k without considering it’s subtree. Finally i sum up all the values within the subtree and outside the subtree for the given k and as all the possible pairs are counted twice , i just make my answer half.

it is failing in test 21 please help me where i am going wrong !