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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
// #include <unordered_map>
#include <cassert>
#include <complex>
#include <cstdlib>
#include <iterator>
#include <numeric>
#include <stack>
using namespace std;
#define maxn 200007
#define maxm 400007
#define maxq 1000007
#define maxl 1234
#define modn 1000000007
#define scan(a) scanf("%d", &a)
#define scanl(a) scanf("%lld", &a)
#define scanl2(a, b) scanf("%lld%lld", &a, &b)
#define scans(a) scanf("%s", a)
#define scans2(a, b) scanf("%s%s", a, b)
#define scand(a) scanf("%lf", &a)
#define scan2(a, b) scanf("%d%d", &a, &b)
#define scand2(a, b) scanf("%lf%lf", &a, &b)
#define scan3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scan4(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define scan5(a, b, c, d, e) scanf("%d%d%d%d%d", &a, &b, &c, &d, &e)
#define print(a) printf("%d\\n", a);
#define printl(a) printf("%lld\\n", a);
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fscan(a) fscanf(stream, "%d", &a);
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define mm(a, b) memset(a, b, sizeof a)
#define log2(n) (double)(log(n) / log(2.0))
#define _crt_disable_perfcrit_locks
#define bll __int128
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef map<ll, ll> mll;
typedef pair<ll, ll> pll;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef map<char, int> mci;
typedef map<char, char> mcc;
typedef pair<char, int> pci;
typedef complex<double> cd;
typedef vector<complex<double>> vcd;

const double eps = 1e-8;
const double PI = 2 * acos(0.0);

