http://codeforces.net/problemset/problem/543/D
I did not understand the editorial so I wrote a solution of my own. But I don't understand the tricky cases where it fails. Here is my submission ID 11679268 . My logic is simple:
Do DFS with "1" as root , store results in dp[i]
result[1]=dp[1]
result[i]=(1 + result[parent_of_i]/(dp[i]+1)) * dp[i] % mod;
Where is the logic wrong? I couldn't figure it out.
Auto comment: topic has been updated by I_love_Captain_America (previous revision, new revision, compare).
This my code http://codeforces.net/contest/543/submission/11369630 — its Accepted
To divide result[parent_of_i]/(dp[i]+1) it's better to precalc prefix and suffix multiplications ans just calc something like pref[i-1]*suff[i+1]
Yes, I calculate inverse power modulo 1000000007 in logarithmic time. I think I found the problem. If result[parent_of_i]==0, then all the further multiplication and division work goes to waste. I tried to work this out, but still haven't got it.
Still couldn't fix the bug when (result[parent_of_i]=0 and dp[i]=1000000006) . vintage_Vlad_Makeev can you please explain how prefix and suffix are calculated ?
Ok. I think I get it now.