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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define trace(x) cerr << #x << ": " << x << " " << endl;

typedef int64_t ll;

#define endl '\\n'
#define int ll
ll mod = 1e9 + 7;
ll mod1 = 1e9 + 5;

ll power(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0)
    return power((((a % mod) * (a % mod)) % mod), b / 2) % mod;
  else
    return ((a % mod) * (power((((a % mod) * (a % mod)) % mod), b / 2) % mod)) %
           mod;
}

// using namespace __gnu_pbds;
// #define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>,
// rb_tree_tag,tree_order_statistics_node_update>
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
// erase,insert same as normal set
//  mt19937
//  mrand(chrono::high_resolution_clock::now().time_since_epoch().count()); for
//  randomized solution

vector<int> v[200001];
int dp[200001];
int fact[200001];
int sub[200001];
int n;
void dfs(int u, int par) {
  int te = 0;
  int k = 1;
  int f = 1;
  for (auto i : v[u]) {
    if (i == par)
      continue;
    dfs(i, u);
    te += sub[i];
    k *= power(fact[sub[i]], mod - 2);
    k %= mod;
    f *= dp[i];
    f %= mod;
  }
  k *= fact[te];
  k %= mod;

  k *= f;
  dp[u] = k;
  dp[u] %= mod;
  sub[u] = te + 1;
}
int dp1[200001];
int dfs1(int u, int par, int ans, int size) {
  if (par != 0) {
    int te = 0;
    int k = 1;
    int f = ans;
    for (auto i : v[u]) {
      if (i == par)
        continue;
      te += sub[i];
      k *= power(fact[sub[i]], mod - 2);
      k %= mod;
      f *= dp[i];
      f %= mod;
    }
    k *= power(fact[size], mod - 2);
    k %= mod;
    k *= fact[te + size];
    k %= mod;

    k *= f;
    k %= mod;

    dp1[u] = k;
  }

  int te = 1;
  int x = 0;
  int f = 1;
  for (auto i : v[u]) {
    if (i == par)
      continue;
    te *= power(fact[sub[i]], mod - 2);
    te %= mod;
    te *= dp[i];
    te %= mod;
    x += sub[i];
  }
  x += size;
  te *= power(fact[size], mod - 2);
  te %= mod;
  te *= ans;
  te %= mod;

  for (auto i : v[u]) {
    if (i == par)
      continue;
    int temp = te * fact[sub[i]];
    temp %= mod;

    temp *= fact[x - sub[i]];

    temp %= mod;
    temp *= power(dp[i], mod - 2);
    temp %= mod;

    dfs1(i, u, temp, n - sub[i]);
  }
}
int32_t main() {
  IOS

          cin >>
      n;
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  fact[0] = 1;
  for (int i = 1; i <= 200000; i++)
    fact[i] = fact[i - 1] * i, fact[i] %= mod;
  dfs(1, 0);
  dp1[1] = dp[1];
  dfs1(1, 0, 1, 0);
  for (int i = 1; i <= n; i++)
    cout << dp1[i] << " ";
}


Error: TLE

Fixed submission:

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define trace(x) cerr << #x << ": " << x << " " << endl;

typedef int64_t ll;

#define endl '\\n'
#define int ll
ll mod = 1e9 + 7;
ll mod1 = 1e9 + 5;

ll power(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0)
    return power((((a % mod) * (a % mod)) % mod), b / 2) % mod;
  else
    return ((a % mod) * (power((((a % mod) * (a % mod)) % mod), b / 2) % mod)) %
           mod;
}

// using namespace __gnu_pbds;
// #define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>,
// rb_tree_tag,tree_order_statistics_node_update>
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
// erase,insert same as normal set
//  mt19937
//  mrand(chrono::high_resolution_clock::now().time_since_epoch().count()); for
//  randomized solution

vector<int> v[200001];
int dp[200001];
int fact[200001];
int sub[200001];
void dfs(int u, int par) {
  int te = 0;
  int k = 1;
  int f = 1;
  for (auto i : v[u]) {
    if (i == par)
      continue;
    dfs(i, u);
    te += sub[i];
    k *= power(fact[sub[i]], mod - 2);
    k %= mod;
    f *= dp[i];
    f %= mod;
  }
  k *= fact[te];
  k %= mod;

  k *= f;
  dp[u] = k;
  dp[u] %= mod;
  sub[u] = te + 1;
}
int32_t main() {
  IOS

      int n;
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  fact[0] = 1;
  for (int i = 1; i <= 200000; i++)
    fact[i] = fact[i - 1] * i, fact[i] %= mod;
  for (int j = 1; j <= n; j++) {
    dfs(j, 0);

    cout << dp[j] << endl;
  }
}