Problem p03181 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/dp/tasks/dp_v
// V - Subtree

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

#include <stdio.h>

using namespace std;

vector<vector<int>> adj;
vector<vector<int>> adj_count;
vector<long long> ans;
vector<bool> visited;

int M;

long long dfs1(int u) {
  // cout << "v1" << u << endl;
  visited[u] = true;
  long long a = 1;
  int c = 0;
  for (auto v : adj[u]) {
    // cout << "ds " << u << " " << v << endl;
    if (!visited[v]) {
      auto r = dfs1(v) + 1;
      a = (a * r) % M;
      adj_count[u][c] = r % M;
    }
    c++;
  }

  return a;
}

void dfs2(int u, long long father) {
  // cout << "v2 " << u << " " << father << endl;
  visited[u] = true;

  int le = adj[u].size();

  if (le == 0) {
    ans[u] = 1;
    return;
  }

  vector<long long> l(le, 1);
  vector<long long> r(le, 1);
  vector<long long> f(le, 1);

  for (auto i = 0; i < le; i++) {
    auto v = adj[u][i];
    if (visited[v])
      adj_count[u][i] = father + 1;
    // cout << "ac " << adj_count[u][i] << endl;

    if (i > 0)
      l[i] = (l[i - 1] * adj_count[u][i]) % M;
    else
      l[i] = adj_count[u][0] % M;
  }

  r[le - 1] = adj_count[u][le - 1];
  if (le > 1)
    f[le - 1] = l[le - 2];
  else
    f[le - 1] = 1;

  for (auto i = le - 2; i >= 0; i--) {
    auto v = adj[u][i];
    r[i] = (r[i + 1] * adj_count[u][i]) % M;
    if (i > 0)
      f[i] = (l[i - 1] * r[i + 1]) % M;
    else
      f[i] = r[i + 1] % M;
  }

  assert(r[0] == l[le - 1]);
  ans[u] = r[0];

  for (auto i = 0; i < le; i++) {
    // cout << "fi" << i << " " << f[i] << endl;
    auto v = adj[u][i];
    if (!visited[v])
      dfs2(v, f[i]);
  }
}

int main() {
  int N;
  cin >> N >> M;

  adj.assign(N, {});
  adj_count.assign(N, {});
  ans.assign(N, 0);
  visited.assign(N, false);

  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj_count[u].push_back(0);
    adj[v].push_back(u);
    adj_count[v].push_back(0);
  }

  dfs1(0);
  visited.assign(N, false);
  dfs2(0, 0);
  for (auto i = 0; i < N; i++)
    cout << ans[i] << endl;
  return 0;
}


Fixed submission:
// https://atcoder.jp/contests/dp/tasks/dp_v
// V - Subtree

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

#include <stdio.h>

using namespace std;

vector<vector<int>> adj;
vector<vector<int>> adj_count;
vector<long long> ans;
vector<bool> visited;

int M;

long long dfs1(int u) {
  // cout << "v1" << u << endl;
  visited[u] = true;
  long long a = 1;
  int c = 0;
  for (auto v : adj[u]) {
    // cout << "ds " << u << " " << v << endl;
    if (!visited[v]) {
      auto r = dfs1(v) + 1;
      a = (a * r) % M;
      adj_count[u][c] = r % M;
    }
    c++;
  }

  return a;
}

void dfs2(int u, long long father) {
  // cout << "v2 " << u << " " << father << endl;
  visited[u] = true;

  int le = adj[u].size();

  if (le == 0) {
    ans[u] = 1;
    return;
  }

  vector<long long> l(le, 1);
  vector<long long> r(le, 1);
  vector<long long> f(le, 1);

  for (auto i = 0; i < le; i++) {
    auto v = adj[u][i];
    if (visited[v])
      adj_count[u][i] = father + 1;
    // cout << "ac " << adj_count[u][i] << endl;

    if (i > 0)
      l[i] = (l[i - 1] * adj_count[u][i]) % M;
    else
      l[i] = adj_count[u][0] % M;
  }

  r[le - 1] = adj_count[u][le - 1] % M;
  if (le > 1)
    f[le - 1] = l[le - 2];
  else
    f[le - 1] = 1;

  for (auto i = le - 2; i >= 0; i--) {
    auto v = adj[u][i];
    r[i] = (r[i + 1] * adj_count[u][i]) % M;
    if (i > 0)
      f[i] = (l[i - 1] * r[i + 1]) % M;
    else
      f[i] = r[i + 1] % M;
  }

  assert(r[0] == l[le - 1]);
  ans[u] = r[0];

  for (auto i = 0; i < le; i++) {
    // cout << "fi" << i << " " << f[i] << endl;
    auto v = adj[u][i];
    if (!visited[v])
      dfs2(v, f[i]);
  }
}

int main() {
  int N;
  cin >> N >> M;

  adj.assign(N, {});
  adj_count.assign(N, {});
  ans.assign(N, 0);
  visited.assign(N, false);

  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj_count[u].push_back(0);
    adj[v].push_back(u);
    adj_count[v].push_back(0);
  }

  dfs1(0);
  visited.assign(N, false);
  dfs2(0, 0);
  for (auto i = 0; i < N; i++)
    cout << ans[i] << endl;
  return 0;
}