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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \\
       tree_order_statistics_node_update>
#define pb push_back
#define ff first
#define ss second
#define endl '\\n'
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);
const double eps = 1e-6;

/////////////////////////////////////////////

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

////////////////////////////////

#define int long long

const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int sz[N];
int mul1[N];
int mul2[N];
int mul;
int fact[N];
int myans[N];
vector<int> v[N];
int n;

void start() {

  fact[0] = 1;

  for (int i = 1; i < N; ++i) {

    fact[i] = fact[i - 1] * i;
    fact[i] %= mod;
  }
}

int power(int a, int n) {

  a %= mod;
  int res = 1;

  while (n) {

    if (n & 1)
      res *= a, res %= mod;
    a *= a;
    a %= mod;
    n /= 2;
  }

  return res;
}

int dfs(int s, int p = 0) {

  sz[s] = 1;

  for (int it : v[s]) {

    if (it != p) {

      sz[s] += dfs(it, s);
    }
  }
  return sz[s];
}

void reroot(int s, int p = 0) {

  for (int it : v[s]) {

    if (it != p) {

      int x = sz[s];
      int y = sz[it];
      sz[s] = n - sz[it];
      sz[it] = n;

      mul = mul * power(x * y % mod, mod - 2);
      mul %= mod;
      mul = mul * sz[it] % mod;
      mul = mul * sz[s] % mod;
      mul %= mod;
      mul1[it] = mul;
      reroot(it, s);

      x = sz[s];
      y = sz[it];
      sz[it] = n - sz[s];
      sz[s] = n;
      mul = mul * power(x * y % mod, mod - 2);
      mul %= mod;
      mul = mul * sz[it] % mod;
      mul = mul * sz[s] % mod;
      mul %= mod;
    }
  }
}

void solve() {

  // freopen("output.txt" , "r" , stdin);
  // freopen("input.txt" , "w" , stdout);
  start();
  cin >> n;
  mul = 1;
  for (int i = 1; i < n; ++i) {

    int x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }

  dfs(1);
  for (int i = 1; i <= n; ++i)
    mul *= sz[i], mul %= mod;

  mul1[1] = mul;
  reroot(1);
  bool flag = false;
  for (int i = 1; i <= n; ++i) {

    if (mul1[i] < 0) {

      flag = true;
    }
    myans[i] = (fact[n] * power(mul1[i], mod - 2)) % mod;
    myans[i] %= mod;
  }
  for (int i = 1; i <= n; ++i)
    cout << myans[i] << endl;
  if (flag)
    cerr << "Yes\\n";
  return;
}

signed main() {

  fast;
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; ++i) {

    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \\
       tree_order_statistics_node_update>
#define pb push_back
#define ff first
#define ss second
#define endl '\\n'
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);
const double eps = 1e-6;

/////////////////////////////////////////////

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

////////////////////////////////

#define int long long

// Panchi banu udti phiru mast gagan me
// tera mera pyar amar .
// Pyar hua ekrar hua .
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int sz[N];
int mul1[N];
int mul2[N];
int mul;
int fact[N];
int ans[N];
vector<int> v[N];
int n;

void start() {

  fact[0] = 1;

  for (int i = 1; i < N; ++i) {

    fact[i] = fact[i - 1] * i;
    fact[i] %= mod;
  }
}

int power(int a, int n) {

  a %= mod;
  int res = 1;

  while (n) {

    if (n & 1)
      res *= a, res %= mod;
    a *= a;
    a %= mod;
    n /= 2;
  }

  return res;
}

int dfs(int s, int p = 0) {

  sz[s] = 1;

  for (int it : v[s]) {

    if (it != p) {

      sz[s] += dfs(it, s);
    }
  }
  return sz[s];
}

void reroot(int s, int p = 0) {

  for (int it : v[s]) {

    if (it != p) {

      int x = sz[s];
      int y = sz[it];
      sz[s] = n - sz[it];
      sz[it] = n;

      mul = mul * power(x * y % mod, mod - 2);
      mul %= mod;
      mul = mul * (sz[it] * sz[s]) % mod;
      mul %= mod;
      mul1[it] = mul;
      reroot(it, s);

      x = sz[s];
      y = sz[it];
      sz[it] = n - sz[s];
      sz[s] = n;
      mul = mul * power(x * y % mod, mod - 2);
      mul %= mod;
      mul = mul * (sz[it] * sz[s]) % mod;
      mul %= mod;
    }
  }
}

void solve() {

  // freopen("output.txt" , "r" , stdin);
  start();
  cin >> n;
  mul = 1;
  for (int i = 1; i < n; ++i) {

    int x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }

  dfs(1);
  for (int i = 1; i <= n; ++i) {

    // cout<<sz[i]<<endl;
  }
  for (int i = 1; i <= n; ++i)
    mul *= sz[i], mul %= mod;

  mul1[1] = mul;
  reroot(1);

  for (int i = 1; i <= n; ++i) {

    ans[i] = (fact[n] * power(mul1[i], mod - 2)) % mod;
    ans[i] %= mod;
  }
  for (int i = 1; i <= n; ++i) {

    mul2[i] = 1;

    dfs(i);

    for (int j = 1; j <= n; ++j) {

      mul2[i] *= sz[j];
      mul2[i] %= mod;
    }
  }

  for (int i = 1; i <= n; ++i) {

    // cout<<mul1[i]<<" "<<mul2[i]<<endl;
    ans[i] = fact[n] * power(mul2[i], mod - 2) % mod;
    ans[i] %= mod;
  }

  for (int i = 1; i <= n; ++i)
    cout << ans[i] << endl;

  // cout<<"Perfect\\n";
}

signed main() {

  fast;
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; ++i) {

    solve();
  }
}