Problem p03181 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RFOR(i, c)                                                             \
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define eb emplace_back
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define __builtin_popcount __builtin_popcountll

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "[";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "]";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &t) {
  os << "{";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &t) {
  os << "{";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first - t.first, s.second - t.second);
}

const int INF = 1 << 28;
const double EPS = 1e-8;
int MOD;
struct handler {
  typedef ll val_t;
  typedef ll opr_t;
  handler() {}
  val_t def_val() { return 1; }
  static val_t update(const val_t &l, const opr_t &r) { return l * r % MOD; }
  static val_t merge(const val_t &l, const val_t &r) { return l * r % MOD; }
};

template <typename Handler> struct SegTree {
  typedef typename Handler::val_t val_t;
  typedef typename Handler::opr_t opr_t;
  vector<val_t> val;
  Handler hdl;
  int n;

  SegTree(int size) : hdl() {
    n = 1;
    while (n < size)
      n <<= 1;
    val = vector<val_t>(2 * n, hdl.def_val());
  }
  SegTree(const vector<val_t> &in) : hdl() {
    n = 1;
    while (n < in.size())
      n <<= 1;
    val = vector<val_t>(2 * n, hdl.def_val());
    for (int i = n - 1 + in.size() - 1; i >= 0; i--) {
      if (n - 1 <= i)
        val[i] = in[i - (n - 1)];
      else
        val[i] = hdl.merge(val[i * 2 + 1], val[i * 2 + 2]);
    }
  }
  void update(int i, const opr_t &a) {
    i += n - 1;
    val[i] = hdl.update(val[i], a);
    while (i > 0) {
      i = (i - 1) / 2;
      val[i] = hdl.merge(val[i * 2 + 1], val[i * 2 + 2]);
    }
  }
  val_t query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return hdl.def_val();
    if (a <= l && r <= b)
      return val[k];
    return hdl.merge(query(a, b, k * 2 + 1, l, (l + r) / 2),
                     query(a, b, k * 2 + 2, (l + r) / 2, r));
  }
  val_t query(int a, int b) { return query(a, b, 0, 0, n); }
  val_t operator[](size_t i) { return query(i, i + 1); }
  friend ostream &operator<<(ostream &os, SegTree<Handler> &t) {
    REP(i, t.n) os << (i ? ", " : "[") << t.query(i, i + 1);
    return os << "]";
  }
};

int T, n, m;
vector<vi> g;
vi ans;
map<int, int> memo[100001];

ll dfs(int u, int p) {
  if (memo[u].count(p))
    return memo[u][p];
  ll ans = 1;
  vector<ll> r;
  for (int v : g[u]) {
    if (v != p) {
      ll res = dfs(v, u) + 1;
      ans = ans * res % MOD;
      r.push_back(res);
    }
  }
  if (p == -1) {
    SegTree<handler> seg(r);
    REP(i, g[u].size()) {
      memo[u][g[u][i]] = seg.query(0, i) * seg.query(i + 1, r.size()) % MOD;
    }
  }
  return memo[u][p] = ans;
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  while (cin >> n >> MOD) {
    g = vector<vi>(n);
    ans = vi(n);
    REP(i, n - 1) {
      int u, v;
      cin >> u >> v;
      -u--;
      v--;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    REP(i, n) { cout << dfs(i, -1) << endl; }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RFOR(i, c)                                                             \
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define eb emplace_back
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define __builtin_popcount __builtin_popcountll

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "[";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "]";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &t) {
  os << "{";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &t) {
  os << "{";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first - t.first, s.second - t.second);
}

const int INF = 1 << 28;
const double EPS = 1e-8;
int MOD;
struct handler {
  typedef ll val_t;
  typedef ll opr_t;
  handler() {}
  val_t def_val() { return 1; }
  static val_t update(const val_t &l, const opr_t &r) { return l * r % MOD; }
  static val_t merge(const val_t &l, const val_t &r) { return l * r % MOD; }
};

template <typename Handler> struct SegTree {
  typedef typename Handler::val_t val_t;
  typedef typename Handler::opr_t opr_t;
  vector<val_t> val;
  Handler hdl;
  int n;

  SegTree(int size) : hdl() {
    n = 1;
    while (n < size)
      n <<= 1;
    val = vector<val_t>(2 * n, hdl.def_val());
  }
  SegTree(const vector<val_t> &in) : hdl() {
    n = 1;
    while (n < in.size())
      n <<= 1;
    val = vector<val_t>(2 * n, hdl.def_val());
    for (int i = n - 1 + in.size() - 1; i >= 0; i--) {
      if (n - 1 <= i)
        val[i] = in[i - (n - 1)];
      else
        val[i] = hdl.merge(val[i * 2 + 1], val[i * 2 + 2]);
    }
  }
  void update(int i, const opr_t &a) {
    i += n - 1;
    val[i] = hdl.update(val[i], a);
    while (i > 0) {
      i = (i - 1) / 2;
      val[i] = hdl.merge(val[i * 2 + 1], val[i * 2 + 2]);
    }
  }
  val_t query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return hdl.def_val();
    if (a <= l && r <= b)
      return val[k];
    return hdl.merge(query(a, b, k * 2 + 1, l, (l + r) / 2),
                     query(a, b, k * 2 + 2, (l + r) / 2, r));
  }
  val_t query(int a, int b) { return query(a, b, 0, 0, n); }
  val_t operator[](size_t i) { return query(i, i + 1); }
  friend ostream &operator<<(ostream &os, SegTree<Handler> &t) {
    REP(i, t.n) os << (i ? ", " : "[") << t.query(i, i + 1);
    return os << "]";
  }
};

int T, n, m;
vector<vi> g;
vi ans;
map<int, int> memo[100001];

ll dfs(int u, int p) {
  if (memo[u].count(p))
    return memo[u][p];
  ll ans = 1;
  vector<ll> r;
  for (int v : g[u]) {
    if (v != p) {
      ll res = dfs(v, u) + 1;
      ans = ans * res % MOD;
      r.push_back(res);
    }
  }
  if (p == -1) {
    SegTree<handler> seg(r);
    REP(i, g[u].size()) {
      memo[u][g[u][i]] = seg.query(0, i) * seg.query(i + 1, r.size()) % MOD;
    }
  }
  return memo[u][p] = ans;
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  while (cin >> n >> MOD) {
    g = vector<vi>(n);
    ans = vi(n);
    REP(i, n - 1) {
      int u, v;
      cin >> u >> v;
      -u--;
      v--;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    vi d(n);
    REP(i, n) d[i] = i;
    sort(RALL(d), [&](int i, int j) { return g[i].size() < g[j].size(); });
    REP(i, n) { dfs(d[i], -1); }
    REP(i, n) { cout << dfs(i, -1) << endl; }
  }
  return 0;
}