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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define poly vector<ll>
#define For(i, l, r) for (int i = (int)(l); i <= (int)(r); i++)
#define Rep(i, r, l) for (int i = (int)(r); i >= (int)(l); i--)
#define pb push_back
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline ll read() {
  ll x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(ll x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10)
    write(x / 10);
  putchar('0' + x % 10);
}
inline void writeln(ll x) {
  write(x);
  puts("");
}
inline void writep(ll x) {
  write(x);
  putchar(' ');
}
inline ull rnd() { return ((ull)rand() << 30 ^ rand()) << 4 | rand() % 4; }
const int N = 2005, mo = 1e9 + 7;
int dep[N], fa[N], D;
poly v[N];
void dfs(int u) {
  dep[u] = dep[fa[u]] + 1;
  for (auto k : v[u]) {
    if (k == fa[u])
      continue;
    fa[k] = u;
    dfs(k);
  }
}
int f[N][N << 1];
void dfs(int u, int fa, int D, int dep, int t) {
  int l = dep - D + t, r = D - dep + t;
  For(i, l + 505, r + 505) f[u][i] = 1;
  for (auto k : v[u]) {
    if (k == fa)
      continue;
    dfs(k, u, D, dep + 1, t);
    For(i, l + 505, r + 505) {
      int ssw = (f[k][i - 1] + f[k][i + 1]) % mo;
      f[u][i] = (ll)f[u][i] * ssw % mo;
    }
  }
}
int solve1(int u) {
  dfs(u, 0, D, 0, 1);
  int res = 0;
  For(i, 0, 1005) res = (res + f[u][i]) % mo;
  return res;
}
int solve2(int u, int v, int t) {
  memset(f, 0, sizeof(f));
  dfs(u, v, D + (!t), 0, 0);
  dfs(v, u, D, 0, t);
  int res = 0;
  For(i, 1, 1005) {
    int ssw = (f[v][i - 1] + f[v][i + 1]) % mo;
    res = (res + (ll)f[u][i] * ssw) % mo;
  }
  return res;
}
int main() {
  int n = read();
  For(i, 1, n - 1) {
    int x = read(), y = read();
    v[x].pb(y);
    v[y].pb(x);
  }
  dep[0] = -1;
  int rt = 0;
  dfs(1);
  For(i, 1, n) if (dep[i] > dep[rt]) rt = i;
  memset(fa, 0, sizeof(fa));
  dfs(rt);
  For(i, rt = 1, n) if (dep[i] > dep[rt]) rt = i;
  if (dep[rt] % 2 == 0) {
    D = dep[rt] / 2;
    For(i, 1, D) rt = fa[rt];
    cout << solve1(rt);
  } else {
    D = dep[rt] / 2;
    For(i, 1, D) rt = fa[rt];
    int u = rt, v = fa[rt];
    int res = (solve2(u, v, 0) + solve2(v, u, 0)) % mo;
    int ssw = (solve2(u, v, 1) + solve2(u, v, -1)) % mo;
    res = (res - ssw + mo) % mo;
    cout << res;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define poly vector<ll>
#define For(i, l, r) for (int i = (int)(l); i <= (int)(r); i++)
#define Rep(i, r, l) for (int i = (int)(r); i >= (int)(l); i--)
#define pb push_back
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline ll read() {
  ll x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(ll x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10)
    write(x / 10);
  putchar('0' + x % 10);
}
inline void writeln(ll x) {
  write(x);
  puts("");
}
inline void writep(ll x) {
  write(x);
  putchar(' ');
}
inline ull rnd() { return ((ull)rand() << 30 ^ rand()) << 4 | rand() % 4; }
const int N = 1005, mo = 1e9 + 7;
int dep[N], fa[N], D;
poly v[N];
void dfs(int u) {
  dep[u] = dep[fa[u]] + 1;
  for (auto k : v[u]) {
    if (k == fa[u])
      continue;
    fa[k] = u;
    dfs(k);
  }
}
int f[N][N << 1];
void dfs(int u, int fa, int D, int dep, int t) {
  int l = dep - D + t, r = D - dep + t;
  For(i, l + 505, r + 505) f[u][i] = 1;
  for (auto k : v[u]) {
    if (k == fa)
      continue;
    dfs(k, u, D, dep + 1, t);
    For(i, l + 505, r + 505) {
      int ssw = (f[k][i - 1] + f[k][i + 1]) % mo;
      f[u][i] = (ll)f[u][i] * ssw % mo;
    }
  }
}
int solve1(int u) {
  dfs(u, 0, D, 0, 1);
  int res = 0;
  For(i, 0, 1000) res = (res + f[u][i]) % mo;
  return res;
}
int solve2(int u, int v, int t) {
  memset(f, 0, sizeof(f));
  dfs(u, v, D + (!t), 0, 0);
  dfs(v, u, D, 0, t);
  int res = 0;
  For(i, 1, 1000) {
    int ssw = (f[v][i - 1] + f[v][i + 1]) % mo;
    res = (res + (ll)f[u][i] * ssw) % mo;
  }
  return res;
}
int main() {
  int n = read();
  read();
  For(i, 1, n - 1) {
    int x = read(), y = read();
    v[x].pb(y);
    v[y].pb(x);
  }
  dep[0] = -1;
  int rt = 0;
  dfs(1);
  For(i, 1, n) if (dep[i] > dep[rt]) rt = i;
  memset(fa, 0, sizeof(fa));
  dfs(rt);
  For(i, rt = 1, n) if (dep[i] > dep[rt]) rt = i;
  if (dep[rt] % 2 == 0) {
    D = dep[rt] / 2;
    For(i, 1, D) rt = fa[rt];
    cout << solve1(rt);
  } else {
    D = dep[rt] / 2;
    For(i, 1, D) rt = fa[rt];
    int u = rt, v = fa[rt];
    int res = (solve2(u, v, 0) + solve2(v, u, 0)) % mo;
    int ssw = (solve2(u, v, 1) + solve2(u, v, -1)) % mo;
    res = (res - ssw + mo) % mo;
    cout << res;
  }
}