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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

ll Pow(ll A, ll N) {
  if (N == 0)
    return 1;
  if (N % 2 == 0)
    return Pow(A * A % MOD, N / 2);
  else
    return Pow(A * A % MOD, N / 2) * A % MOD;
}

int N;
ll fac[int(2e5 + 5)], rev[int(2e5 + 5)];
vector<int> Graph[int(2e5 + 5)];

int Dist[int(2e5 + 5)], Have[int(2e5 + 5)];
int Parent[int(2e5 + 5)];
int Calc_Dist(int start) {
  Dist[start] = 1;
  queue<int> que;
  que.push(start);
  while (!que.empty()) {
    int Q = que.front();
    que.pop();
    for (int v : Graph[Q]) {
      if (Dist[v] > 0) {
        Parent[Q] = v;
        continue;
      }
      Dist[v] = Dist[Q] + 1;
      que.push(v);
    }
  }
  return 0;
}

int Calc_Child(int num) {
  if (Have[num] > 0)
    return Have[num];
  int ans = 1;
  for (int v : Graph[num]) {
    if (Dist[num] > Dist[v])
      continue;
    ans += Calc_Child(v);
  }
  return Have[num] = ans;
}

ll DP[int(2e5 + 5)];
bool visited[int(2e5 + 5)];
ll Calc_DP(int num) {
  if (visited[num])
    return DP[num];
  visited[num] = true;
  ll ans = fac[Have[num] - 1];

  for (int v : Graph[num]) {
    if (Dist[num] > Dist[v])
      continue;
    ans *= Calc_DP(v);
    ans %= MOD;
    ans *= rev[Have[v]];
    ans %= MOD;
  }
  return DP[num] = ans;
}

ll DP2[int(2e5 + 5)];
ll Calc_DP2(int num) {
  if (visited[num])
    return DP2[num];
  visited[num] = true;
  if (Parent[num] == 0)
    return DP2[num] = 1;

  ll ans = fac[N - Have[num] - 1];
  ans *= Calc_DP2(Parent[num]);
  ans %= MOD;
  ans *= rev[N - Have[Parent[num]]];
  ans %= MOD;

  ans *= DP[Parent[num]];
  ans %= MOD;
  ans *= rev[Have[Parent[num]] - 1];
  ans %= MOD;
  ans *= fac[Have[num]];
  ans %= MOD;
  ans *= Pow(DP[num], MOD - 2);
  ans %= MOD;

  return DP2[num] = ans;
}

int main() {
  cin >> N;
  fac[0] = 1;
  rev[0] = 1;
  for (ll i = 1; i <= N; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    rev[i] = Pow(fac[i], MOD - 2);
  }

  for (int i = 0; i < N - 1; i++) {
    int A, B;
    cin >> A >> B;
    Graph[A].push_back(B);
    Graph[B].push_back(A);
  }
  Calc_Dist(1);
  Calc_Child(1);
  Calc_DP(1);

  fill(visited, visited + N + 1, false);

  for (int i = 1; i <= N; i++) {
    if (!visited[i])
      Calc_DP2(i);
  }

  for (int i = 1; i <= N; i++) {
    ll ans = fac[N - 1];
    ans *= DP[i];
    ans %= MOD;
    ans *= rev[Have[i] - 1];
    ans %= MOD;
    ans *= DP2[i];
    ans %= MOD;
    ans *= rev[N - Have[i]];
    ans %= MOD;
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

ll Pow(ll A, ll N) {
  if (N == 0)
    return 1;
  if (N % 2 == 0)
    return Pow(A * A % MOD, N / 2);
  else
    return Pow(A * A % MOD, N / 2) * A % MOD;
}

int N;
ll fac[int(2e5 + 5)], rev[int(2e5 + 5)];
vector<int> Graph[int(2e5 + 5)];

int Dist[int(2e5 + 5)], Have[int(2e5 + 5)];
int Parent[int(2e5 + 5)];
int Calc_Dist(int start) {
  Dist[start] = 1;
  queue<int> que;
  que.push(start);
  while (!que.empty()) {
    int Q = que.front();
    que.pop();
    for (int v : Graph[Q]) {
      if (Dist[v] > 0) {
        Parent[Q] = v;
        continue;
      }
      Dist[v] = Dist[Q] + 1;
      que.push(v);
    }
  }
  return 0;
}

int Calc_Child(int num) {
  if (Have[num] > 0)
    return Have[num];
  int ans = 1;
  for (int v : Graph[num]) {
    if (Dist[num] > Dist[v])
      continue;
    ans += Calc_Child(v);
  }
  return Have[num] = ans;
}

ll DP[int(2e5 + 5)];
bool visited[int(2e5 + 5)];
ll Calc_DP(int num) {
  if (visited[num])
    return DP[num];
  visited[num] = true;
  ll ans = fac[Have[num] - 1];

  for (int v : Graph[num]) {
    if (Dist[num] > Dist[v])
      continue;
    ans *= Calc_DP(v);
    ans %= MOD;
    ans *= rev[Have[v]];
    ans %= MOD;
  }
  return DP[num] = ans;
}

ll DP2[int(2e5 + 5)];
ll Calc_DP2(int num) {
  if (visited[num])
    return DP2[num];
  visited[num] = true;
  if (Parent[num] == 0)
    return DP2[num] = 1;

  ll ans = fac[N - Have[num] - 1];
  ans *= Calc_DP2(Parent[num]);
  ans %= MOD;
  ans *= rev[N - Have[Parent[num]]];
  ans %= MOD;

  for (int v : Graph[Parent[num]]) {
    if (v == num)
      continue;
    if (v == Parent[Parent[num]])
      continue;
    ans *= DP[v];
    ans %= MOD;
    ans *= rev[Have[v]];
    ans %= MOD;
  }
  return DP2[num] = ans;
}

int main() {
  cin >> N;
  fac[0] = 1;
  rev[0] = 1;
  for (ll i = 1; i <= N; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    rev[i] = Pow(fac[i], MOD - 2);
  }

  for (int i = 0; i < N - 1; i++) {
    int A, B;
    cin >> A >> B;
    Graph[A].push_back(B);
    Graph[B].push_back(A);
  }
  Calc_Dist(N - 1);
  Calc_Child(N - 1);
  Calc_DP(N - 1);

  fill(visited, visited + N + 1, false);

  for (int i = 1; i <= N; i++) {
    if (!visited[i])
      Calc_DP2(i);
  }

  for (int i = 1; i <= N; i++) {
    ll ans = fac[N - 1];
    ans *= DP[i];
    ans %= MOD;
    ans *= rev[Have[i] - 1];
    ans %= MOD;
    ans *= DP2[i];
    ans %= MOD;
    ans *= rev[N - Have[i]];
    ans %= MOD;
    cout << ans << endl;
  }

  return 0;
}