Problem p03794 (C++) — buggy submission, verdict: Runtime Error

// In the Name of God
// Ya Ali

#include <bits/stdc++.h>

#define int long long

#define pb push_back

#define err(A) cout << #A << " = " << (A) << endl

using namespace std;

const int M = 1e9 + 7 + 0;
const int maxn = 1024;

int n;

vector<int> g[maxn];

int lvl[maxn];

int d0[maxn];
int d1[maxn];

int D, D1, D2;
int far;

int r0, r1;

int dp[maxn][maxn / 2];
int cp[maxn][maxn / 2];

int ans;

void pfs(int v, int par = n) {
  lvl[v] = lvl[par] + 1;
  for (int u : g[v])
    if (u != par)
      pfs(u, v);
  if (lvl[v] > lvl[far])
    far = v;
}

void dfs(int v, int par = n) {
  lvl[v] = lvl[par] + 1;
  for (int i = 0; i <= D2; i++) {
    if (D1 - lvl[v] >= i)
      dp[v][i] = 1;
    if (D1 - lvl[v] >= i - 1)
      cp[v][i] = 1;
  }
  for (int u : g[v])
    if (u != par) {
      dfs(u, v);
      for (int i = 0; i <= D2; i++) {
        (dp[v][i] *= dp[u][i] + (i ? dp[u][i - 1] : 0)) %= M;
        (cp[v][i] *= cp[u][i] + (i ? cp[u][i - 1] : 0)) %= M;
      }
    }
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int v, u, i = 0; i < n - 1; i++) {
    cin >> v >> u;
    v--, u--;
    g[v].pb(u);
    g[u].pb(v);
  }

  lvl[n] = -1;
  pfs(far);
  pfs(far);
  for (int i = 0; i < n; i++)
    d0[i] = lvl[i];
  pfs(far);
  for (int i = 0; i < n; i++)
    d1[i] = lvl[i];

  D = lvl[far];
  D1 = (D + 0) / 2;
  D2 = (D + 1) / 2;

  if (D & 1) {
    for (int i = 0; i < n; i++)
      if (d0[i] + d1[i] == D)
        if (d0[i] == d1[i] - 1)
          r0 = i;
        else if (d0[i] == d1[i] + 1)
          r1 = i;
    lvl[r1] = -1;
    dfs(r0, r1);
    lvl[r0] = -1;
    dfs(r1, r0);
    for (int i = 0; i <= D2; i++) {
      ans += dp[r0][i] * cp[r1][i] % M;
      ans += cp[r0][i + 1] * dp[r1][i] % M;
      ans -= dp[r0][i] * dp[r1][i] % M;
      ans -= dp[r0][i + 1] * dp[r1][i] % M;
      ans += dp[r1][i] * cp[r0][i] % M;
      ans += cp[r1][i + 1] * dp[r0][i] % M;
      ans -= dp[r1][i] * dp[r0][i] % M;
      ans -= dp[r1][i + 1] * dp[r0][i] % M;
      ans %= M;
    }
  } else {
    for (int i = 0; i < n; i++)
      if (d0[i] + d1[i] == D and d0[i] == d1[i])
        r0 = i;
    dfs(r0);
    for (int i = 0; i <= D2; i++)
      ans += dp[r0][i];
  }

  ans = (ans % M + M) % M;

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

// In the Name of God
// Ya Ali

#include <bits/stdc++.h>

#define int long long

#define pb push_back

#define err(A) cout << #A << " = " << (A) << endl

using namespace std;

const int M = 1e9 + 7 + 0;
const int maxn = 1024;

int n;

vector<int> g[maxn];

int lvl[maxn];

int d0[maxn];
int d1[maxn];

int D, D1, D2;
int far;

int r0, r1;

int dp[maxn][maxn / 2];
int cp[maxn][maxn / 2];

int ans;

void pfs(int v, int par = n) {
  lvl[v] = lvl[par] + 1;
  for (int u : g[v])
    if (u != par)
      pfs(u, v);
  if (lvl[v] > lvl[far])
    far = v;
}

void dfs(int v, int par = n) {
  lvl[v] = lvl[par] + 1;
  for (int i = 0; i <= D2; i++) {
    if (D1 - lvl[v] >= i)
      dp[v][i] = 1;
    if (D1 - lvl[v] >= i - 1)
      cp[v][i] = 1;
  }
  for (int u : g[v])
    if (u != par) {
      dfs(u, v);
      for (int i = 0; i <= D2; i++) {
        (dp[v][i] *= dp[u][i] + (i ? dp[u][i - 1] : 0)) %= M;
        (cp[v][i] *= cp[u][i] + (i ? cp[u][i - 1] : 0)) %= M;
      }
    }
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int v, u, i = 0; i < n - 1; i++) {
    cin >> v >> u;
    v--, u--;
    g[v].pb(u);
    g[u].pb(v);
  }

  lvl[n] = -1;
  pfs(far);
  pfs(far);
  for (int i = 0; i < n; i++)
    d0[i] = lvl[i];
  pfs(far);
  for (int i = 0; i < n; i++)
    d1[i] = lvl[i];

  D = lvl[far];
  D1 = (D + 0) / 2;
  D2 = (D + 1) / 2;

  if (D & 1) {
    for (int i = 0; i < n; i++)
      if (d0[i] + d1[i] == D)
        if (d0[i] == d1[i] - 1)
          r0 = i;
        else if (d0[i] == d1[i] + 1)
          r1 = i;
    lvl[r1] = -1;
    dfs(r0, r1);
    lvl[r0] = -1;
    dfs(r1, r0);
    for (int i = 0; i <= D2; i++) {
      ans += dp[r0][i] * cp[r1][i] % M;
      ans += cp[r0][i + 1] * dp[r1][i] % M;
      ans -= dp[r0][i] * dp[r1][i] % M;
      ans += dp[r1][i] * cp[r0][i] % M;
      ans += cp[r1][i + 1] * dp[r0][i] % M;
      ans -= dp[r1][i] * dp[r0][i] % M;
      ans -= dp[r1][i] * dp[r0][i] % M;
      ans %= M;
      return 1;
    }
  } else {
    for (int i = 0; i < n; i++)
      if (d0[i] + d1[i] == D and d0[i] == d1[i])
        r0 = i;
    dfs(r0);
    for (int i = 0; i <= D2; i++)
      ans += dp[r0][i];
  }

  ans = (ans % M + M) % M;

  cout << ans << endl;

  return 0;
}