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

#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
#define mod 1000000007
using namespace std;
typedef long long ll;

ll rev(ll a) {
  ll now = mod - 2, ans = 1;
  while (now) {
    if (now & 1)
      ans *= a;
    now >>= 1, ans %= mod, a *= a, a %= mod;
  }
  return ans;
}

int main(void) {
  ll i, j, k, n, a, b, now, ans;
  scanf("%lld", &n);
  vector<ll> vec[n];
  for (i = 0; i < n - 1; ++i) {
    scanf("%lld%lld", &a, &b);
    vec[--a].push_back(--b);
    vec[b].push_back(a);
  }
  ll fact[n + 1], dp1[n], dp2[n], c1[n], c2[n], p[n];
  for (i = 0; i < n; ++i)
    p[i] = -1, c1[i] = c2[i] = 0;
  p[0] = 0;
  fact[0] = 1;
  for (i = 1; i <= n; ++i)
    fact[i] = fact[i - 1] * i, fact[i] %= mod;
  stack<ll> s;
  queue<ll> q;
  q.push(0);
  while (q.size()) {
    now = q.front(), q.pop();
    s.push(now);
    for (i = 0; i < vec[now].size(); ++i)
      if (p[vec[now][i]] == -1)
        p[vec[now][i]] = now, q.push(vec[now][i]);
  }
  while (s.size()) {
    now = s.top(), s.pop();
    dp1[now] = 1;
    for (i = 0; i < vec[now].size(); ++i)
      if (vec[now][i] != p[now]) {
        c1[now] += c1[vec[now][i]] + 1;
        dp1[now] *= rev(fact[c1[vec[now][i]] + 1]);
        dp1[now] %= mod;
        dp1[now] *= dp1[vec[now][i]];
        dp1[now] %= mod;
      }
    dp1[now] *= fact[c1[now]];
    dp1[now] %= mod;
  }
  q.push(0);
  while (q.size()) {
    now = q.front(), q.pop();
    for (i = 0; i < vec[now].size(); ++i)
      if (vec[now][i] != p[now])
        q.push(vec[now][i]);
    dp2[now] = 1;
    if (now) {
      c2[now] = n - c1[now] - 2;
      dp2[now] *= rev(fact[c2[p[now]]]);
      dp2[now] %= mod;
      dp2[now] *= dp2[p[now]];
      dp2[now] %= mod;
      dp2[now] *= dp1[p[now]];
      dp2[now] %= mod;
      dp2[now] *= rev(dp1[now]);
      dp2[now] %= mod;
      dp2[now] *= rev(fact[c1[p[now]]]);
      dp2[now] %= mod;
      dp2[now] *= fact[c1[now] + 1];
      dp2[now] %= mod;
      dp2[now] *= fact[c2[now]];
      dp2[now] %= mod;
      c2[now]++;
    }
  }
  for (i = 0; i < n; ++i) {
    ans = dp1[i] * dp2[i];
    ans %= mod;
    ans *= fact[n - 1];
    ans %= mod;
    ans *= rev(fact[c1[i]]);
    ans %= mod;
    ans *= rev(fact[c2[i]]);
    ans %= mod;
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
#define mod 1000000007
using namespace std;
typedef long long ll;

ll rev(ll a) {
  ll now = mod - 2, ans = 1;
  while (now) {
    if (now & 1)
      ans *= a;
    now >>= 1, ans %= mod, a *= a, a %= mod;
  }
  return ans;
}

int main(void) {
  ll i, j, k, n, a, b, now, ans;
  scanf("%lld", &n);
  vector<ll> vec[n];
  for (i = 0; i < n - 1; ++i) {
    scanf("%lld%lld", &a, &b);
    vec[--a].push_back(--b);
    vec[b].push_back(a);
  }
  ll fact[n + 1], dp1[n], dp2[n], c1[n], c2[n], p[n];
  for (i = 0; i < n; ++i)
    p[i] = -1, c1[i] = c2[i] = 0;
  p[0] = 0;
  fact[0] = 1;
  for (i = 1; i <= n; ++i)
    fact[i] = fact[i - 1] * i, fact[i] %= mod;
  stack<ll> s;
  queue<ll> q;
  q.push(0);
  while (q.size()) {
    now = q.front(), q.pop();
    s.push(now);
    for (i = 0; i < vec[now].size(); ++i)
      if (p[vec[now][i]] == -1)
        p[vec[now][i]] = now, q.push(vec[now][i]);
  }
  while (s.size()) {
    now = s.top(), s.pop();
    dp1[now] = 1;
    for (i = 0; i < vec[now].size(); ++i)
      if (vec[now][i] != p[now]) {
        c1[now] += c1[vec[now][i]] + 1;
        dp1[now] *= rev(fact[c1[vec[now][i]] + 1]);
        dp1[now] %= mod;
        dp1[now] *= dp1[vec[now][i]];
        dp1[now] %= mod;
      }
    dp1[now] *= fact[c1[now]];
    dp1[now] %= mod;
  }
  q.push(0);
  while (q.size()) {
    now = q.front(), q.pop();
    for (i = 0; i < vec[now].size(); ++i)
      if (vec[now][i] != p[now])
        q.push(vec[now][i]);
    dp2[now] = 1;
    if (now) {
      for (i = 0; i < vec[p[now]].size(); ++i)
        if (vec[p[now]][i] != now) {
          if (vec[p[now]][i] != p[p[now]]) {
            c2[now] += c1[vec[p[now]][i]] + 1;
            dp2[now] *= rev(fact[c1[vec[p[now]][i]] + 1]);
            dp2[now] %= mod;
            dp2[now] *= dp1[vec[p[now]][i]];
            dp2[now] %= mod;
          } else {
            c2[now] += c2[p[now]];
            dp2[now] *= rev(fact[c2[p[now]]]);
            dp2[now] %= mod;
            dp2[now] *= dp2[p[now]];
            dp2[now] %= mod;
          }
        }
      dp2[now] *= fact[c2[now]];
      dp2[now] %= mod;
      c2[now]++;
    }
  }
  for (i = 0; i < n; ++i) {
    ans = dp1[i] * dp2[i];
    ans %= mod;
    ans *= fact[n - 1];
    ans %= mod;
    ans *= rev(fact[c1[i]]);
    ans %= mod;
    ans *= rev(fact[c2[i]]);
    ans %= mod;
    printf("%lld\\n", ans);
  }
  return 0;
}