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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unordered_map>

typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<long long, long long> Pll;
typedef std::pair<double, double> Pdd;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define mapa make_pair
#define all(a) a.begin(), a.end()
#define MM << " " <<

template <typename T> using MaxHeap = std::priority_queue<T>;
template <typename T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> std::vector<T> vec(int len, T elem) {
  return std::vector<T>(len, elem);
}
template <typename T> void vdeb(std::vector<T> &da) {
  for (int i = 0; i < da.size(); i++) {
    if (i == da.size() - 1)
      std::cout << da[i];
    else
      std::cout << da[i] << ' ';
  }
  std::cout << std::endl;
}
template <typename T> void vdeb(std::vector<std::vector<T>> &da) {
  for (int i = 0; i < da.size(); i++)
    vdeb(da[i]);
  std::cout << std::endl;
}

long long pow(long long n, long long p, long long k) { // n^k(mod p)
  if (!k)
    return 1;
  long long a = pow(n, p, k >> 1);
  a = a * a % p;
  if (k & 1)
    a = a * n % p;
  return a;
}
void euclid(long long &a, long long &b, long long p) { // a>=b A*b+B*(a-a/b*b)=1
  if (a == 1) {
    a = 1;
  } else {
    long long A = b, B = a % b;
    euclid(A, B, p);
    b = (A - (p + a / b) % p * B % p + p) % p;
    a = B;
  }
}
long long rev(long long n, long long p) { // nの逆元 n*x-p*y=1
  // long long q = p;
  // euclid(p, n, p);
  // return n % q;
  return pow(n, p, p - 2);
}
long long bino(long long n, long long m, long long p) { // nCm(mod p)
  long long ans = 1, div = 1;
  for (int i = 0; i < m; i++) {
    ans = (n - i) * ans % p;
    div = div * (i + 1) % p;
  }
  return ans * rev(div, p) % p;
}
struct modint {
  long long num;
  long long p;
  modint() {
    num = 0;
    p = 1000000007;
  }
  modint(int x) {
    p = 1000000007;
    num = (long long)x % p;
  }
  modint(long long x) {
    p = 1000000007;
    num = x % p;
  }
  modint operator+(const modint &other) {
    modint ret;
    ret.p = p;
    ret.num = (num + other.num) % p;
    return ret;
  }
  modint operator+(const long long &other) {
    modint ret;
    ret.p = p;
    ret.num = (num + (other % p)) % p;
    return ret;
  }
  modint operator-(const modint &other) {
    modint ret;
    ret.p = p;
    ret.num = (num - other.num + p) % p;
    return ret;
  }
  modint operator-(const long long &other) {
    modint ret;
    ret.p = p;
    ret.num = (num - (other % p) + p) % p;
    return ret;
  }
  modint operator*(const modint &other) {
    modint ret;
    ret.p = p;
    ret.num = (num * other.num) % p;
    return ret;
  }
  modint operator*(const long long &other) {
    modint ret;
    ret.p = p;
    ret.num = (num * (other % p)) % p;
    return ret;
  }
  modint operator/(const modint &other) {
    modint ret;
    ret.p = p;
    ret.num = (num * rev(other.num, p)) % p;
    return ret;
  }
  modint operator/(const long long &other) {
    modint ret;
    ret.p = p;
    ret.num = (num * rev(other % p, p)) % p;
    return ret;
  }
  void operator+=(const long long &other) { num = (num + other) % p; }
  void operator+=(const modint &other) { num = (num + other.num) % p; }
  void operator-=(const long long &other) { num = (num - other + p) % p; }
  void operator-=(const modint &other) { num = (num - other.num + p) % p; }
  void operator*=(const long long &other) { num = (num * other) % p; }
  void operator*=(const modint &other) { num = (num * other.num) % p; }
};
std::ostream &operator<<(std::ostream &os, const modint &x) {
  std::cout << x.num;
  return os;
}

using namespace std;

struct P {
  modint c;
  int to, si;
  bool flag = false;
  P() {}
  P(int _to) {
    c = modint(1);
    to = _to;
    si = 1;
  }
};

struct DP {
  modint c;
  int si;
  DP() {
    c = modint(1);
    si = 1;
  }
};

