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

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

#define FOR(a, b, c) for (int a = b; a <= c; ++a)
#define FORW(a, b, c) for (int a = b; a >= c; --a)
#define fi first
#define se second
#define pb push_back
#define int long long

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int N = 2e5 + 100;
const int oo = 1e8;
const int mod = 1e9 + 7;

int n;
int f[N], g[N], sz[N], frac[N], fracw[N], pr[N];
vector<int> vi[N], rig[N];

void add(int &x, int y) { x = (x + y) % mod; }
void mul(int &x, int y) { x = x % mod * (y % mod) % mod; }

int pow(int x, int y) {
  if (!y)
    return 1;
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = res % mod * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int ckn(int k, int n) {
  int res = frac[n];
  mul(res, fracw[n - k]);
  mul(res, fracw[k]);
  return res;
}

void dfs2(int u, int val) {
  g[u] = 1;
  if (u != 1) {
    int cur = n - sz[u];
    g[u] = ckn(n - sz[pr[u]], cur - 1) * g[pr[u]] % mod * val % mod;
  }

  int cnt = rig[u].size() - 1, lef = 1, slef = 0;
  for (auto v : vi[u])
    if (v != pr[u]) {
      cnt--;
      dfs2(v, lef * rig[u][cnt] % mod * ckn(slef, sz[u] - sz[v] - 1) % mod);
      mul(lef, f[v] * ckn(sz[v], sz[v] + slef));
      slef += sz[v];
    }
}

void dfs1(int u, int p) {
  sz[u] = 1;
  pr[u] = p;
  for (auto v : vi[u])
    if (v != p) {
      dfs1(v, u);
      sz[u] += sz[v];
    }

  f[u] = 1;
  int srig = 0;
  rig[u].pb(1);
  for (int i = vi[u].size() - 1; i >= 0; --i)
    if (vi[u][i] != p) {
      int v = vi[u][i];
      mul(f[u], f[v] * ckn(sz[v], srig + sz[v]) % mod);
      rig[u].pb(f[u]);
      srig += sz[v];
    }
}

signed main() {
  //    freopen("test.inp", "r", stdin);
  //    freopen("test.out", "w", stdout);
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  frac[0] = 1;
  FOR(i, 1, N - 1) frac[i] = frac[i - 1] * i % mod;
  FOR(i, 0, N - 1) fracw[i] = pow(frac[i], mod - 2);

  cin >> n;
  FOR(i, 1, n - 1) {
    int u, v;
    cin >> u >> v;
    vi[u].pb(v);
    vi[v].pb(u);
  }
  dfs1(1, 1);
  dfs2(1, 1);
  FOR(i, 1, n) {
    int res = g[i] * ckn(n - sz[i], n - 1);
    int cur = sz[i] - 1;
    for (auto v : vi[i])
      if (v != pr[i])
        mul(res, f[v] * ckn(sz[v], cur)), cur -= sz[v];
    cout << res << '\\n';
  }
}


Error: TLE

Fixed submission:

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

#define FOR(a, b, c) for (int a = b; a <= c; ++a)
#define FORW(a, b, c) for (int a = b; a >= c; --a)
#define fi first
#define se second
#define pb push_back
#define int long long

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int N = 2e5 + 100;
const int oo = 1e8;
const int mod = 1e9 + 7;

int n;
int f[N], g[N], sz[N], frac[N], fracw[N], pr[N];
vector<int> vi[N];

void add(int &x, int y) { x = (x + y) % mod; }
void mul(int &x, int y) { x = x % mod * (y % mod) % mod; }

int pow(int x, int y) {
  if (!y)
    return 1;
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = res % mod * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int ckn(int k, int n) {
  int res = frac[n];
  mul(res, fracw[n - k]);
  mul(res, fracw[k]);
  return res;
}

void dfs2(int u) {
  g[u] = 1;
  if (u != 1) {
    int cur = n - sz[u];
    g[u] = ckn(n - sz[pr[u]], cur - 1) * g[pr[u]] % mod;
    cur -= (n - sz[pr[u]] + 1);
    for (auto v : vi[pr[u]])
      if (v != u && v != pr[pr[u]])
        mul(g[u], f[v] * ckn(sz[v], cur) % mod), cur -= sz[v];
  }
  for (auto v : vi[u])
    if (v != pr[u])
      dfs2(v);
}

void dfs1(int u, int p) {
  sz[u] = 1;
  pr[u] = p;
  for (auto v : vi[u])
    if (v != p) {
      dfs1(v, u);
      sz[u] += sz[v];
    }

  f[u] = 1;
  int cur = sz[u] - 1;
  for (auto v : vi[u])
    if (v != p) {
      mul(f[u], f[v] * ckn(sz[v], cur));
      cur -= sz[v];
    }
}

signed main() {
  //    freopen("test.inp", "r", stdin);
  //    freopen("test.out", "w", stdout);
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  frac[0] = 1;
  FOR(i, 1, N - 1) frac[i] = frac[i - 1] * i % mod;
  FOR(i, 0, N - 1) fracw[i] = pow(frac[i], mod - 2);

  cin >> n;
  FOR(i, 1, n - 1) {
    int u, v;
    cin >> u >> v;
    vi[u].pb(v);
    vi[v].pb(u);
  }
  dfs1(1, 1);
  dfs2(1);
  FOR(i, 1, n) {
    int res = g[i] * ckn(n - sz[i], n - 1);
    int cur = sz[i] - 1;
    for (auto v : vi[i])
      if (v != pr[i])
        mul(res, f[v] * ckn(sz[v], cur)), cur -= sz[v];
    cout << res << '\\n';
  }
}