Need help in graph problem

Problem: https://codeforces.com/problemset/problem/580/C
My Solution: https://codeforces.com/contest/580/submission/83913055
verdict: TLE
I tried all paths from leaves to root and counted how many paths satisfy the given condition.
Need better approach as i did not understood the given editorial.

I had done it with a simple dfs. You can check my dfs function : https://ideone.com/ZsJJBa , i added some comments but you can ask if you had any query further.

1 Like

Thank you so much, i understood your solution and implemented it successfully.