int cc, bb;
char ch;
inline int F() {
  while (ch = getchar(), (ch < 48 || ch > 57) && ch != '-')
    ;
  ch == '-' ? cc = bb = 0 : (cc = ch - '0', bb = 1);
  while (ch = getchar(), ch > 47 && ch < 58)
    cc = (cc << 1) + (cc << 3) + (ch ^ 48);
  return bb ? cc : -cc;
}
void write(int x) {
  int y = 10, len = 1;
  while (y <= x) {
    y *= 10;
    len++;
  }
  while (len--) {
    y /= 10;
    putchar(x / y + 48);
    x %= y;
  }
}
struct edge {
  int u, v, next;
  edge() {}
  edge(int u, int v, int next) : u(u), v(v), next(next) {}
};
edge g[maxm];
int st[maxn];
int n, m;
int dp[maxn];
void init() {
  m = 0;
  memset(st, -1, sizeof st);
}
void addedge(int u, int v) {
  g[m] = edge(u, v, st[u]);
  st[u] = m++;
  g[m] = edge(v, u, st[v]);
  st[v] = m++;
}
int fac[maxn];
int inv[maxn], ifac[maxn];
int siz[maxn];            // 标记每个节点的子树大小
void dfs1(int x, int f) { // x当前节点
  siz[x] = 1;
  dp[x] = 1;
  for (int i = st[x]; i != -1; i = g[i].next) {
    int y = g[i].v;
    if (y == f)
      continue;
    dfs1(y, x);
    siz[x] += siz[y];
    dp[x] = 1ll * dp[x] * dp[y] % modn;
    // dp[x] = 1ll * dp[x] * dp[y] % modn * ifac[siz[y]] % modn;
  }
  dp[x] = 1ll * dp[x] * inv[siz[x]] % modn; // 实际结果等于dp[x]*fac[siz[x]]
  // dp[x] = 1ll * dp[x] * fac[siz[x] - 1] % modn;
}
void dfs(int u, int f) {
  // dp[u] = fac[siz[u] - 1];
  int v;
  for (int i = st[u]; i != -1; i = g[i].next) {
    v = g[i].v;
    if (v == f)
      continue;
    dp[v] = 1ll * dp[u] * siz[v] % modn * inv[n - siz[v]] % modn;
    dfs(v, u);
    // dp[u] = 1ll * dp[u] * dp[v] % modn * ifac[siz[v]] % modn;
  }
}
int main() {
  // ios::sync_with_stdio(0);
  // cin.tie(0);
  // FILE* stream = fopen("L:/20.in", "rb+")
  scan(n);
  int u, v;
  init();
  for (int i = 1; i < n; i++) {
    // u = i, v = i + 1;
    scan2(u, v);
    addedge(u, v);
  }
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1ll * fac[i - 1] * i % modn;
  inv[0] = inv[1] = ifac[0] = 1;
  for (int i = 2; i <= n; i++) // 求2-n的逆元
    inv[i] = 1ll * inv[modn % i] * (modn - modn / i) % modn;
  // for (int i = 1; i <= n; i++)//求1!到n!的逆元
  //	ifac[i] = 1ll * ifac[i - 1] * inv[i] % modn;
  dfs1(1, 0);
  // dp[1] = 1ll * dp[1] * fac[siz[1]] % modn;
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printl(1ll * dp[i] * fac[n] % modn);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
// #include <unordered_map>
#include <cassert>
#include <complex>
#include <cstdlib>
#include <iterator>
#include <numeric>
#include <stack>
using namespace std;
#define maxn 200007
#define maxm 400007
#define maxq 1000007
#define maxl 1234
#define modn 1000000007
#define scan(a) scanf("%d", &a)
#define scanl(a) scanf("%lld", &a)
#define scanl2(a, b) scanf("%lld%lld", &a, &b)
#define scans(a) scanf("%s", a)
#define scans2(a, b) scanf("%s%s", a, b)
#define scand(a) scanf("%lf", &a)
#define scan2(a, b) scanf("%d%d", &a, &b)
#define scand2(a, b) scanf("%lf%lf", &a, &b)
#define scan3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scan4(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define scan5(a, b, c, d, e) scanf("%d%d%d%d%d", &a, &b, &c, &d, &e)
#define print(a) printf("%d\\n", a);
#define printl(a) printf("%lld\\n", a);
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fscan(a) fscanf(stream, "%d", &a);
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define mm(a, b) memset(a, b, sizeof a)
#define log2(n) (double)(log(n) / log(2.0))
#define _crt_disable_perfcrit_locks
#define bll __int128
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef map<ll, ll> mll;
typedef pair<ll, ll> pll;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef map<char, int> mci;
typedef map<char, char> mcc;
typedef pair<char, int> pci;
typedef complex<double> cd;
typedef vector<complex<double>> vcd;

const double eps = 1e-8;
const double PI = 2 * acos(0.0);

int cc, bb;
char ch;
inline int F() {
  while (ch = getchar(), (ch < 48 || ch > 57) && ch != '-')
    ;
  ch == '-' ? cc = bb = 0 : (cc = ch - '0', bb = 1);
  while (ch = getchar(), ch > 47 && ch < 58)
    cc = (cc << 1) + (cc << 3) + (ch ^ 48);
  return bb ? cc : -cc;
}
void write(int x) {
  int y = 10, len = 1;
  while (y <= x) {
    y *= 10;
    len++;
  }
  while (len--) {
    y /= 10;
    putchar(x / y + 48);
    x %= y;
  }
}
struct edge {
  int u, v, next;
  edge() {}
  edge(int u, int v, int next) : u(u), v(v), next(next) {}
};
edge g[maxm];
int st[maxn];
int n, m;
int dp[maxn];
void init() {
  m = 0;
  memset(st, -1, sizeof st);
}
void addedge(int u, int v) {
  g[m] = edge(u, v, st[u]);
  st[u] = m++;
  g[m] = edge(v, u, st[v]);
  st[v] = m++;
}
int fac[maxn];
int inv[maxn], ifac[maxn];
int siz[maxn];            // 标记每个节点的子树大小
void dfs1(int x, int f) { // x当前节点
  siz[x] = 1;
  for (int i = st[x]; i != -1; i = g[i].next) {
    int y = g[i].v;
    if (y == f)
      continue;
    dfs1(y, x);
    siz[x] += siz[y];
  }
}
void dfs(int u, int f) {
  dp[u] = fac[siz[u] - 1];
  int v;
  for (int i = st[u]; i != -1; i = g[i].next) {
    v = g[i].v;
    if (v == f)
      continue;
    dfs(v, u);
    dp[u] = 1ll * dp[u] * dp[v] % modn * ifac[siz[v]] % modn;
  }
}
int main() {
  // ios::sync_with_stdio(0);
  // cin.tie(0);
  // FILE* stream = fopen("L:/20.in", "rb+")
  scan(n);
  int u, v;
  init();
  for (int i = 1; i < n; i++) {
    scan2(u, v);
    addedge(u, v);
  }
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1ll * fac[i - 1] * i % modn;
  inv[0] = inv[1] = ifac[0] = 1;
  for (int i = 2; i <= n; i++) // 求2-n的逆元
    inv[i] = 1ll * inv[modn % i] * (modn - modn / i) % modn;
  for (int i = 1; i <= n; i++) // 求1!到n!的逆元
    ifac[i] = 1ll * ifac[i - 1] * inv[i] % modn;
  for (int i = 1; i <= n; i++) {
    // memset(dp, -1, sizeof dp);
    dfs1(i, 0);
    dfs(i, 0);
    print(dp[i]);
  }
  return 0;
}