WA on codeforces (D. Maximum distributed tree)

i am getting WA on Maximum distributed tree and i can’t find the error, please help!

my approach is to for each edge calculate the how frequently it is traveled and store it in path_freq and then sort path_freq in decreasing order, and also sort the factors array in decreasing order, now just kept multiplying the greatest factor with greatest path_freq and added them all for the ans.

Think about the case when m>n-1. Read the editorial

path_freq.push_back(((subtree[u]%MOD)*((n - subtree[u])%MOD))%MOD)

don’t take mod of subtree size + as @sebastian said for case m > n - 1.


ah i see, found the error thanks, should’ve read the editorial first, thank you

can please someone help me with my solution?

I think problem is overflow. When you are multiplying factors for the case of m>n-1, then multiplying top1 and top2 will cause overflow. So, try with long long

#define int uint64_t

actually I have already defined this. :frowning:

same code without comment :
I have been stuck on tc13 for 2 days…would really appreciate it if someone could help me out.