Problem Statement: http://codeforces.com/problemset/problem/219/D Approach : Simple DFS. For Node 4 : No. of downward arrows in path from 1 + No. of upward arrows in rest of the tree except the path 0 + 2 2 This is same for Node 2,3 as well 🙂 Solution :