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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

constexpr int P = 1000000007;

ll powmod(ll n, ll k) {
  ll r = 1, t = n % P;
  for (; k != 0; k /= 2) {
    if (k & 1)
      r = r * t % P;
    t = t * t % P;
  }
  return r;
}
ll inv(ll n) { return powmod(n, P - 2); }

vector<int> f0, f1;
void init(int n) {
  f0.resize(n + 1);
  f0[0] = 1;
  for (int i = 1; i <= n; i++) {
    f0[i] = (ll)f0[i - 1] * i % P;
  }
  f1.resize(n + 1);
  f1[n] = inv(f0[n]);
  for (int i = n; i > 0; i--) {
    f1[i - 1] = (ll)f1[i] * i % P;
  }
}
ll fact(int k) { return f0[k]; }
ll comb(int n, int k) {
  if (n < k || k < 0)
    return 0;
  return (ll)f0[n] * f1[k] % P * f1[n - k] % P;
}

ll invcomb(int n, int k) {
  if (n < k || k < 0)
    return 0;
  return (ll)f1[n] * f0[k] % P * f0[n - k] % P;
}

struct Graph {
  struct Vertex {
    int n;
    int r;
  };
  struct Edge {
    int i, n;
    int r;
    int k;
  };
  Graph(int n, int m) : v(n, {-1, 0}), e(m), n(n), m(0) {}
  void add_edge(int i, int j) {
    e[m] = {j, v[i].n, 0, 0};
    v[i].n = m;
    m++;
  }
  int dfs(int i, int p) {
    int k = 1;
    int *pk = nullptr;
    for (int j = v[i].n; j >= 0; j = e[j].n) {
      Edge &o = e[j];
      if (o.i == p) {
        pk = &o.k;
        continue;
      }
      k += o.k = dfs(o.i, i);
    }
    if (pk)
      *pk = n - k;
    return k;
  }
  int dfs0(int i, int p, int k) {
    ll r = 1;
    k--;
    for (int j = v[i].n; j >= 0; j = e[j].n) {
      Edge &o = e[j];
      if (o.i == p)
        continue;

      o.r = dfs0(o.i, i, o.k);
      r = r * o.r % P * comb(k, o.k) % P;
      k -= o.k;
    }
    return r;
  }
  void dfs1(int i, int p, ll r, ll r1) {
    for (int j = v[i].n; j >= 0; j = e[j].n) {
      Edge &o = e[j];
      if (o.i == p) {
        o.r = r1;
        r = r * o.r % P * comb(n - 1, o.k) % P;
        break;
      }
    }
    for (int j = v[i].n; j >= 0; j = e[j].n) {
      Edge &o = e[j];
      if (o.i == p)
        continue;

      int r0 = r * inv(o.r) % P * invcomb(n - 1, o.k) % P;
      dfs1(o.i, i, o.r, r0);
    }
    v[i].r = r;
  }
  void solve() {
    dfs(0, -1);
    int r = dfs0(0, -1, n);
    dfs1(0, -1, r, -1);

    for (int i = 0; i < n; i++) {
      cout << v[i].r << '\\n';
    }
  }
  vector<Vertex> v;
  vector<Edge> e;
  int n, m;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;

  init(n);

  Graph g(n, (n - 1) * 2);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    g.add_edge(a, b);
    g.add_edge(b, a);
  }

  g.solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

constexpr int P = 1000000007;

ll powmod(ll n, ll k) {
  ll r = 1, t = n % P;
  for (; k != 0; k /= 2) {
    if (k & 1)
      r = r * t % P;
    t = t * t % P;
  }
  return r;
}
ll inv(ll n) { return powmod(n, P - 2); }

vector<int> f0, f1;
void init(int n) {
  f0.resize(n + 1);
  f0[0] = 1;
  for (int i = 1; i <= n; i++) {
    f0[i] = (ll)f0[i - 1] * i % P;
  }
  f1.resize(n + 1);
  f1[n] = inv(f0[n]);
  for (int i = n; i > 0; i--) {
    f1[i - 1] = (ll)f1[i] * i % P;
  }
}
ll fact(int k) { return f0[k]; }
ll comb(int n, int k) {
  if (n < k || k < 0)
    return 0;
  return (ll)f0[n] * f1[k] % P * f1[n - k] % P;
}

struct Graph {
  struct Vertex {
    int n;
  };
  struct Edge {
    int i, n;
    ll r;
    int k;
  };
  Graph(int n, int m) : v(n, {-1}), e(m), n(n), m(0) {}
  void add_edge(int i, int j) {
    e[m] = {j, v[i].n, 0, 0};
    v[i].n = m;
    m++;
  }
  ll dfs(int i, int p, int &k0) {
    ll r0 = 1;
    k0 = 1;
    for (int j = v[i].n; j >= 0; j = e[j].n) {
      Edge &o = e[j];
      if (o.i == p)
        continue;

      if (o.k == 0) {
        o.r = dfs(o.i, i, o.k);
      }
      k0 += o.k;
    }
    int l = k0 - 1;
    for (int j = v[i].n; j >= 0; j = e[j].n) {
      Edge &o = e[j];
      if (o.i == p)
        continue;

      r0 = r0 * o.r % P * comb(l, o.k) % P;
      l -= o.k;
    }
    return r0;
  }
  void solve() {
    int k;
    for (int i = 0; i < n; i++) {
      ll r = dfs(i, -1, k);
      cout << r << '\\n';
    }
  }
  vector<Vertex> v;
  vector<Edge> e;
  int n, m;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;

  init(n);

  Graph g(n, (n - 1) * 2);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    g.add_edge(a, b);
    g.add_edge(b, a);
  }

  g.solve();

  return 0;
}