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

#include <bits/stdc++.h>
#include <stack>
using namespace std;
#define mod 1000000007
#define nmax 200005
vector<vector<int>> graph(nmax);
vector<int> ver(nmax);
vector<long long> dp1(nmax);
vector<vector<int>> child(nmax);
vector<int> pre(nmax);
long long fact[nmax];
map<pair<int, int>, pair<long long, int>> dp2;
int N;
long long inv(long long a);

void dfs1(int s, int N) {
  stack<int> st;
  vector<bool> visit(N + 1);
  for (int i = 0; i < N + 1; i++)
    visit[i] = false;
  st.push(s);
  visit[s] = true;
  int v, w;
  while (!st.empty()) {
    v = st.top();
    st.pop();
    for (int i = 0; i < graph[v].size(); i++) {
      w = graph[v][i];
      if (!visit[w]) {
        visit[w] = true;
        child[v].push_back(w);
        pre[w] = v;
        st.push(w);
      }
    }
  }
  return;
}
int dfs_ver(int s, int N) {
  if (ver[s] != 0)
    return ver[s];
  int v;
  long long sumver = 1;
  if (child[s].size() == 0) {
    ver[s] = 1;
    return 1;
  } else
    ;
  {
    for (int i = 0; i < child[s].size(); i++) {
      v = child[s][i];
      if (ver[v] != 0)
        sumver = (sumver + ver[v]) % mod;
      else {
        sumver = (sumver + dfs_ver(v, N)) % mod;
      }
    }
    ver[s] = sumver;
    return sumver;
  }
}

int dfs_dp1(int s, int N) {
  if (dp1[s] != 0)
    return dp1[s];
  int v;
  long long sumdp1 = 1;
  if (child[s].size() == 0) {
    dp1[s] = 1;
    return 1;
  } else
    ;
  {
    for (int i = 0; i < child[s].size(); i++) {
      v = child[s][i];
      if (dp1[v] != 0)
        sumdp1 = sumdp1 * dp1[v] % mod * inv(fact[ver[v]]) % mod;
      else {
        sumdp1 = (sumdp1 * dfs_dp1(v, N) % mod * inv(fact[ver[v]])) % mod;
      }
    }
    dp1[s] = fact[ver[s] - 1] * sumdp1 % mod;
    return dp1[s];
  }
}

void fraction() {
  fact[0] = 1;
  for (int i = 1; i < nmax; i++)
    fact[i] = i * fact[i - 1] % mod;
  return;
}

long long power(long long a, int m) {
  long long r = 1;
  int n;
  for (int i = 0; m >> i > 0; i++) {
    n = m >> i;
    if (n & 1)
      r = r * a % mod;
    a = a * a % mod;
  }
  return r;
}
long long inv(long long a) { return power(a, mod - 2); }

void dfs_dp2(int s, int N) {
  int v, w, ww;
  long long root = 1, ver = 0, root2 = 1, ver2 = 0;
  pair<long long, int> pa;
  stack<int> st;
  st.push(s);
  vector<bool> visit(N + 1);
  while (!st.empty()) {
    v = st.top();
    st.pop();
    root = 1;
    ver = 0;
    for (int i = 0; i < graph[v].size(); i++) {
      w = graph[v][i];
      root = (root * dp2[make_pair(v, w)].first) % mod *
             inv(fact[dp2[make_pair(v, w)].second]) % mod;
      ver = (ver + dp2[make_pair(v, w)].second) % mod;
    }
    for (int i = 0; i < graph[v].size(); i++) {
      ww = graph[v][i];
      root2 = root * fact[dp2[make_pair(v, ww)].second] % mod *
              inv(dp2[make_pair(v, ww)].first) % mod;
      ver2 = ver - dp2[make_pair(v, ww)].second;
      if ((dp2[make_pair(ww, v)].first == 0) &
          (dp2[make_pair(ww, v)].second == 0)) {
        pa = make_pair(root2 * fact[ver2] % mod, ver2 + 1);
        dp2[make_pair(ww, v)] = pa;
        st.push(ww);
      }
    }
  }
  return;
}

