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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

vector<long long> fact(200001, 1);
void factorial(int N) {
  for (int i = 1; i < N; i++) {
    fact[i + 1] = (i + 1) * fact[i] % MOD;
  }
}

map<long long, long long> inv_fact;
long long inv_factorial(long long x) {
  if (inv_fact.find(x) != inv_fact.end()) {
    return inv_fact[x];
  }
  long long p = MOD - 2;
  long long ret = 1;
  long long tmp = x;
  while (p > 0) {
    if (p % 2 == 1)
      ret = ret * tmp % MOD;
    tmp = tmp * tmp % MOD;
    p /= 2;
  }
  inv_fact[x] = ret;
  return ret;
}

pair<int, int> make_key(int x, int p) { return make_pair(x, p); }
int cnt = 0;
ll dfs(int node, int parent, vector<vector<int>> &edge,
       map<pair<int, int>, ll> &dic, map<pair<int, int>, ll> &elm) {
  cnt++;
  pair<int, int> key = make_key(node, parent);

  if (dic.find(key) != dic.end()) {
    return dic[key];
  } else if (dic.find(make_key(node, -1)) != dic.end()) {
    pair<int, int> sub_key = make_key(node, -1);
    pair<int, int> inv_key = make_key(parent, node);
    dic[key] = ((((dic[sub_key] * inv_factorial(dic[inv_key])) % MOD *
                  inv_factorial(fact[elm[sub_key] - 1])) %
                 MOD * fact[elm[sub_key] - elm[inv_key] - 1]) %
                MOD * fact[elm[inv_key]]) %
               MOD;
    elm[key] = elm[sub_key] - elm[inv_key];
    // cout << node << ' ' << parent << ' ' <<  elm[sub_key] << ' ' <<
    // fact[elm[inv_key]] << endl;
    return dic[key];
  }

  dic[key] = 1;
  elm[key] = 1;
  for (auto x : edge[node]) {
    if (x == parent) {
      continue;
    }
    pair<int, int> sub_key = make_key(x, node);
    dic[key] *= dfs(x, node, edge, dic, elm);
    dic[key] %= MOD;
    dic[key] *= inv_factorial(fact[elm[sub_key]]);
    dic[key] %= MOD;
    elm[key] += elm[sub_key];
  }
  dic[key] *= fact[elm[key] - 1];
  dic[key] %= MOD;
  return dic[key];
}

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

  map<pair<int, int>, ll> dp;
  map<pair<int, int>, ll> elm;

  deque<int> que;
  que.push_back(0);

  vector<int> used(N, 0);
  int rr = 0;
  while ((int)que.size()) {
    rr++;
    int x = que.front();
    que.pop_front();
    used[x] = 1;
    dfs(x, -1, edge, dp, elm);
    for (auto y : edge[x]) {
      if (used[y] == 1)
        continue;
      que.push_back(y);
    }
  }

  for (int i = 0; i < N; i++) {
    ll ans = 1;
    cout << dp[make_key(i, -1)] << endl;
  }
  // cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

vector<long long> fact(200001, 1);
void factorial(int N) {
  for (int i = 1; i < N; i++) {
    fact[i + 1] = (i + 1) * fact[i] % MOD;
  }
}

map<long long, long long> inv_fact;
long long inv_factorial(long long x) {
  if (inv_fact.find(x) != inv_fact.end()) {
    return inv_fact[x];
  }
  long long p = MOD - 2;
  long long ret = 1;
  long long tmp = x;
  while (p > 0) {
    if (p % 2 == 1)
      ret = ret * tmp % MOD;
    tmp = tmp * tmp % MOD;
    p /= 2;
  }
  inv_fact[x] = ret;
  return ret;
}

string make_key(int x, int p) {
  string ret = to_string(p) + "," + to_string(x);
  return ret;
}
int cnt = 0;
ll dfs(int node, int parent, vector<vector<int>> &edge, map<string, ll> &dic,
       map<string, ll> &elm) {
  cnt++;
  string key = make_key(node, parent);

  if (dic.find(key) != dic.end()) {
    return dic[key];
  } else if (dic.find(make_key(node, -1)) != dic.end()) {
    string sub_key = make_key(node, -1);
    string inv_key = make_key(parent, node);
    dic[key] = ((((dic[sub_key] * inv_factorial(dic[inv_key])) % MOD *
                  inv_factorial(fact[elm[sub_key] - 1])) %
                 MOD * fact[elm[sub_key] - elm[inv_key] - 1]) %
                MOD * fact[elm[inv_key]]) %
               MOD;
    elm[key] = elm[sub_key] - elm[inv_key];
    // cout << node << ' ' << parent << ' ' <<  elm[sub_key] << ' ' <<
    // fact[elm[inv_key]] << endl;
    return dic[key];
  }

  dic[key] = 1;
  elm[key] = 1;
  for (auto x : edge[node]) {
    if (x == parent) {
      continue;
    }
    string sub_key = make_key(x, node);
    dic[key] *= dfs(x, node, edge, dic, elm);
    dic[key] %= MOD;
    dic[key] *= inv_factorial(fact[elm[sub_key]]);
    dic[key] %= MOD;
    elm[key] += elm[sub_key];
  }
  dic[key] *= fact[elm[key] - 1];
  dic[key] %= MOD;
  return dic[key];
}

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

  map<string, ll> dp;
  map<string, ll> elm;

  deque<int> que;
  que.push_back(0);

  vector<int> used(N, 0);
  while ((int)que.size()) {
    int x = que.front();
    que.pop_front();
    used[x] = 1;
    dfs(x, -1, edge, dp, elm);
    for (auto y : edge[x]) {
      if (used[y] == 1)
        continue;
      que.push_back(y);
    }
  }

  for (int i = 0; i < N; i++) {
    ll ans = 1;
    cout << dp[make_key(i, -1)] << endl;
  }
  // cout << cnt << endl;
}