vector<modint> li(220000);
vector<modint> inv(220000);
vector<DP> nod(220000);

void merge(DP &x, P &y) {
  x.c *= y.c * li[x.si + y.si - 1] * inv[x.si - 1] * inv[y.si];
  x.si += y.si;
  return;
}

void merge(P &x, DP &y, P ops) {
  x.c = y.c / ops.c * inv[y.si - 1] * li[ops.si] * li[y.si - ops.si - 1];
  x.si = y.si - ops.si;
  x.flag = false;
  return;
}

void dfs1(int x, int y, int z, vector<vector<P>> &da) { // da[x][z].to = y, x->y
  if (da[x][z].flag)
    return;
  rip(i, da[y].size(), 0) {
    if (da[y][i].to == x)
      continue;
    dfs1(y, da[y][i].to, i, da);
    merge(nod[y], da[y][i]);
  }
  da[x][z].flag = false;
  da[x][z].si = nod[y].si;
  da[x][z].c = nod[y].c;
}

void dfs2(int x, int y, int z, vector<vector<P>> &da) {
  rip(i, da[y].size(), 0) {
    if (da[y][i].to == x) {
      merge(da[y][i], nod[x], da[x][z]);
      merge(nod[y], da[y][i]);
      break;
    }
  }
  rip(i, da[y].size(), 0) {
    if (da[y][i].to == x)
      continue;
    dfs2(y, da[y][i].to, i, da);
  }
  return;
}

void rooting(vector<vector<P>> &da) {
  rip(i, da[0].size(), 0) {
    dfs1(0, da[0][i].to, i, da);
    merge(nod[0], da[0][i]);
  }
  rip(i, da[0].size(), 0) { dfs2(0, da[0][i].to, i, da); }
  return;
}

int main() {
  li[0].num = 1;
  inv[0].num = 1;
  rip(i, 220000, 1) {
    li[i] = li[i - 1] * i;
    inv[i] = rev(li[i].num, 1000000007);
  }
  int n;
  cin >> n;
  vector<vector<P>> da(n, vector<P>(0));
  rip(i, n - 1, 0) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    da[a].push_back(P(b));
    da[b].push_back(P(a));
  }
  rooting(da);
  rip(i, n, 0) { cout << nod[i].c << endl; }
}

Error: TLE

Fixed submission:

// #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unordered_map>

typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<long long, long long> Pll;
typedef std::pair<double, double> Pdd;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define mapa make_pair
#define all(a) a.begin(), a.end()
#define MM << " " <<

template <typename T> using MaxHeap = std::priority_queue<T>;
template <typename T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> std::vector<T> vec(int len, T elem) {
  return std::vector<T>(len, elem);
}
template <typename T> void vdeb(std::vector<T> &da) {
  for (int i = 0; i < da.size(); i++) {
    if (i == da.size() - 1)
      std::cout << da[i];
    else
      std::cout << da[i] << ' ';
  }
  std::cout << std::endl;
}
template <typename T> void vdeb(std::vector<std::vector<T>> &da) {
  for (int i = 0; i < da.size(); i++)
    vdeb(da[i]);
  std::cout << std::endl;
}