int main() {
  cin >> N;
  int a, b, v;
  fraction();
  pair<int, int> st = make_pair(0, 0);
  for (int i = 0; i < N + 1; i++) {
    ver[i] = 0;
    dp1[i] = 0;
  }
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
    dp2[make_pair(a, b)] = st;
    dp2[make_pair(b, a)] = st;
  }
  dfs1(1, N);
  dfs_ver(1, N);
  dfs_dp1(1, N);
  for (int i = 2; i < N + 1; i++) {
    v = pre[i];
    dp2[make_pair(v, i)] = make_pair(dp1[i], ver[i]);
  }
  dfs_dp2(1, N);

  long long r = 1, versum = 0;
  for (int i = 1; i < N + 1; i++) {
    r = 1;
    versum = 0;
    for (int j = 0; j < graph[i].size(); j++) {
      v = graph[i][j];
      r = r * dp2[make_pair(i, v)].first % mod *
          inv(fact[dp2[make_pair(i, v)].second]) % mod;
      versum = (versum + dp2[make_pair(i, v)].second) % mod;
    }
    cout << fact[versum] * r % mod << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <stack>
using namespace std;
#define mod 1000000007
#define nmax 200001
vector<vector<int>> graph(nmax);
vector<int> ver(nmax);
vector<long long> dp1(nmax);
vector<vector<int>> child(nmax);
vector<int> pre(nmax);
long long fact[nmax];
map<pair<int, int>, pair<long long, int>> dp2;
int N;
long long inv(long long a);

void dfs1(int s, int N) {
  stack<int> st;
  vector<bool> visit(N + 1);
  for (int i = 0; i < N + 1; i++)
    visit[i] = false;
  st.push(s);
  visit[s] = true;
  int v, w;
  while (!st.empty()) {
    v = st.top();
    st.pop();
    for (int i = 0; i < graph[v].size(); i++) {
      w = graph[v][i];
      if (!visit[w]) {
        visit[w] = true;
        child[v].push_back(w);
        pre[w] = v;
        st.push(w);
      }
    }
  }
  return;
}
int dfs_ver(int s, int N) {
  if (ver[s] != 0)
    return ver[s];
  int v;
  long long sumver = 1;
  if (child[s].size() == 0) {
    ver[s] = 1;
    return 1;
  } else
    ;
  {
    for (int i = 0; i < child[s].size(); i++) {
      v = child[s][i];
      if (ver[v] != 0)
        sumver = (sumver + ver[v]) % mod;
      else {
        sumver = (sumver + dfs_ver(v, N)) % mod;
      }
    }
    ver[s] = sumver;
    return sumver;
  }
}

int dfs_dp1(int s, int N) {
  if (dp1[s] != 0)
    return dp1[s];
  int v;
  long long sumdp1 = 1;
  if (child[s].size() == 0) {
    dp1[s] = 1;
    return 1;
  } else
    ;
  {
    for (int i = 0; i < child[s].size(); i++) {
      v = child[s][i];
      if (dp1[v] != 0)
        sumdp1 = sumdp1 * dp1[v] % mod * inv(fact[ver[v]]) % mod;
      else {
        sumdp1 = (sumdp1 * dfs_dp1(v, N) % mod * inv(fact[ver[v]])) % mod;
      }
    }
    dp1[s] = fact[ver[s] - 1] * sumdp1 % mod;
    return dp1[s];
  }
}

void fraction() {
  fact[0] = 1;
  for (int i = 1; i < nmax; i++)
    fact[i] = i * fact[i - 1] % mod;
  return;
}

long long power(long long a, int m) {
  long long r = 1;
  int n;
  for (int i = 0; m >> i > 0; i++) {
    n = m >> i;
    if (n & 1)
      r = r * a % mod;
    a = a * a % mod;
  }
  return r;
}
long long inv(long long a) { return power(a, mod - 2); }

void dfs_dp2(int s, int N) {
  int v, w, ww;
  long long root = 1, ver = 0;
  pair<long long, int> pa;
  stack<int> st;
  st.push(s);
  vector<bool> visit(N + 1);
  for (int i = 0; i < N + 1; i++)
    visit[i] = false;
  while (!st.empty()) {
    v = st.top();
    st.pop();
    for (int i = 0; i < child[v].size(); i++) {
      root = 1;
      ver = 0;
      w = child[v][i];
      for (int j = 0; j < graph[v].size(); j++) {
        ww = graph[v][j];
        if (w != ww) {
          root = (root * dp2[make_pair(v, ww)].first) % mod *
                 inv(fact[dp2[make_pair(v, ww)].second]) % mod;
          ver = (ver + dp2[make_pair(v, ww)].second) % mod;
        }
      }
      pa = make_pair(root * fact[ver] % mod, ver + 1);
      dp2[make_pair(w, v)] = pa;
      st.push(w);
    }
  }
  return;
}

int main() {
  cin >> N;
  int a, b, v;
  fraction();
  pair<int, int> st = make_pair(0, 0);
  for (int i = 0; i < N + 1; i++) {
    ver[i] = 0;
    dp1[i] = 0;
  }
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
    dp2[make_pair(a, b)] = st;
    dp2[make_pair(b, a)] = st;
  }
  dfs1(1, N);
  dfs_ver(1, N);
  dfs_dp1(1, N);
  for (int i = 2; i < N + 1; i++) {
    v = pre[i];
    dp2[make_pair(v, i)] = make_pair(dp1[i], ver[i]);
  }
  dfs_dp2(1, N);

  long long r = 1, versum = 0;
  for (int i = 1; i < N + 1; i++) {
    r = 1;
    versum = 0;
    for (int j = 0; j < graph[i].size(); j++) {
      v = graph[i][j];
      r = r * dp2[make_pair(i, v)].first % mod *
          inv(fact[dp2[make_pair(i, v)].second]) % mod;
      versum = (versum + dp2[make_pair(i, v)].second) % mod;
      // cout <<dp2[make_pair(i,v)].first <<" "<<dp2[make_pair(i,v)].second  <<
      // " " << i<<" "<< v << endl;
    }
    cout << fact[versum] * r % mod << endl;
  }
  return 0;
}