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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;

long long inv(int a, int p) {
  return a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p;
}
static const int MODVAL = 1000000007;
struct mint {
  int val;
  mint() : val(0) {}
  mint(int x) : val(x % MODVAL) {
    if (val < 0)
      val += MODVAL;
  }
  mint(size_t x) : val(x % MODVAL) {}
  mint(long long x) : val(x % MODVAL) {
    if (val < 0)
      val += MODVAL;
  }
  mint &operator+=(mint y) {
    val += y.val;
    if (val >= MODVAL)
      val -= MODVAL;
    return *this;
  }
  mint &operator-=(mint y) {
    val -= y.val;
    if (val < 0)
      val += MODVAL;
    return *this;
  }
  mint &operator*=(mint y) {
    val = (val * (long long)y.val) % MODVAL;
    return *this;
  }
  mint &operator/=(mint y) {
    val = (val * inv(y.val, MODVAL)) % MODVAL;
    return *this;
  }
};
inline mint operator+(mint x, mint y) { return x += y; }
inline mint operator-(mint x, mint y) { return x -= y; }
inline mint operator*(mint x, mint y) { return x *= y; }
inline mint operator/(mint x, mint y) { return x /= y; }
mint POW(mint x, long long n) {
  mint r(1);
  for (; n; x *= x, n >>= 1)
    if (n & 1)
      r *= x;
  return r;
}
mint FAC(int n) {
  static vector<mint> FAC_(1, 1);
  while (int(FAC_.size()) <= n)
    FAC_.push_back(FAC_.back() * FAC_.size());
  return FAC_[n];
}
inline mint CMB(int n, int k) {
  return k < 0 || n < k ? 0 : FAC(n) / (FAC(k) * FAC(n - k));
}
inline ostream &operator<<(ostream &os, mint a) { return os << a.val; }

int n;

vector<int> g[200000 + 5];
struct K {
  mint v;
  int count;
};

map<ll, K> memo;
K solve(int p, int u) {
  ll key = (ll(p) << 32LL) | u;
  if (memo.count(key) == 0) {
    K res;
    res.v = 1;
    res.count = 1;
    vector<int> counts;
    for (int v : g[u]) {
      if (v == p) {
        continue;
      }
      K cur = solve(u, v);
      res.v *= cur.v;
      counts.push_back(cur.count);
      res.count += cur.count;
    }
    int cc = res.count - 1;
    for (int c : counts) {
      res.v *= CMB(cc, c);
      cc -= c;
    }
    memo[key] = res;
  }
  return memo[key];
}

mint ans[200000 + 10];
void propagate(int p, int u, mint pval, int pcount) {
  mint res = 1;
  vector<int> counts;
  for (int v : g[u]) {
    if (v == p) {
      res *= pval;
      counts.push_back(pcount);
    } else {
      K cur = solve(u, v);
      res *= cur.v;
      counts.push_back(cur.count);
    }
  }
  int cc = n - 1;
  for (int c : counts) {
    res *= CMB(cc, c);
    cc -= c;
  }
  ans[u] = res;
  for (int v : g[u]) {
    if (v == p) {
      continue;
    }
    K cur = solve(u, v);
    propagate(u, v, res / cur.v / CMB(n - 1, cur.count), n - cur.count);
  }
}

int main(void) {
  scanf("%d", &n);
  REP(i, n - 1) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  solve(1000000000, 0);
  propagate(1000000000, 0, 1, 0);

  REP(u, n) { printf("%d\\n", ans[u].val); }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;

long long inv(int a, int p) {
  return a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p;
}
static const int MODVAL = 1000000007;
struct mint {
  int val;
  mint() : val(0) {}
  mint(int x) : val(x % MODVAL) {
    if (val < 0)
      val += MODVAL;
  }
  mint(size_t x) : val(x % MODVAL) {}
  mint(long long x) : val(x % MODVAL) {
    if (val < 0)
      val += MODVAL;
  }
  mint &operator+=(mint y) {
    val += y.val;
    if (val >= MODVAL)
      val -= MODVAL;
    return *this;
  }
  mint &operator-=(mint y) {
    val -= y.val;
    if (val < 0)
      val += MODVAL;
    return *this;
  }
  mint &operator*=(mint y) {
    val = (val * (long long)y.val) % MODVAL;
    return *this;
  }
  mint &operator/=(mint y) {
    val = (val * inv(y.val, MODVAL)) % MODVAL;
    return *this;
  }
};
inline mint operator+(mint x, mint y) { return x += y; }
inline mint operator-(mint x, mint y) { return x -= y; }
inline mint operator*(mint x, mint y) { return x *= y; }
inline mint operator/(mint x, mint y) { return x /= y; }
mint POW(mint x, long long n) {
  mint r(1);
  for (; n; x *= x, n >>= 1)
    if (n & 1)
      r *= x;
  return r;
}
mint FAC(int n) {
  static vector<mint> FAC_(1, 1);
  while (int(FAC_.size()) <= n)
    FAC_.push_back(FAC_.back() * FAC_.size());
  return FAC_[n];
}
inline mint CMB(int n, int k) {
  return k < 0 || n < k ? 0 : FAC(n) / (FAC(k) * FAC(n - k));
}
inline ostream &operator<<(ostream &os, mint a) { return os << a.val; }

int n;

vector<int> g[200000 + 5];
struct K {
  mint v;
  int count;
};

map<ll, K> memo;
K solve(int p, int u) {
  ll key = (ll(p) << 32LL) | u;
  if (memo.count(key) == 0) {
    K res;
    res.v = 1;
    res.count = 1;
    vector<int> counts;
    for (int v : g[u]) {
      if (v == p) {
        continue;
      }
      K cur = solve(u, v);
      res.v *= cur.v;
      counts.push_back(cur.count);
      res.count += cur.count;
    }
    int cc = res.count - 1;
    for (int c : counts) {
      res.v *= CMB(cc, c);
      cc -= c;
    }
    memo[key] = res;
  }
  return memo[key];
}

mint ans[200000 + 10];
void propagate(int p, int u, mint pval, int pcount) {
  mint res = 1;
  vector<int> counts;
  for (int v : g[u]) {
    if (v == p) {
      res *= pval;
      counts.push_back(pcount);
    } else {
      K cur = solve(u, v);
      res *= cur.v;
      counts.push_back(cur.count);
    }
  }
  int cc = n - 1;
  for (int c : counts) {
    res *= CMB(cc, c);
    cc -= c;
  }
  ans[u] = res;
  for (int v : g[u]) {
    if (v == p) {
      continue;
    }
    K cur = solve(u, v);
    propagate(u, v, res / cur.v / CMB(n - 1, cur.count), n - cur.count);
  }
}

int main(void) {
  scanf("%d", &n);
  REP(i, n - 1) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  solve(1000000000, 0);
  propagate(1000000000, 0, 1, 0);

  REP(u, n) {
    K res;
    res.v = 1;
    res.count = 1;
    vector<int> counts;
    for (int v : g[u]) {
      K cur = solve(u, v);
      res.v *= cur.v;
      res.count += cur.count;
      counts.push_back(cur.count);
    }
    int cc = res.count - 1;
    for (int c : counts) {
      res.v *= CMB(cc, c);
      cc -= c;
    }
    printf("%d\\n", res.v.val);
  }
  return 0;
}