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

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
#define N 300000

const LL mod = 998244353;
LL n, f[N][3][3], g[3][3], d[N], m, mx, rt, fa[N];
vector<LL> e[N];

void dfs(LL u, LL dist) {
  if (dist > mx) {
    mx = dist;
    rt = u;
  }
  for (LL v : e[u])
    if (v != fa[u]) {
      fa[v] = u;
      dfs(v, dist + 1);
    }
}

LL inv(LL x) { return x == 1 ? 1 : (mod - mod / x) * inv(mod % x) % mod; }

void upd(LL &x, LL y) { x = (x + y) % mod; }

void dp(LL u, LL dist) {
  if (dist == m / 2)
    f[u][1][1] = 1;
  else
    f[u][0][0] = 1;
  for (LL v : e[u])
    if (v != fa[u]) {
      fa[v] = u;
      dp(v, dist + 1);
      memset(g, 0, sizeof g);
      for (LL i1 : {0, 1, 2})
        for (LL i2 : {0, 1, 2})
          for (LL j1 : {0, 1, 2})
            for (LL j2 : {0, 1, 2})
              for (LL k : {0, 1, 2}) {
                LL t1 = j1 * (k == 1), t2 = j2 * (k == 2);
                t1 = min(2LL, i1 + t1);
                t2 = min(2LL, i2 + t2);
                upd(g[t1][t2], f[u][i1][i2] * f[v][j1][j2]);
              }
      memcpy(f[u], g, sizeof f[u]);
    }
}

int main() {
  scanf("%lld", &n);
  for (LL i = 1; i < n; ++i) {
    LL x, y;
    scanf("%lld%lld", &x, &y);
    e[x].push_back(y);
    e[y].push_back(x);
  }
  dfs(1, 1);
  memset(fa, 0, sizeof fa);
  mx = 0;
  dfs(rt, 1);
  for (LL i = rt; i; i = fa[i])
    d[++m] = i;
  memset(fa, 0, sizeof fa);
  if (m & 1) {
    LL p = d[m / 2 + 1];
    dp(p, 0);
    printf("%lld\\n", f[p][1][1] * inv(2) % mod);
  } else {
    LL p1 = d[m / 2], p2 = d[m / 2 + 1];
    fa[p1] = p2;
    fa[p2] = p1;
    dp(p1, 1);
    dp(p2, 1);
    printf("%lld\\n", (f[p1][1][0] + f[p1][1][1] + f[p1][1][2]) *
                         (f[p2][1][0] + f[p2][1][1] + f[p2][1][2]) % mod);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
#define N 120000

const LL mod = 998244353;
LL n, f[N][2][2], g[2][2], d[N], m, mx, rt, fa[N];
vector<LL> e[N];

void dfs(LL u, LL dist) {
  if (dist > mx) {
    mx = dist;
    rt = u;
  }
  for (LL v : e[u])
    if (v != fa[u]) {
      fa[v] = u;
      dfs(v, dist + 1);
    }
}

LL inv(LL x) { return x == 1 ? 1 : (mod - mod / x) * inv(mod % x) % mod; }

void upd(LL &x, LL y) { x = (x + y) % mod; }

void dp(LL u, LL dist) {
  if (dist == m / 2)
    f[u][1][1] = 1;
  else
    f[u][0][0] = 1;
  for (LL v : e[u])
    if (v != fa[u]) {
      fa[v] = u;
      dp(v, dist + 1);
      memset(g, 0, sizeof g);
      for (LL i1 : {0, 1})
        for (LL i2 : {0, 1})
          for (LL j1 : {0, 1})
            for (LL j2 : {0, 1})
              for (LL k = 0; k <= 2; ++k) {
                LL t1 = j1 && k == 1, t2 = j2 && k == 2;
                if ((i1 && t1) || (i2 && t2))
                  continue;
                upd(g[i1 | t1][i2 | t2], f[u][i1][i2] * f[v][j1][j2]);
              }
      memcpy(f[u], g, sizeof f[u]);
    }
}

int main() {
  scanf("%lld", &n);
  for (LL i = 1; i < n; ++i) {
    LL x, y;
    scanf("%lld%lld", &x, &y);
    e[x].push_back(y);
    e[y].push_back(x);
  }
  dfs(1, 1);
  memset(fa, 0, sizeof fa);
  mx = 0;
  dfs(rt, 1);
  for (LL i = rt; i; i = fa[i])
    d[++m] = i;
  memset(fa, 0, sizeof fa);
  if (m & 1) {
    LL p = d[m / 2 + 1];
    dp(p, 0);
    printf("%lld\\n", f[p][1][1] * inv(2) % mod);
  } else {
    LL p1 = d[m / 2], p2 = d[m / 2 + 1];
    fa[p1] = p2;
    fa[p2] = p1;
    dp(p1, 1);
    dp(p2, 1);
    printf("%lld\\n", f[p1][1][0] * f[p2][1][0] % mod);
  }

  return 0;
}