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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x, y) x = max(x, y)
#define gmin(x, y) x = min(x, y)
#define gadd(x, y) x = add(x, y)
#define gmul(x, y) x = mul(x, y)
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int MOD = 1e9 + 7;

inline int add(int x, int y) {
  int res = x + y;
  while (res >= MOD)
    res -= MOD;
  return res;
}

inline int mul(int x, int y) { return (1LL * x * y) % MOD; }

int modpow(int x, int p) {
  int res = 1;
  while (p) {
    if (p & 1)
      gmul(res, x);
    gmul(x, x);
    p /= 2;
  }
  return res;
}

inline int invert(int x) { return modpow(x, MOD - 2); }

const int N = 2e5 + 5;

int fac[N], inv[N];

void init(int n) {
  fac[0] = inv[0] = 1;
  for (int i = 1; i < n; ++i) {
    fac[i] = mul(fac[i - 1], i);
    inv[i] = invert(fac[i]);
  }
}

int comb(int n, int k) { return mul(fac[n], mul(inv[k], inv[n - k])); }

vector<int> g[N];
int sz[N], par[N], dp[N], ans[N];

int dfs(int u, int p = -1) {
  sz[u] = 1;
  par[u] = p;
  for (int v : g[u]) {
    if (v == p) {
      continue;
    }
    sz[u] += dfs(v, u);
  }
  return sz[u];
}

int n;

void gdp(int u = 0) {
  dp[u] = fac[sz[u] - 1];
  for (int v : g[u]) {
    if (v == par[u])
      continue;
    gdp(v);
    gmul(dp[u], dp[v]);
    gmul(dp[u], inv[sz[v]]);
  }
}

void solve(int u) {
  if (u == 0)
    ans[u] = dp[u];
  else {
    int res = dp[u];
    int top = ans[par[u]];
    gmul(top, invert(dp[u]));
    gmul(top, fac[sz[u]]);
    gmul(top, inv[n - 1]);
    gmul(top, fac[n - 1 - sz[u]]);
    ans[u] = mul(comb(n - 1, sz[u] - 1), mul(res, top));
  }
  for (int v : g[u]) {
    if (v == par[u])
      continue;
    solve(v);
  }
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  init(N);
  cin >> n;
  rep(i, 0, n - 1) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(0);
  gdp(0);
  solve(0);
  rep(i, 0, n) cout << ans[i] << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x, y) x = max(x, y)
#define gmin(x, y) x = min(x, y)
#define gadd(x, y) x = add(x, y)
#define gmul(x, y) x = mul(x, y)
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int MOD = 1e9 + 7;

inline int add(int x, int y) {
  int res = x + y;
  while (res >= MOD)
    res -= MOD;
  return res;
}

inline int mul(int x, int y) { return (1LL * x * y) % MOD; }

int modpow(int x, int p) {
  int res = 1;
  while (p) {
    if (p & 1)
      gmul(res, x);
    gmul(x, x);
    p /= 2;
  }
  return res;
}

inline int invert(int x) { return modpow(x, MOD - 2); }

const int N = 2e5 + 5;

int fac[N], inv[N];

void init(int n) {
  fac[0] = inv[0] = 1;
  for (int i = 1; i < n; ++i) {
    fac[i] = mul(fac[i - 1], i);
    inv[i] = invert(fac[i]);
  }
}

int comb(int n, int k) { return mul(fac[n], mul(inv[k], inv[n - k])); }

vector<int> g[N];
int sz[N], par[N], dp[2 * N];
bool vis[2 * N];
int e[2 * N][2];

int dfs(int u, int p = -1) {
  sz[u] = 1;
  par[u] = p;
  for (int v : g[u]) {
    v = e[v][1];
    if (v == p) {
      continue;
    }
    sz[u] += dfs(v, u);
  }
  return sz[u];
}

int n;

int gdp(int ind) {
  if (!vis[ind]) {
    vis[ind] = 1;
    dp[ind] =
        fac[(e[ind][1] == par[e[ind][0]] ? n - sz[e[ind][0]] : sz[e[ind][1]]) -
            1];
    for (int v : g[e[ind][1]]) {
      int to = e[v][1];
      if (to == e[ind][0])
        continue;
      gmul(dp[ind], gdp(v));
      int amt = (to == par[e[ind][1]] ? n - sz[e[ind][1]] : sz[to]);
      gmul(dp[ind], inv[amt]);
    }
  }
  return dp[ind];
}

int ans(int x) {
  int res = fac[n - 1];
  for (int v : g[x]) {
    int to = e[v][1];
    gmul(res, gdp(v));
    int amt = (to == par[x] ? n - sz[x] : sz[to]);
    gmul(res, inv[amt]);
  }
  return res;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  init(N);
  cin >> n;
  rep(i, 0, n - 1) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    e[2 * i][0] = x;
    e[2 * i][1] = y;
    e[2 * i + 1][0] = y;
    e[2 * i + 1][1] = x;
    g[x].push_back(2 * i);
    g[y].push_back(2 * i + 1);
  }
  dfs(0);
  rep(i, 0, n) cout << ans(i) << '\\n';
}