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

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
#define N 1200

const LL mod = 1000000007;
LL n, rt, rt1, rt2, q[N], tot, len, head[N], f[N][N][2], fa[N], dep[N];
bool vis[N];
struct edge {
  LL v, nxt;
} e[N << 1];

void add(LL x, LL y) {
  e[++tot].v = y;
  e[tot].nxt = head[x];
  head[x] = tot;
}

LL bfs(LL s) {
  memset(vis, 0, sizeof vis);
  LL tt = 0, ww = 1;
  q[1] = s;
  vis[s] = 1;
  while (tt < ww) {
    LL u = q[++tt];
    for (LL i = head[u], v; i; i = e[i].nxt)
      if (!vis[v = e[i].v]) {
        vis[v] = 1;
        q[++ww] = v;
      }
  }
  return q[n];
}

void dfs(LL u) {
  for (LL i = head[u], v; i; i = e[i].nxt)
    if ((v = e[i].v) != fa[u]) {
      fa[v] = u;
      dfs(v);
    }
}

void dp(LL u) {
  for (LL i = 0; i <= len - dep[u]; ++i)
    f[u][i][0] = f[u][i][1] = f[u][i + 1][1] = 1;
  for (LL i = head[u], v; i; i = e[i].nxt)
    if ((v = e[i].v) != fa[u]) {
      dep[v] = dep[u] + 1;
      fa[v] = u;
      dp(v);
      for (LL j = 0; j <= len; ++j) {
        f[u][j][0] = f[u][j][0] * (f[v][j][0] + (j ? f[v][j - 1][0] : 0)) % mod;
        f[u][j][1] = f[u][j][1] * (f[v][j][1] + (j ? f[v][j - 1][1] : 0)) % mod;
      }
    }
}

int main() {
  scanf("%lld", &n);
  for (LL i = 1; i < n; ++i) {
    LL x, y;
    scanf("%lld%lld", &x, &y);
    add(x, y);
    add(y, x);
  }
  LL S = bfs(1);
  LL T = bfs(S);
  dfs(S);
  len = 0;
  for (LL i = T; i != S; i = fa[i])
    ++len;
  if (len & 1) {
    len /= 2;
    rt1 = T;
    for (int i = 1; i <= len; ++i)
      rt1 = fa[rt1];
    rt2 = fa[rt1];
    memset(fa, 0, sizeof fa);
    fa[rt1] = rt2;
    fa[rt2] = rt1;
    dp(rt1);
    dp(rt2);
    LL ans = 0;
#define f1 f[rt1]
#define f2 f[rt2]
    for (LL i = 0; i <= len; ++i)
      ans = (ans + f1[i][0] * (f2[i][1] + f2[i + 1][1] - f2[i][0]) % mod +
             f2[i][0] * (f1[i][1] + f1[i + 1][1] - f1[i][0]) % mod -
             f1[i][0] * f2[i + 1][0] % mod - f1[i + 1][0] * f2[i][0] % mod +
             mod * 10) %
            mod;
    printf("%lld\\n", ans);
  } else {
    len /= 2;
    rt = T;
    for (int i = 1; i <= len; ++i)
      rt = fa[rt];
    memset(fa, 0, sizeof fa);
    dp(rt);
    LL ans = 0;
    for (LL i = 0; i <= len; ++i)
      ans = (ans + f[rt][i][0]) % mod;
    printf("%lld\\n", ans);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
#define N 1200

const LL mod = 1000000007;
LL n, rt, q[N], tot, len, head[N], f[N][N], fa[N], dep[N];
bool vis[N];
struct edge {
  LL v, nxt;
} e[N << 1];

void add(LL x, LL y) {
  e[++tot].v = y;
  e[tot].nxt = head[x];
  head[x] = tot;
}

LL bfs(LL s) {
  memset(vis, 0, sizeof vis);
  LL tt = 0, ww = 1;
  q[1] = s;
  vis[s] = 1;
  while (tt < ww) {
    LL u = q[++tt];
    for (LL i = head[u], v; i; i = e[i].nxt)
      if (!vis[v = e[i].v]) {
        vis[v] = 1;
        q[++ww] = v;
      }
  }
  return q[n];
}

void dfs(LL u) {
  for (LL i = head[u], v; i; i = e[i].nxt)
    if ((v = e[i].v) != fa[u]) {
      fa[v] = u;
      dfs(v);
    }
}

void dp(LL u) {
  dep[u] = dep[fa[u]] + 1;
  for (LL i = 0; i <= len + 1 - dep[u]; ++i)
    f[u][i] = 1;
  for (LL i = head[u], v; i; i = e[i].nxt)
    if ((v = e[i].v) != fa[u]) {
      fa[v] = u;
      dp(v);
      for (LL j = 0; j <= len; ++j) {
        f[u][j] = f[u][j] * (f[v][j] + (j ? f[v][j - 1] : 0)) % mod;
      }
    }
}

int main() {
  scanf("%lld", &n);
  for (LL i = 1; i < n; ++i) {
    LL x, y;
    scanf("%lld%lld", &x, &y);
    add(x, y);
    add(y, x);
  }
  LL S = bfs(1);
  LL T = bfs(S);
  dfs(S);
  len = 0;
  for (LL i = T; i != S; i = fa[i])
    ++len;
  if (len & 1) {
    assert(0);
  } else {
    len /= 2;
    rt = T;
    for (int i = 1; i <= len; ++i)
      rt = fa[rt];
    memset(fa, 0, sizeof fa);
    dp(rt);
    LL ans = 0;
    for (LL i = 0; i <= len; ++i)
      ans = (ans + f[rt][i]) % mod;
    printf("%lld\\n", ans);
  }

  return 0;
}