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

// Link : https://codeforces.com/contest/1253

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200005
#define mod 1000000007
/*
#include <limits.h>
INT_MAX
LLONG_MAX
*/
int add(int x, int y) { return ((x + y) % mod + mod) % mod; }
int mul(int x, int y) { return (ll)x * y % mod; }
int mypow(int x, int c) {
  int ret = 1;
  while (c > 0) {
    if (c & 1) {
      ret = mul(ret, x);
    }
    x = mul(x, x);
    c /= 2;
  }
  return ret;
}

int fac[N], invFac[N];

vector<int> adj[N];
int dp[N], subSize[N], out[N];

void dfs(int node, int par) {
  subSize[node] = 1;
  dp[node] = 1;
  for (int ch : adj[node]) {
    if (ch == par) {
      continue;
    }
    dfs(ch, node);
    subSize[node] += subSize[ch];

    dp[node] = mul(dp[node], mul(dp[ch], invFac[subSize[ch]]));
  }
  dp[node] = mul(dp[node], fac[subSize[node] - 1]);
}

void dfs2(int node, int par, int n, int tmpDp) {
  int r = mul(dp[node], invFac[subSize[node] - 1]);
  r = mul(r, mul(fac[n - 1], mul(invFac[n - subSize[node]], tmpDp)));
  out[node] = r;
  for (int ch : adj[node]) {
    if (ch == par) {
      continue;
    }
    int nextDp = mul(out[node], invFac[n - 1]);
    nextDp = mul(nextDp, fac[n - subSize[ch] - 1]);
    nextDp = mul(nextDp, fac[subSize[ch]]);
    nextDp = mul(nextDp, mypow(dp[ch], mod - 2));
    dfs2(ch, node, n, nextDp);
  }
}

void solve() {
  int n;
  scanf("%d ", &n);
  fac[0] = invFac[0] = 1;
  for (int i = 1; i <= n; ++i) {
    fac[i] = mul(fac[i - 1], i);
    invFac[i] = mypow(fac[i], mod - 2);
  }
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    scanf("%d %d ", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1, 0);
  dfs2(1, 0, n, 1);
  for (int i = 1; i <= n; ++i) {
    printf("%d\\n", out[i]);
  }
}

int main() {
  // freopen("input.txt","r",stdin);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

// Link : https://codeforces.com/contest/1253

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200005
#define mod 1000000007
/*
#include <limits.h>
INT_MAX
LLONG_MAX
*/
int add(int x, int y) { return ((x + y) % mod + mod) % mod; }
int mul(int x, int y) { return (ll)x * y % mod; }
int mypow(int x, int c) {
  int ret = 1;
  while (c > 0) {
    if (c & 1) {
      ret = mul(ret, x);
    }
    x = mul(x, x);
    c /= 2;
  }
  return ret;
}

int fac[N], invFac[N];

vector<int> adj[N];
int dp[N], subSize[N], out[N];

void dfs(int node, int par) {
  subSize[node] = 1;
  dp[node] = 1;
  for (int ch : adj[node]) {
    if (ch == par) {
      continue;
    }
    dfs(ch, node);
    subSize[node] += subSize[ch];

    dp[node] = mul(dp[node], mul(dp[ch], invFac[subSize[ch]]));
  }
  dp[node] = mul(dp[node], fac[subSize[node] - 1]);
}

void dfs2(int node, int par, int n, int tmpDp) {
  int r = mul(dp[node], invFac[subSize[node] - 1]);
  r = mul(r, mul(fac[n - 1], mul(invFac[n - subSize[node]], tmpDp)));
  out[node] = r;
  for (int ch : adj[node]) {
    if (ch == par) {
      continue;
    }
    int nextDp = mul(out[node], invFac[n - 1]);
    nextDp = mul(nextDp, fac[n - subSize[ch] - 1]);
    nextDp = mul(nextDp, fac[subSize[ch]]);
    nextDp = mul(nextDp, mypow(dp[ch], mod - 2));
    dfs2(ch, node, n, nextDp);
  }
}

void solve() {
  int n;
  scanf("%d ", &n);
  fac[0] = invFac[0] = 1;
  for (int i = 1; i <= n; ++i) {
    fac[i] = mul(fac[i - 1], i);
    invFac[i] = mypow(fac[i], mod - 2);
  }
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    scanf("%d %d ", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; ++i) {
    dfs(i, 0);
    printf("%d\\n", dp[i]);
  }
}

int main() {
  // freopen("input.txt","r",stdin);
  solve();
  return 0;
}