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

#include <functional>
#include <iostream>
#include <vector>
using namespace std;

//  a^b % m
long long powmod(long long a, long long b, long long m) {
  long long r = 1;
  for (; b > 0; b >>= 1, a = a * a % m)
    if (b & 1)
      r = r * a % m;
  return r;
}

int main() {
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    E[a - 1].push_back(b - 1);
    E[b - 1].push_back(a - 1);
  }

  vector<vector<int>> V(N);
  vector<vector<long long>> P(N);
  for (int i = 0; i < N; i++) {
    V[i] = vector<int>(E[i].size() + 1, -1);
    P[i] = vector<long long>(E[i].size() + 1);
  }

  long long M = 1000000007LL;
  vector<long long> F(N + 1);
  F[0] = 1;
  for (int i = 1; i <= N; i++)
    F[i] = F[i - 1] * i % M;
  vector<long long> IF(N + 1);
  for (int i = 0; i <= N; i++)
    IF[i] = powmod(F[i], M - 2, M);

  function<void(int, int)> f1 = [&](int c, int p) {
    int pi;
    if (p == -1)
      pi = (int)E[c].size();
    else
      for (int i = 0; i < (int)E[c].size(); i++)
        if (E[c][i] == p)
          pi = i;
    if (V[c][pi] != -1)
      return;

    int v = 0;
    long long pp = 1;

    for (int e : E[c])
      if (e != p) {
        f1(e, c);
        int ei = 0;
        for (int i = 0; i < (int)E[e].size(); i++)
          if (E[e][i] == c)
            ei = i;
        v += V[e][ei];
        pp *= P[e][ei];
        pp %= M;
        pp *= IF[V[e][ei]];
        pp %= M;
      }
    V[c][pi] = v + 1;
    P[c][pi] = pp * F[v] % M;
  };
  f1(0, -1);

  function<void(int, int)> f2 = [&](int c, int p) {
    if (p != -1) {
      int pi = 0;
      for (int i = 0; i < (int)E[c].size(); i++)
        if (E[c][i] == p)
          pi = i;

      long long tp = P[p][P[p].size() - 1];

      tp *= IF[N - 1];
      tp %= M;
      tp *= F[V[c][pi]];
      tp %= M;
      tp *= F[N - V[c][pi] - 1];
      tp %= M;
      tp *= powmod(P[c][pi], M - 2, M);
      tp %= M;

      P[c][P[c].size() - 1] = P[c][pi] * IF[V[c][pi] - 1] % M * F[N - 1] % M *
                              IF[N - V[c][pi]] % M * tp % M;
    }

    for (int e : E[c])
      if (e != p)
        f2(e, c);
  };
  f2(0, -1);

  for (int i = 0; i < N; i++)
    cout << P[i][P[i].size() - 1] << endl;
}


Error: TLE

Fixed submission:

#include <functional>
#include <iostream>
#include <vector>
using namespace std;

//  a^b % m
long long powmod(long long a, long long b, long long m) {
  long long r = 1;
  for (; b > 0; b >>= 1, a = a * a % m)
    if (b & 1)
      r = r * a % m;
  return r;
}

int main() {
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    E[a - 1].push_back(b - 1);
    E[b - 1].push_back(a - 1);
  }

  vector<vector<int>> V(N);
  vector<vector<long long>> P(N);
  for (int i = 0; i < N; i++) {
    V[i] = vector<int>(E[i].size() + 1, -1);
    P[i] = vector<long long>(E[i].size() + 1);
  }

  long long M = 1000000007LL;
  vector<long long> F(N + 1);
  F[0] = 1;
  for (int i = 1; i <= N; i++)
    F[i] = F[i - 1] * i % M;
  vector<long long> IF(N + 1);
  for (int i = 0; i <= N; i++)
    IF[i] = powmod(F[i], M - 2, M);

  function<void(int, int)> f = [&](int c, int p) {
    int pi;
    if (p == -1)
      pi = (int)E[c].size();
    else
      for (int i = 0; i < (int)E[c].size(); i++)
        if (E[c][i] == p)
          pi = i;
    if (V[c][pi] != -1)
      return;

    int v = 0;
    long long pp = 1;

    for (int e : E[c])
      if (e != p) {
        f(e, c);
        int ei = 0;
        for (int i = 0; i < (int)E[e].size(); i++)
          if (E[e][i] == c)
            ei = i;
        v += V[e][ei];
        pp *= P[e][ei];
        pp %= M;
        pp *= IF[V[e][ei]];
        pp %= M;
      }
    pp *= F[v];
    pp %= M;
    v++;

    V[c][pi] = v;
    P[c][pi] = pp;
  };

  for (int i = 0; i < N; i++) {
    f(i, -1);
    cout << P[i][P[i].size() - 1] << endl;
  }
}