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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pa;

#define fst first
#define snd second
#define pb push_back

const int maxn = 1000005, maxm = 55;
const double pi = acos(-1.0);
const ll mod = 1000000007;
const double eps = 1e-6;

ll ksm(ll a, ll p) {
  ll r = 1;
  while (p > 0) {
    if (p & 1)
      r = r * a % mod;
    a = a * a % mod;
    p >>= 1;
  }
  return r;
}

int n;
vector<int> E[maxn];

int fat[maxn], cnt[maxn], dl[maxn], dln = 0, sta;
ll dp[maxn], dh[maxn], dq[maxn];
ll jc[maxn] = {1, 0}, jci[maxn] = {1, 0};

ll ans[maxn];

int main() {
  int i, j, k;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    jc[i] = jc[i - 1] * i % mod;
    jci[i] = ksm(jc[i], mod - 2);
  }
  for (i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    --x;
    --y;
    E[x].pb(y);
    E[y].pb(x);
  }
  dl[dln++] = 0;
  sta = 0;
  memset(fat, -1, sizeof(fat));
  while (sta < dln) {
    int x = dl[sta++];
    for (i = 0; i < E[x].size(); i++) {
      int y = E[x][i];
      if (fat[x] == y)
        continue;
      fat[y] = x;
      dl[dln++] = y;
    }
  }
  for (i = n - 1; i >= 0; i--) {
    int x = dl[i];
    cnt[x] = 1;
    for (j = 0; j < E[x].size(); j++) {
      int y = E[x][j];
      if (y != fat[x])
        cnt[x] += cnt[y];
    }
  }
  memset(dq, -1, sizeof(dq));
  for (i = n - 1; i >= 0; i--) {
    int x = dl[i];
    dp[x] = jc[cnt[x] - 1];
    for (j = 0; j < E[x].size(); j++) {
      int y = E[x][j];
      if (y != fat[x]) {
        dp[x] *= jci[cnt[y]];
        dp[x] %= mod;
        dp[x] *= dp[y];
        dp[x] %= mod;
      }
    }
  }
  dh[0] = 1;
  for (i = 1; i < n; i++) {
    int x = dl[i], y = fat[x];
    dh[x] = jc[n - cnt[x] - 1];
    if (dq[y] < 0) {
      dq[y] = 1;
      for (j = 0; j < E[y].size(); j++) {
        int z = E[y][j];
        if (z == fat[y])
          continue;
        dq[y] *= dp[z];
        dq[y] %= mod;
        dq[y] *= jci[cnt[z]];
        dq[y] %= mod;
      }
    }
    dh[x] *= dq[y] % mod;
    dh[x] %= mod;
    dh[x] *= dh[y];
    dh[x] %= mod;
    dh[x] *= jci[n - cnt[y]];
    dh[x] %= mod;
    dh[x] *= ksm(dp[x], mod - 2);
    dh[x] %= mod;
    dh[x] *= jc[cnt[x]];
    dh[x] %= mod;
  }
  for (i = 0; i < n; i++) {
    ans[i] = dh[i] * dp[i] % mod * jc[n - 1] % mod * jci[cnt[i] - 1] % mod *
             jci[n - cnt[i]] % mod;
    printf("%d\\n", int(ans[i]));
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pa;

#define fst first
#define snd second
#define pb push_back

const int maxn = 1000005, maxm = 55;
const double pi = acos(-1.0);
const ll mod = 1000000007;
const double eps = 1e-6;

ll ksm(ll a, ll p) {
  ll r = 1;
  while (p > 0) {
    if (p & 1)
      r = r * a % mod;
    a = a * a % mod;
    p >>= 1;
  }
  return r;
}

int n;
vector<int> E[maxn];

int fat[maxn], cnt[maxn], dl[maxn], dln = 0, sta;
ll dp[maxn], dh[maxn];
ll jc[maxn] = {1, 0}, jci[maxn] = {1, 0};

ll ans[maxn];

int main() {
  int i, j, k;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    jc[i] = jc[i - 1] * i % mod;
    jci[i] = ksm(jc[i], mod - 2);
  }
  for (i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    --x;
    --y;
    E[x].pb(y);
    E[y].pb(x);
  }
  dl[dln++] = 0;
  sta = 0;
  memset(fat, -1, sizeof(fat));
  while (sta < dln) {
    int x = dl[sta++];
    for (i = 0; i < E[x].size(); i++) {
      int y = E[x][i];
      if (fat[x] == y)
        continue;
      fat[y] = x;
      dl[dln++] = y;
    }
  }
  for (i = n - 1; i >= 0; i--) {
    int x = dl[i];
    cnt[x] = 1;
    for (j = 0; j < E[x].size(); j++) {
      int y = E[x][j];
      if (y != fat[x])
        cnt[x] += cnt[y];
    }
  }
  for (i = n - 1; i >= 0; i--) {
    int x = dl[i];
    dp[x] = jc[cnt[x] - 1];
    for (j = 0; j < E[x].size(); j++) {
      int y = E[x][j];
      if (y != fat[x]) {
        dp[x] *= jci[cnt[y]];
        dp[x] %= mod;
        dp[x] *= dp[y];
        dp[x] %= mod;
      }
    }
  }
  dh[0] = 1;
  for (i = 1; i < n; i++) {
    int x = dl[i], y = fat[x];
    dh[x] = jc[n - cnt[x] - 1];
    for (j = 0; j < E[y].size(); j++) {
      int z = E[y][j];
      if (z == x)
        continue;
      if (z == fat[y]) {
        dh[x] *= dh[y];
        dh[x] %= mod;
        dh[x] *= jci[n - cnt[y]];
        dh[x] %= mod;
      } else {
        dh[x] *= dp[z];
        dh[x] %= mod;
        dh[x] *= jci[cnt[z]];
        dh[x] %= mod;
      }
    }
  }
  for (i = 0; i < n; i++) {
    ans[i] = dh[i] * dp[i] % mod * jc[n - 1] % mod * jci[cnt[i] - 1] % mod *
             jci[n - cnt[i]] % mod;
    printf("%lld\\n", ans[i]);
  }
  return 0;
}