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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<int, int>
#define F first
#define S second
#define mod 1000000007
#define MOD 998244353
#define INF 40000000000000000
int dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dx4[4] = {-1, 0, 1, 0};
int dy4[4] = {0, -1, 0, 1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int extgcd(int a, int b, int &x, int &y) {
  int d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
int mod_inverse(int a, int mm) {
  int x, y;
  extgcd(a, mm, x, y);
  return (mm + x % mm) % mm;
}

using Graph = vector<vector<int>>;
int N;
Graph G;
Graph CC;
Graph dp;
vector<int> fac(200010);
int count0(int now, int p = -1) {
  int s = G[now].size();
  CC[now].assign(s, -1);
  int res = 0;
  rep(i, s) {
    int to = G[now][i];
    if (to == p)
      continue;
    CC[now][i] = count0(to, now);
    res += CC[now][i];
  }
  return res + 1;
}
void count1(int now, int pval = 0, int p = -1) {
  int s = G[now].size();
  rep(i, s) {
    if (G[now][i] == p)
      CC[now][i] = pval;
  }
  int sum = pval + 1;
  rep(i, s) {
    if (G[now][i] == p)
      continue;
    sum += CC[now][i];
  }
  rep(i, s) {
    int to = G[now][i];
    if (to == p)
      continue;
    count1(to, sum - CC[now][i], now);
  }
}

int rec(int now, int p = -1) {
  int s = G[now].size();
  dp[now].assign(s, -1);
  int res = 1;
  int c = 0;
  rep(i, s) {
    int to = G[now][i];
    if (to == p)
      continue;
    dp[now][i] = rec(to, now);
    res = (res * dp[now][i]) % mod;
    res = (res * mod_inverse(fac[CC[now][i]], mod)) % mod;
    c += CC[now][i];
  }
  res = (res * fac[c]) % mod;
  return res;
}
void rerec(int now, int pval = 1, int p = -1) {
  int s = G[now].size();
  rep(i, s) {
    if (G[now][i] == p)
      dp[now][i] = pval;
  }
  int res = 1;
  int c = 0;
  rep(i, s) {
    res = (res * dp[now][i]) % mod;
    res = (res * mod_inverse(fac[CC[now][i]], mod)) % mod;
    c += CC[now][i];
  }
  rep(i, s) {
    int to = G[now][i];
    if (to == p)
      continue;
    rerec(to,
          res * fac[CC[now][i]] % mod * mod_inverse(dp[now][i], mod) % mod *
              fac[c - CC[now][i]] % mod,
          now);
  }
}
int solve(int now) {
  int s = G[now].size();
  int res = 1, c = 0;
  rep(i, s) {
    res = (res * mod_inverse(fac[CC[now][i]], mod)) % mod;
    res = (res * dp[now][i]) % mod;
    c += CC[now][i];
  }
  return res * fac[c] % mod;
}
signed main() {
  cin >> N;
  G.assign(N, vector<int>());
  CC.assign(N, vector<int>());
  dp.assign(N, vector<int>());
  fac[0] = 1;
  rep(i, 200005) fac[i + 1] = (fac[i] * (i + 1)) % mod;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  count0(0);
  count1(0);
  rec(0);
  rerec(0);
  rep(i, N) cout << solve(i) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<int, int>
#define F first
#define S second
#define mod 1000000007
#define MOD 998244353
#define INF 40000000000000000
int dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dx4[4] = {-1, 0, 1, 0};
int dy4[4] = {0, -1, 0, 1};

int fac[200010];
int extgcd(int a, int b, int &x, int &y) {
  int d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
int mod_inverse(int a, int mm) {
  int x, y;
  extgcd(a, mm, x, y);
  return (mm + x % mm) % mm;
}
int culc(vector<int> C, int c) {
  if (C.size() == 0)
    return 1;
  int res = fac[c];
  for (auto x : C) {
    res = res * mod_inverse(fac[x], mod) % mod;
  }
  return res;
}

int N;
vector<int> Graph[200010];
map<pair<int, int>, int> dp0;
map<pair<int, int>, int> dp1;
int count(int now, int next) {
  if (dp0[make_pair(now, next)] != 0)
    return dp0[make_pair(now, next)];
  int res = 1;
  for (auto t : Graph[next]) {
    if (t == now)
      continue;
    res += count(next, t);
  }
  dp0[make_pair(now, next)] = res;
  return res;
}
int solve(int now, int next) {
  if (dp1[make_pair(now, next)] != 0)
    return dp1[make_pair(now, next)];
  int res = 1, c = 0;
  vector<int> C;
  for (auto t : Graph[next]) {
    if (t == now)
      continue;
    c += count(next, t);
    C.push_back(count(next, t));
    res = (res * solve(next, t)) % mod;
  }
  res = (res * culc(C, c)) % mod;
  dp1[make_pair(now, next)] = res;
  return res;
}
int ans(int n) {
  int res = 1, c = 0;
  vector<int> C;
  for (auto t : Graph[n]) {
    c += count(n, t);
    C.push_back(count(n, t));
    res = (res * solve(n, t)) % mod;
  }
  res = (res * culc(C, c)) % mod;
  return res;
}
signed main(void) {
  cin >> N;
  fac[0] = 1;
  rep(i, 200005) fac[i + 1] = (fac[i] * (i + 1)) % mod;

  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    Graph[a].push_back(b);
    Graph[b].push_back(a);
  }
  rep(i, N) cout << ans(i) << endl;
}