Problem p03348 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(x, y, h, w) x >= 0 && x < h &&y >= 0 && y < w

#define int long long
// typedef    long long          ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> P;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

const int INF = 1e+18;
const double EPS = 1e-9;
const int MOD = 1000000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int ma = -1, mai;
int d[100], n;
vi G[100];

void dfs(int v, int par) {
  for (int to : G[v]) {
    if (to != par) {
      d[to] = d[v] + 1;
      dfs(to, v);
    }
  }
}

int calc(int c1, int c2) {
  d[c1] = 0;
  d[c2] = 0;
  dfs(c1, c2);
  dfs(c2, c1);
  int cnt[110][110] = {};
  for (int i = 0; i < n; i++) {
    cnt[d[i]][G[i].size()]++;
  }
  for (int i = 0; i < ma / 2; i++) {
    int m = 0;
    for (int j = 0; j < n; j++) {
      if (cnt[i][j])
        chmax(m, j);
    }
    for (int j = 0; j < n; j++) {
      cnt[i + 1][1] += (m - j) * cnt[i][j];
    }
  }
  return cnt[ma / 2][1];
}

signed main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
    G[b].pb(a);
  }
  dfs(0, -1);
  for (int i = 0; i < n; i++) {
    if (d[i] > ma) {
      mai = i;
      ma = d[i];
    }
  }
  d[mai] = 0;
  dfs(mai, -1);
  ma = -1;
  for (int i = 0; i < n; i++)
    chmax(ma, d[i]);
  int c1 = -1, c2 = -1;
  for (int i = 0; i < n; i++) {
    int t = 0;
    d[i] = 0;
    dfs(i, -1);
    for (int j = 0; j < n; j++)
      chmax(t, d[j]);
    if (t <= ma / 2) {
      if (c1 == -1)
        c1 = i;
      else
        c2 = i;
    }
  }
  int ans = INF;
  if (c2 == -1) {
    ans = calc(c1, c1);
    for (int to : G[c1])
      chmin(ans, calc(c1, to));
  } else
    ans = calc(c1, c2);
  cout << ma / 2 + 1 << " " << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(x, y, h, w) x >= 0 && x < h &&y >= 0 && y < w

#define int long long
// typedef    long long          ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> P;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

const int INF = 1e+18;
const double EPS = 1e-9;
const int MOD = 1000000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int ma = -1, mai;
int d[100], n;
vi G[100];

void dfs(int v, int par) {
  for (int to : G[v]) {
    if (to != par) {
      d[to] = d[v] + 1;
      dfs(to, v);
    }
  }
}

int calc(int c1, int c2) {
  d[c1] = 0;
  d[c2] = 0;
  dfs(c1, c2);
  dfs(c2, c1);
  int cnt[110][110] = {};
  for (int i = 0; i < n; i++) {
    cnt[d[i]][G[i].size()]++;
  }
  for (int i = 0; i < ma / 2; i++) {
    int m = 0;
    for (int j = 0; j < n; j++) {
      if (cnt[i][j])
        chmax(m, j);
    }
    for (int j = 0; j < n; j++) {
      cnt[i + 1][1] += (m - j) * cnt[i][j];
    }
  }
  return cnt[ma / 2][1];
}

signed main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
    G[b].pb(a);
  }
  dfs(0, -1);
  for (int i = 0; i < n; i++) {
    if (d[i] > ma) {
      mai = i;
      ma = d[i];
    }
  }
  d[mai] = 0;
  dfs(mai, -1);
  ma = -1;
  for (int i = 0; i < n; i++)
    chmax(ma, d[i]);
  int c1 = -1, c2 = -1;
  for (int i = 0; i < n; i++) {
    int t = 0;
    d[i] = 0;
    dfs(i, -1);
    for (int j = 0; j < n; j++)
      chmax(t, d[j]);
    if (t <= (ma + 1) / 2) {
      if (c1 == -1)
        c1 = i;
      else
        c2 = i;
    }
  }
  int ans = INF;
  if (c2 == -1) {
    ans = calc(c1, c1);
    for (int to : G[c1])
      chmin(ans, calc(c1, to));
  } else
    ans = calc(c1, c2);
  cout << ma / 2 + 1 << " " << ans << endl;
  return 0;
}