Problem p03348 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define mmax(a, b) a = max(a, b)
#define mmin(a, b) a = min(a, b)
#define mkp make_pair
#define pin pair<int, int>
#define V vector
vel dis(int mid1, vvel &way) {
  int n = way.size();
  vel dist(n, -1);
  dist[mid1] = 0;
  queue<int> q;
  q.push(mid1);
  while (!q.empty()) {
    int st = q.front();
    q.pop();
    rep(i, way[st].size()) {
      int to = way[st][i];
      if (dist[to] == -1) {
        dist[to] = dist[st] + 1;
        q.push(to);
      }
    }
  }
  return dist;
}
pin most_far(int now, int n, vvel &way) {
  vel dist1 = dis(now, way);
  pin ans = mkp(-1, 0);
  rep(i, n) {
    if (dist1[i] > ans.first) {
      ans = mkp(dist1[i], i);
    }
  }
  return ans;
}
int fid(int le, int st, int goal, vvel &way) {
  vel dist = dis(st, way);
  while (dist[goal] > le) {
    rep(j, way[goal].size()) {
      if (dist[goal] > dist[way[goal][j]]) {
        goal = way[goal][j];
        break;
      }
    }
  }
  return goal;
}
int RE() {
  vel v(3, 2);
  return v.at(4);
}
signed main() {
  int n;
  cin >> n;
  vvel way(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    way[a].push_back(b);
    way[b].push_back(a);
  }
  int u = most_far(0, n, way).second;
  pin pi = most_far(u, n, way);
  int v = pi.second;
  int cho = pi.first;
  int kind = (cho / 2) + 1;
  cout << kind << " ";
  int mid1 = fid(cho / 2, u, v, way);
  vel dist1 = dis(mid1, way);
  if (cho % 2 == 0) {
    vel ans(kind, 1);
    rep(i, n) {
      int waysz = way[i].size();
      mmax(ans[dist1[i]], waysz - 1);
    }
    ans[0]++;
    int rret = 1;
    for (int i = 0; i < kind - 1; i++) {
      rret *= ans[i];
    }
    rep(j, way[mid1].size()) {
      int mid2 = way[mid1][j];
      vel dist2 = dis(mid2, way);
      ans = vel(kind, 1);
      rep(i, n) {
        int waysz = way[i].size();
        mmax(ans[min(dist1[i], dist2[i])], waysz - 1);
      }
      int ret = 1;
      for (int i = 0; i < kind - 1; i++) {
        ret *= ans[i];
      }
      mmin(rret, ret * 2);
    }
    cout << rret << endl;
  } else {
    RE();
    int mid2 = fid(kind + 1, u, v, way);
    vel dist2 = dis(mid2, way);
    vel ans(kind, 1);
    rep(i, n) {
      int waysz = way[i].size();
      mmax(ans[min(dist1[i], dist2[i])], waysz - 1);
    }
    int ret = 1;
    for (int i = 0; i < kind - 1; i++) {
      ret *= ans[i];
    }
    cout << ret * 2 << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define mmax(a, b) a = max(a, b)
#define mmin(a, b) a = min(a, b)
#define mkp make_pair
#define pin pair<int, int>
#define V vector
vel dis(int mid1, vvel &way) {
  int n = way.size();
  vel dist(n, -1);
  dist[mid1] = 0;
  queue<int> q;
  q.push(mid1);
  while (!q.empty()) {
    int st = q.front();
    q.pop();
    rep(i, way[st].size()) {
      int to = way[st][i];
      if (dist[to] == -1) {
        dist[to] = dist[st] + 1;
        q.push(to);
      }
    }
  }
  return dist;
}
pin most_far(int now, int n, vvel &way) {
  vel dist1 = dis(now, way);
  pin ans = mkp(-1, 0);
  rep(i, n) {
    if (dist1[i] > ans.first) {
      ans = mkp(dist1[i], i);
    }
  }
  return ans;
}
int fid(int le, int st, int goal, vvel &way) {
  vel dist = dis(st, way);
  while (dist[goal] > le) {
    rep(j, way[goal].size()) {
      if (dist[goal] > dist[way[goal][j]]) {
        goal = way[goal][j];
        break;
      }
    }
  }
  return goal;
}
int RE() {
  vel v(3, 2);
  return v.at(4);
}
signed main() {
  int n;
  cin >> n;
  vvel way(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    way[a].push_back(b);
    way[b].push_back(a);
  }
  int u = most_far(0, n, way).second;
  pin pi = most_far(u, n, way);
  int v = pi.second;
  int cho = pi.first;
  int kind = (cho / 2) + 1;
  cout << kind << " ";
  int mid1 = fid(cho / 2, u, v, way);
  vel dist1 = dis(mid1, way);
  if (cho % 2 == 0) {
    vel ans(kind, 1);
    rep(i, n) {
      int waysz = way[i].size();
      mmax(ans[dist1[i]], waysz - 1);
    }
    ans[0]++;
    int rret = 1;
    for (int i = 0; i < kind - 1; i++) {
      rret *= ans[i];
    }
    rep(j, way[mid1].size()) {
      int mid2 = way[mid1][j];
      vel dist2 = dis(mid2, way);
      ans = vel(kind, 1);
      rep(i, n) {
        int waysz = way[i].size();
        mmax(ans[min(dist1[i], dist2[i])], waysz - 1);
      }
      int ret = 1;
      for (int i = 0; i < kind - 1; i++) {
        ret *= ans[i];
      }
      mmin(rret, ret * 2);
    }
    cout << rret << endl;
  } else {
    int mid2 = fid(kind, u, v, way);
    vel dist2 = dis(mid2, way);
    vel ans(kind, 1);
    rep(i, n) {
      int waysz = way[i].size();
      mmax(ans[min(dist1[i], dist2[i])], waysz - 1);
    }
    int ret = 1;
    for (int i = 0; i < kind - 1; i++) {
      ret *= ans[i];
    }
    cout << ret * 2 << endl;
  }
  return 0;
}