long long pow(long long n, long long p, long long k) { // n^k(mod p)
  if (!k)
    return 1;
  long long a = pow(n, p, k >> 1);
  a = a * a % p;
  if (k & 1)
    a = a * n % p;
  return a;
}
void euclid(long long &a, long long &b, long long p) { // a>=b A*b+B*(a-a/b*b)=1
  if (a == 1) {
    a = 1;
  } else {
    long long A = b, B = a % b;
    euclid(A, B, p);
    b = (A - (p + a / b) % p * B % p + p) % p;
    a = B;
  }
}
long long rev(long long n, long long p) { // nの逆元 n*x-p*y=1
  // long long q = p;
  // euclid(p, n, p);
  // return n % q;
  return pow(n, p, p - 2);
}
long long bino(long long n, long long m, long long p) { // nCm(mod p)
  long long ans = 1, div = 1;
  for (int i = 0; i < m; i++) {
    ans = (n - i) * ans % p;
    div = div * (i + 1) % p;
  }
  return ans * rev(div, p) % p;
}
struct modint {
  long long num;
  long long p;
  modint() {
    num = 0;
    p = 1000000007;
  }
  modint(int x) {
    p = 1000000007;
    num = (long long)x % p;
  }
  modint(long long x) {
    p = 1000000007;
    num = x % p;
  }
  modint operator+(const modint &other) {
    modint ret;
    ret.p = p;
    ret.num = (num + other.num) % p;
    return ret;
  }
  modint operator+(const long long &other) {
    modint ret;
    ret.p = p;
    ret.num = (num + (other % p)) % p;
    return ret;
  }
  modint operator-(const modint &other) {
    modint ret;
    ret.p = p;
    ret.num = (num - other.num + p) % p;
    return ret;
  }
  modint operator-(const long long &other) {
    modint ret;
    ret.p = p;
    ret.num = (num - (other % p) + p) % p;
    return ret;
  }
  modint operator*(const modint &other) {
    modint ret;
    ret.p = p;
    ret.num = (num * other.num) % p;
    return ret;
  }
  modint operator*(const long long &other) {
    modint ret;
    ret.p = p;
    ret.num = (num * (other % p)) % p;
    return ret;
  }
  modint operator/(const modint &other) {
    modint ret;
    ret.p = p;
    ret.num = (num * rev(other.num, p)) % p;
    return ret;
  }
  modint operator/(const long long &other) {
    modint ret;
    ret.p = p;
    ret.num = (num * rev(other % p, p)) % p;
    return ret;
  }
  void operator+=(const long long &other) { num = (num + other) % p; }
  void operator+=(const modint &other) { num = (num + other.num) % p; }
  void operator-=(const long long &other) { num = (num - other + p) % p; }
  void operator-=(const modint &other) { num = (num - other.num + p) % p; }
  void operator*=(const long long &other) { num = (num * other) % p; }
  void operator*=(const modint &other) { num = (num * other.num) % p; }
};
std::ostream &operator<<(std::ostream &os, const modint &x) {
  std::cout << x.num;
  return os;
}

using namespace std;

struct P {
  int to, si;
  modint c;
  bool flag;
  P() {}
  P(int _to) {
    to = _to;
    si = 1;
    flag = false;
  }
};

vector<modint> li(220000);
vector<modint> inv(220000);
vector<modint> gg(220000);

modint cal(vector<int> &da) {
  modint ret(1);
  int co = 0;
  rip(i, da.size(), 0) {
    co += da[i];
    ret *= inv[da[i]];
  }
  ret *= li[co];
  return ret;
}

void rec(int x, int y, int z, vector<vector<P>> &da) { // x->y
  if (da[x][z].flag)
    return;
  modint ret(1);
  vector<int> me(0);
  rip(i, da[y].size(), 0) {
    if (da[y][i].to == x)
      continue;
    rec(y, da[y][i].to, i, da);
    ret *= da[y][i].c;
    me.push_back(da[y][i].si);
    da[x][z].si += da[y][i].si;
  }
  ret *= cal(me);
  da[x][z].c = ret;
  da[x][z].flag = true;
  return;
}

modint ans_cal(int x, vector<vector<P>> &da) {
  modint ret(1);
  vector<int> me(0);
  rip(i, da[x].size(), 0) ret *= da[x][i].c, me.push_back(da[x][i].si);
  ret *= cal(me);
  return ret;
}

int main() {
  li[0].num = 1;
  inv[0].num = 1;
  rip(i, 220000, 1) {
    li[i] = li[i - 1] * i;
    inv[i] = rev(li[i].num, 1000000007);
  }
  int n;
  cin >> n;
  vector<vector<P>> da(n, vector<P>(0));
  rip(i, n - 1, 0) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    da[a].push_back(P(b));
    da[b].push_back(P(a));
  }
  rip(i, n, 0) { rip(j, da[i].size(), 0) rec(i, da[i][j].to, j, da); }
  vector<modint> ans(n);
  rip(i, n, 0) { ans[i] = ans_cal(i, da); }
  rip(i, n, 0) cout << ans[i] << endl;
}