Please Help Me to debug the code

Problem Link : https://atcoder.jp/contests/dp/tasks/dp_p

My Solution Link before dp : https://ide.codingblocks.com/s/345205

My Solution Link After Applying Dp :

My code before applying dp is passing on 11 test cases but after applying dp it is only passing in two test cases please let me know the reason why.

Both the codes are not getting accepted on atcoder please help me so that the codes get accepted on atcoder.

Hi Aman, your calculation is wrong!
calculate answer simultaneously in a DFS call
if cons==0
ans = ans*(f(elem,0,root)+f(elem,1,root))
else
ans = ans*f(elem,0,root)

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.