Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define mt(a, b, c) make_tuple((a), (b), (c))

typedef long long ll;
const int maxV = 111111;
vector<int> G[maxV]; // 頂点情報のみのグラフ

// treeDFS(親, 現在地, 根から現在地までの距離, 根からの最大の距離,
// 根から最大の距離となる頂点
void treeDFS(int from, int current, int dist, int &maxDist, int &maxVertex) {
  // 距離と終点を更新
  if (dist > maxDist) {
    maxDist = dist;
    maxVertex = current;
  }

  for (auto to : G[current]) {
    // 逆流を防ぐ
    if (to == from)
      continue;
    treeDFS(current, to, dist + 1, maxDist, maxVertex);
  }
}

void getTreeDiameter() {
  int start = 0, end = 0, maxDist = 0;
  treeDFS(-1, start, 0, maxDist, end);
  start = end, end = 0, maxDist = 0;
  treeDFS(-1, start, 0, maxDist, end);
  // printf("start: %d, end: %d, diameter: %d\n", start, end, maxDist);
  if (maxDist == 0)
    cout << "First" << endl;
  else if (maxDist == 1)
    cout << "Second" << endl;
  else if (maxDist % 3 == 1)
    cout << "Second" << endl;
  else
    cout << "First" << endl;
}
int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    G[a - 1].pb(b - 1);
    G[b - 1].pb(a - 1);
  }
  getTreeDiameter();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define mt(a, b, c) make_tuple((a), (b), (c))

typedef long long ll;
const int maxV = 211111;
vector<int> G[maxV]; // 頂点情報のみのグラフ

// treeDFS(親, 現在地, 根から現在地までの距離, 根からの最大の距離,
// 根から最大の距離となる頂点
void treeDFS(int from, int current, int dist, int &maxDist, int &maxVertex) {
  // 距離と終点を更新
  if (dist > maxDist) {
    maxDist = dist;
    maxVertex = current;
  }

  for (auto to : G[current]) {
    // 逆流を防ぐ
    if (to == from)
      continue;
    treeDFS(current, to, dist + 1, maxDist, maxVertex);
  }
}

void getTreeDiameter() {
  int start = 0, end = 0, maxDist = 0;
  treeDFS(-1, start, 0, maxDist, end);
  start = end, end = 0, maxDist = 0;
  treeDFS(-1, start, 0, maxDist, end);
  // printf("start: %d, end: %d, diameter: %d\n", start, end, maxDist);
  if (maxDist == 0)
    cout << "First" << endl;
  else if (maxDist == 1)
    cout << "Second" << endl;
  else if (maxDist % 3 == 1)
    cout << "Second" << endl;
  else
    cout << "First" << endl;
}
int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    G[a - 1].pb(b - 1);
    G[b - 1].pb(a - 1);
  }
  getTreeDiameter();
  return 0;
}