Problem p03794 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define ls (t << 1)
#define rs ((t << 1) + 1)
#define mid ((l + r) >> 1)
#define fi first
#define se second
#define mk make_pair
#define pb push_back

#define N 1005
#define M 200005
#define seed 23333
#define Mo 1000000007

using namespace std;
int i, j, m, n, p, k, f[N][N * 2], dis[N], fa[N], Q[N], x, y, dia[N], ans, D,
    col[N], Dis[N], lim[N];
vector<int> v[N];
void jia(int &x, int y) {
  x += y;
  if (x >= Mo)
    x -= Mo;
}
void jian(int &x, int y) {
  x -= y;
  if (x < 0)
    x += Mo;
}
int bfs(int *dis, int x) {
  memset(fa, 0, sizeof(fa));
  for (i = 1; i <= n; ++i)
    dis[i] = -1;
  dis[x] = 0;
  Q[Q[0] = 1] = x;
  int i, l;
  for (l = 1; l <= Q[0]; ++l) {
    int p = Q[l];
    for (i = 0; i < (int)v[p].size(); ++i) {
      int k = v[p][i];
      if (dis[k] != -1)
        continue;
      dis[k] = dis[p] + 1;
      fa[k] = p;
      Q[++Q[0]] = k;
    }
  }
  int now = 1;
  for (i = 1; i <= n; ++i)
    if (dis[now] < dis[i])
      now = i;
  return now;
}
void dfs(int x) {
  int i, j;
  for (i = -n + N; i <= n + N; ++i)
    if (abs(i - N) <= D / 2 - dis[x])
      f[x][i] = 1;
  for (i = 0; i < (int)v[x].size(); ++i) {
    int p = v[x][i];
    if (fa[x] == p)
      continue;
    dfs(p);
    for (j = -n + N; j <= n + N; ++j)
      f[x][j] = 1ll * f[x][j] * (f[p][j - 1] + f[p][j + 1]) % Mo;
  }
}
void Dfs(int x, int tp) {
  int i, j;
  for (i = -n + N; i <= n + N; ++i)
    if (col[x]) {
      if (abs(i - N) <= lim[x])
        f[x][i] = 1;
    } else if (abs(i - N - tp) <= lim[x])
      f[x][i] = 1;
  for (i = 0; i < (int)v[x].size(); ++i) {
    int p = v[x][i];
    if (fa[x] == p)
      continue;
    Dfs(p, tp);
    for (j = -n + N; j <= n + N; ++j)
      f[x][j] = 1ll * f[x][j] * (f[p][j - 1] + f[p][j + 1]) % Mo;
  }
}
int main() {
  scanf("%d", &n);
  for (i = 1; i < n; ++i) {
    scanf("%d%d", &x, &y);
    v[x].pb(y);
    v[y].pb(x);
  }
  int x = bfs(dis, 1), y = bfs(dis, x);
  for (; y; y = fa[y])
    dia[++dia[0]] = y;
  if (dia[0] & 1) {
    D = dia[0] - 1;
    bfs(dis, dia[D / 2 + 1]);
    dfs(dia[D / 2 + 1]);
    for (i = -n + N; i <= n + N; ++i)
      jia(ans, f[dia[D / 2 + 1]][i]);
    printf("%d\\n", ans);
  } else {
    D = dia[0] - 1;
    bfs(Dis, dia[D / 2 + 2]);
    bfs(dis, dia[D / 2 + 1]);
    for (i = 1; i <= n; ++i)
      if (dis[i] < Dis[i])
        col[i] = 1;
    for (i = 1; i <= n; ++i)
      if (col[i])
        lim[i] = D / 2 + 1 - dis[i];
      else
        lim[i] = D / 2 - Dis[i];
    Dfs(dia[D / 2 + 1], 0);
    for (i = -n + N; i <= n + N; ++i)
      jia(ans, f[dia[D / 2 + 1]][i]);
    for (i = 1; i <= n; ++i)
      if (col[i])
        lim[i] = D / 2 - dis[i];
      else
        lim[i] = D / 2 + 1 - Dis[i];
    memset(f, 0, sizeof(f));
    Dfs(dia[D / 2 + 1], 0);
    for (i = -n + N; i <= n + N; ++i)
      jia(ans, f[dia[D / 2 + 1]][i]);
    memset(f, 0, sizeof(f));
    Dfs(dia[D / 2 + 1], -1);
    for (i = -n + N; i <= n + N; ++i)
      jian(ans, f[dia[D / 2 + 1]][i]);
    memset(f, 0, sizeof(f));
    Dfs(dia[D / 2 + 1], 1);
    for (i = -n + N; i <= n + N; ++i)
      jian(ans, f[dia[D / 2 + 1]][i]);
    printf("%d\\n", ans);
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define ls (t << 1)
#define rs ((t << 1) + 1)
#define mid ((l + r) >> 1)
#define fi first
#define se second
#define mk make_pair
#define pb push_back

#define N 1005
#define M 200005
#define seed 23333
#define Mo 1000000007

using namespace std;
int i, j, m, n, p, k, f[N][N * 2], dis[N], fa[N], Q[N], x, y, dia[N], ans, D,
    col[N], Dis[N], lim[N];
vector<int> v[N];
void jia(int &x, int y) {
  x += y;
  if (x >= Mo)
    x -= Mo;
}
void jian(int &x, int y) {
  x -= y;
  if (x < 0)
    x += Mo;
}
int bfs(int *dis, int x) {
  memset(fa, 0, sizeof(fa));
  for (i = 1; i <= n; ++i)
    dis[i] = -1;
  dis[x] = 0;
  Q[Q[0] = 1] = x;
  int i, l;
  for (l = 1; l <= Q[0]; ++l) {
    int p = Q[l];
    for (i = 0; i < (int)v[p].size(); ++i) {
      int k = v[p][i];
      if (dis[k] != -1)
        continue;
      dis[k] = dis[p] + 1;
      fa[k] = p;
      Q[++Q[0]] = k;
    }
  }
  int now = 1;
  for (i = 1; i <= n; ++i)
    if (dis[now] < dis[i])
      now = i;
  return now;
}
void dfs(int x) {
  int i, j;
  for (i = -n + N; i <= n + N; ++i)
    if (abs(i - N) <= D / 2 - dis[x])
      f[x][i] = 1;
  for (i = 0; i < (int)v[x].size(); ++i) {
    int p = v[x][i];
    if (fa[x] == p)
      continue;
    dfs(p);
    for (j = -n + N; j <= n + N; ++j)
      f[x][j] = 1ll * f[x][j] * (f[p][j - 1] + f[p][j + 1]) % Mo;
  }
}
void Dfs(int x) {
  int i, j;
  for (i = -n + N; i <= n + N; ++i)
    if (abs(i - N) <= lim[x])
      f[x][i] = 1;
  for (i = 0; i < (int)v[x].size(); ++i) {
    int p = v[x][i];
    if (fa[x] == p)
      continue;
    Dfs(p);
    for (j = -n + N; j <= n + N; ++j)
      f[x][j] = 1ll * f[x][j] * (f[p][j - 1] + f[p][j + 1]) % Mo;
  }
}
int g[N][N * 2][8], G[N * 2][8];
void Del(int x) {
  int i, j, k, l;
  if (col[x] || lim[x] > 1) {
    for (i = -n + N; i <= n + N; ++i)
      if (abs(i - N) <= lim[x])
        g[x][i][0] = 1;
  } else {
    g[x][N][1] = 1;
    g[x][N - 1][2] = 1;
    g[x][N + 1][4] = 1;
  }
  for (i = 0; i < (int)v[x].size(); ++i) {
    int p = v[x][i];
    if (fa[x] == p)
      continue;
    Del(p);
    memcpy(G, g[x], sizeof(G));
    memset(g[x], 0, sizeof(g[x]));
    for (j = -n + N; j <= n + N; ++j)
      for (k = 0; k < 8; ++k)
        if (G[j][k])
          for (l = 0; l < 8; ++l)
            jia(g[x][j][k | l],
                1ll * G[j][k] * (g[p][j - 1][l] + g[p][j + 1][l]) % Mo);
  }
}
int main() {
  scanf("%d", &n);
  for (i = 1; i < n; ++i) {
    scanf("%d%d", &x, &y);
    v[x].pb(y);
    v[y].pb(x);
  }
  int x = bfs(dis, 1), y = bfs(dis, x);
  for (; y; y = fa[y])
    dia[++dia[0]] = y;
  if (dia[0] & 1) {
    D = dia[0] - 1;
    bfs(dis, dia[D / 2 + 1]);
    dfs(dia[D / 2 + 1]);
    for (i = -n + N; i <= n + N; ++i)
      jia(ans, f[dia[D / 2 + 1]][i]);
    printf("%d\\n", ans);
  } else {
    for (;;)
      ;
    D = dia[0] - 1;
    bfs(Dis, dia[D / 2 + 2]);
    bfs(dis, dia[D / 2 + 1]);
    for (i = 1; i <= n; ++i)
      if (dis[i] < Dis[i])
        col[i] = 1;
    for (i = 1; i <= n; ++i)
      if (col[i])
        lim[i] = D / 2 + 1 - dis[i];
      else
        lim[i] = D / 2 - Dis[i];
    Dfs(dia[D / 2 + 1]);
    for (i = -n + N; i <= n + N; ++i)
      jia(ans, f[dia[D / 2 + 1]][i]);
    for (i = 1; i <= n; ++i)
      if (col[i])
        lim[i] = D / 2 - dis[i];
      else
        lim[i] = D / 2 + 1 - Dis[i];
    memset(f, 0, sizeof(f));
    Dfs(dia[D / 2 + 1]);
    for (i = -n + N; i <= n + N; ++i)
      jia(ans, f[dia[D / 2 + 1]][i]);
    Del(dia[D / 2 + 1]);
    for (i = -n + N; i <= n + N; ++i)
      jian(ans, g[dia[D / 2 + 1]][i][1]), jian(ans, g[dia[D / 2 + 1]][i][2]),
          jian(ans, g[dia[D / 2 + 1]][i][4]);
    printf("%d\\n", ans);
  }
}