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

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define LL long long
#define y second
#define sqr(x) ((x) * (x))
#define pii pair<int, int>
#define pll pair<LL, LL>
#define pdd pair<double, double>
#define MEM(x) memset(x, 0, sizeof(x))
#define MEMS(x) memset(x, -1, sizeof(x))
using namespace std;
vector<pair<int, pii>> v[200005];
map<int, pii> ma[200005];
LL fra[200005];
LL inv[200005];
LL val[200005];
LL Size[200005];
int mod = 1e9 + 7;
LL f_pow(LL a, LL b) {
  LL res = 1, temp = a;
  while (b) {
    if (b & 1)
      res = res * temp % mod;
    temp = temp * temp % mod;
    b >>= 1;
  }
  return res;
}
pll dfs(int x, int f) {
  LL a = val[x], b = Size[x];
  for (auto &it : v[x]) {
    if (it.x == f) {
      swap(v[x][0], it);
    }
    if (it.x != f) {
      if (it.y.x == -1) {
        it.y = dfs(it.x, x);
        ma[x][it.x] = it.y;
        a *= it.y.x;
        a %= mod;
        a *= inv[it.y.y];
        a %= mod;
        val[x] *= it.y.x;
        val[x] %= mod;
        val[x] *= inv[it.y.y];
        val[x] %= mod;
        Size[x] += it.y.y;
        b += it.y.y;
      }
    }
  }

  if (v[x][0].x == f)
    v[x].resize(1);
  else
    v[x].clear();
  if (ma[x].find(f) != ma[x].end()) {
    pii p = ma[x][f];
    a *= f_pow(p.x, mod - 2);
    a %= mod;
    a *= fra[p.y];
    a %= mod;
    b -= p.y;
  }
  // printf("%d %d %lld %lld\\n",x,f,a,b);
  a *= fra[b];
  a %= mod;

  b++;
  return mp(a, b);
}
int main() {
  int n;
  scanf("%d", &n);
  fill(val, val + n + 1, 1);
  fra[0] = 1;
  for (int i = 1; i <= n; i++)
    fra[i] = fra[i - 1] * i % mod;
  inv[n] = f_pow(fra[n], mod - 2);
  for (int i = n - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x].pb(mp(y, mp(-1, -1)));
    v[y].pb(mp(x, mp(-1, -1)));
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld\\n", dfs(i, 0).x);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define LL long long
#define y second
#define sqr(x) ((x) * (x))
#define pii pair<int, int>
#define pll pair<LL, LL>
#define pdd pair<double, double>
#define MEM(x) memset(x, 0, sizeof(x))
#define MEMS(x) memset(x, -1, sizeof(x))
using namespace std;
vector<pair<int, pii>> v[200005];
LL fra[200005];
LL inv[200005];
int mod = 1e9 + 7;
LL f_pow(LL a, LL b) {
  LL res = 1, temp = a;
  while (b) {
    if (b & 1)
      res = res * temp % mod;
    temp = temp * temp % mod;
    b >>= 1;
  }
  return res;
}
pll dfs(int x, int f) {
  LL a = 1, b = 0;
  for (auto &it : v[x]) {
    if (it.x != f) {
      if (it.y.x == -1) {
        it.y = dfs(it.x, x);
      }
      a *= it.y.x;
      a %= mod;
      a *= inv[it.y.y];
      a %= mod;
      b += it.y.y;
    }
  }
  a *= fra[b];
  a %= mod;
  b++;
  return mp(a, b);
}
int main() {
  int n;
  scanf("%d", &n);
  fra[0] = 1;
  for (int i = 1; i <= n; i++)
    fra[i] = fra[i - 1] * i % mod;
  inv[n] = f_pow(fra[n], mod - 2);
  for (int i = n - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x].pb(mp(y, mp(-1, -1)));
    v[y].pb(mp(x, mp(-1, -1)));
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld\\n", dfs(i, 0).x);
  }
}