Problem p02728 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

using LL = long long;
const LL M = 1e9 + 7;

LL combination(LL n, LL r) {}

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N);
  for (auto i = 1; i < N; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }

  vector<LL> fact{1, 1}, inv{1, 1}, finv{1, 1};
  for (LL i = fact.size(); i <= N; ++i) {
    fact.emplace_back(fact.back() * i % M);
    inv.emplace_back(inv.at(M % i) * (M - M / i) % M);
    finv.emplace_back(finv.back() * inv.back() % M);
  }
  auto combination = [&](auto n, auto r) {
    return (fact[n] * finv[n - r] % M) * finv[r] % M;
  };

  vector<LL> res(N);
  unordered_map<int, unordered_map<int, LL>> weight, size;
  function<void(int, int)> dfs = [&](auto pre, auto cur) {
    weight[pre][cur] = 1;
    for (auto nex : G[cur])
      if (nex != pre) {
        dfs(cur, nex);
        (weight[pre][cur] *= weight[cur][nex]) %= M;
        (weight[pre][cur] *=
         combination(size[pre][cur] + size[cur][nex], size[cur][nex])) %= M;
        size[pre][cur] += size[cur][nex];
      }
    ++size[pre][cur];
  };
  dfs(-1, 0);

  function<void(int, int)> dp = [&](auto pre, auto cur) {
    vector<int> V;
    for (auto nex : G[cur])
      if (nex != pre)
        V.emplace_back(nex);
    vector<pair<LL, LL>> W;
    W.emplace_back(weight[cur][pre], size[cur][pre]);
    for (auto nex : V) {
      auto w = W.back().first;
      auto s = W.back().second;
      (w *= weight[cur][nex]) %= M;
      (w *= combination(s + size[cur][nex], size[cur][nex])) %= M;
      s += size[cur][nex];
      W.emplace_back(w, s);
    }
    res[cur] = W.back().first;
    LL rw = 1;
    LL rs = 0;
    for (int i = V.size() - 1; 0 <= i; --i) {
      auto nex = V[i];
      auto w = W[i].first;
      auto s = W[i].second;
      (weight[nex][cur] = w * rw) %= M;
      (weight[nex][cur] *= combination(s + rs, rs)) %= M;
      size[nex][cur] = N - size[cur][nex];
      (rw *= weight[cur][nex]) %= M;
      (rw *= combination(rs + size[cur][nex], size[cur][nex])) %= M;
      rs += size[cur][nex];
      // cout<<nex+1<<"to"<<cur+1<<":"<<weight[nex][cur]<<":"<<size[nex][cur]<<endl;
    }
    for (auto nex : V)
      dp(cur, nex);
  };
  weight[0][-1] = 1;
  size[0][-1] = 0;
  dp(-1, 0);

  for (auto i = 0; i < N; ++i)
    cout << res[i] << endl;

  //   for(auto i=-1; i<N; ++i) {
  //     cout<<i+1<<":";
  //     for(auto p: weight[i]) cout<<"
  //     "<<p.first+1<<":"<<p.second<<":"<<size[i][p.first]; cout<<endl;
  //   }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using LL = long long;
const LL M = 1e9 + 7;

LL combination(LL n, LL r) {
  if (n < r)
    return 0;
  if (n < 0 || r < 0)
    return 0;
  static std::vector<LL> fact{1, 1}, inv{1, 1}, finv{1, 1};
  for (LL i = fact.size(); i <= n; ++i) {
    fact.emplace_back(fact.back() * i % M);
    inv.emplace_back(inv.at(M % i) * (M - M / i) % M);
    finv.emplace_back(finv.back() * inv.back() % M);
  }
  return (fact[n] * finv[n - r] % M) * finv[r] % M;
}

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N);
  for (auto i = 1; i < N; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }

  vector<unordered_map<int, LL>> weight(N), size(N);
  function<LL(int, int)> dfs = [&](auto cur, auto pre) {
    if (weight[cur][pre])
      return weight[cur][pre];
    weight[cur][pre] = 1;
    for (auto nex : G[cur])
      if (nex != pre) {
        dfs(nex, cur);
        (weight[cur][pre] *= weight[nex][cur]) %= M;
        (weight[cur][pre] *=
         combination(size[cur][pre] + size[nex][cur], size[nex][cur])) %= M;
        size[cur][pre] += size[nex][cur];
      }
    ++size[cur][pre];
    return weight[cur][pre];
  };

  for (auto i = 0; i < N; ++i)
    cout << dfs(i, -1) << endl;
}