I tried following the editorial to come up with a solution to the Tree House problem in the May Long Challenge 2021. Can anyone help me figure out how I can further refine my code to pass the last two cases. The problem and solution I came up with is given below:
Problem:
Solution:
https://www.codechef.com/viewsolution/46672786
ACfied
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef unsigned long long ull;
ull solve(ull v, ull parent, vector<vector<ull>>& tree)
{
vector<ull> vals;
for (ull u: tree[v])
if (u != parent)
vals.push_back(solve(u, v, tree));
sort(vals.rbegin(), vals.rend());
ull ret = 1;
ull mult = 1;
for (ull u: vals)
ret += (mult++)*u;
return ret;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ull t; cin>>t;
while (t--)
{
ull n, x; cin>>n>>x;
vector<vector<ull>> tree(n);
for (ull i = 0; i < n - 1; ++i)
{
ull u, v;
cin>>u>>v;
tree[u - 1].push_back(v - 1);
tree[v - 1].push_back(u - 1);
}
cout<<(solve(0, -1, tree) % MOD * x % MOD)%MOD<<"\n";
}
return 0;
}
Can you find the mistake yourself? 
2 Likes
Oh so basically, the final product could overflow. Thanks a lot, man.
how do u put the code like?