Problem p03181 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb(x) emplace_back(x)
#define forn(i, N) for (int i = 0; i < N; i++)
#define rep(i, x, N) for (int i = x; i <= N; i++)
#define revrep(i, x, N) for (int i = x; i >= N; i--)
#define ar array

using namespace std;
using ll = long long int;
using ld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using vd = vector<double>;
using vvi = vector<vi>;
int ctoi(char c) { return c - '0'; }
int slen(string s) { return s.length(); }
int vsize(vi a) { return (int)a.size(); }
int in() {
  int x;
  cin >> x;
  return x;
}
void tf(bool ok) { cout << (ok ? "YES" : "NO") << endl; }

// https://dmoj.ca/problem/dpv

const int MAXN = 1e5 + 10;

vector<int> g[MAXN];
ll dp[MAXN], c[MAXN], ans[MAXN];
int N, M;

void dfs1(int e, int p) {
  dp[e] = 1;
  vi cdrn;
  for (int &u : g[e]) {
    if (u != p) {
      dfs1(u, e);
      dp[e] = (dp[e] * (dp[u] + 1)) % M;
      cdrn.pb(u);
    }
  }

  vll ps, ss;
  ll pref = 1, suf = 1;
  forn(i, vsize(cdrn)) {
    ps.pb(pref);
    pref = (pref * (dp[cdrn[i]] + 1)) % M;
  }
  revrep(i, vsize(cdrn) - 1, 0) {
    ss.pb(suf);
    suf = (suf * (dp[cdrn[i]] + 1)) % M;
  }
  reverse(all(ss));
  forn(i, vsize(cdrn)) { c[cdrn[i]] = (ps[i] * ss[i]) % M; }
}

void dfs2(int e, int p) {
  ans[e] = (dp[e] * c[e]) % M;
  for (int &u : g[e]) {
    if (u != p) {
      c[u] = (c[u] * c[e] + 1) % M;
      dfs2(u, e);
    }
  }
}

int main() {
  // freopen("input.txt","rt",stdin);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> N >> M;
  forn(i, N - 1) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }

  dfs1(1, -1);
  c[1] = 1;
  dfs2(1, -1);

  rep(i, 1, N) cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb(x) emplace_back(x)
#define forn(i, N) for (int i = 0; i < N; i++)
#define rep(i, x, N) for (int i = x; i <= N; i++)
#define revrep(i, x, N) for (int i = x; i >= N; i--)
#define ar array

using namespace std;
using ll = long long int;
using ld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using vd = vector<double>;
using vvi = vector<vi>;
int ctoi(char c) { return c - '0'; }
int slen(string s) { return s.length(); }
int vsize(vi a) { return (int)a.size(); }
int in() {
  int x;
  cin >> x;
  return x;
}
void tf(bool ok) { cout << (ok ? "YES" : "NO") << endl; }

// https://dmoj.ca/problem/dpv

const int MAXN = 1e5 + 10;

vector<int> g[MAXN];
ll dp[MAXN], c[MAXN], ans[MAXN];
int N, M;

void dfs1(int e, int p) {
  dp[e] = 1;
  vi cdrn;
  for (int &u : g[e]) {
    if (u != p) {
      dfs1(u, e);
      dp[e] = (dp[e] * (dp[u] + 1)) % M;
      cdrn.pb(u);
    }
  }

  vll ps, ss;
  ll pref = 1, suf = 1;
  forn(i, vsize(cdrn)) {
    ps.pb(pref);
    pref = (pref * (dp[cdrn[i]] + 1)) % M;
  }
  revrep(i, vsize(cdrn) - 1, 0) {
    ss.pb(suf);
    suf = (suf * (dp[cdrn[i]] + 1)) % M;
  }
  reverse(all(ss));
  forn(i, vsize(cdrn)) { c[cdrn[i]] = (ps[i] * ss[i]) % M; }
}

void dfs2(int e, int p) {
  ans[e] = (dp[e] * c[e]) % M;
  for (int &u : g[e]) {
    if (u != p) {
      c[u] = (c[u] * c[e] + 1) % M;
      dfs2(u, e);
    }
  }
}

int main() {

  cin >> N >> M;
  forn(i, N - 1) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }

  dfs1(1, -1);
  c[1] = 1;
  dfs2(1, -1);

  rep(i, 1, N) cout << ans[i] << endl;
  return 0;
}