Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N;
  cin >> N;
  Graph graph(N);
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  queue<P> queue1;
  queue1.push({0, 0});
  vector<bool> visited(N, false);
  vector<ll> dist(N, 0);
  visited[0] = true;
  while (!queue1.empty()) {
    P p = queue1.front();
    queue1.pop();
    dist[p.first] = p.second;
    for (ll v : graph[p.first]) {
      if (!visited[v]) {
        visited[v] = true;
        queue1.push({v, p.second + 1});
      }
    }
  }
  ll max_v = -1;
  ll max_dist = -1;
  rep2(i, 1, N) {
    if (dist[i] > max_dist) {
      max_dist = dist[i];
      max_v = i;
    }
  }
  queue<P> queue2;
  queue2.push({max_v, 0});
  visited.assign(N, false);
  dist.assign(N, 0);
  visited[max_v] = true;
  while (!queue2.empty()) {
    P p = queue2.front();
    queue2.pop();
    dist[p.first] = p.second;
    for (ll v : graph[p.first]) {
      if (!visited[v]) {
        visited[v] = true;
        queue2.push({v, p.second + 1});
      }
    }
  }
  ll max_v2 = -1;
  ll max_dist2 = -1;
  rep(i, N) {
    if (max_v == i)
      continue;
    if (dist[i] > max_dist2) {
      max_dist2 = dist[i];
      max_v2 = i;
    }
  }
  if (max_dist2 % 3 == 1) {
    cout << "Second" << '\n';
  } else {
    cout << "First" << '\n';
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N;
  cin >> N;
  Graph graph(N);
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  if (N == 1) {
    cout << "First" << '\n';
    return;
  }
  queue<P> queue1;
  queue1.push({0, 0});
  vector<bool> visited(N, false);
  vector<ll> dist(N, 0);
  visited[0] = true;
  while (!queue1.empty()) {
    P p = queue1.front();
    queue1.pop();
    dist[p.first] = p.second;
    for (ll v : graph[p.first]) {
      if (!visited[v]) {
        visited[v] = true;
        queue1.push({v, p.second + 1});
      }
    }
  }
  ll max_v = -1;
  ll max_dist = -1;
  rep2(i, 1, N) {
    if (dist[i] > max_dist) {
      max_dist = dist[i];
      max_v = i;
    }
  }
  queue<P> queue2;
  queue2.push({max_v, 0});
  visited.assign(N, false);
  dist.assign(N, 0);
  visited[max_v] = true;
  while (!queue2.empty()) {
    P p = queue2.front();
    queue2.pop();
    dist[p.first] = p.second;
    for (ll v : graph[p.first]) {
      if (!visited[v]) {
        visited[v] = true;
        queue2.push({v, p.second + 1});
      }
    }
  }
  ll max_v2 = -1;
  ll max_dist2 = -1;
  rep(i, N) {
    if (max_v == i)
      continue;
    if (dist[i] > max_dist2) {
      max_dist2 = dist[i];
      max_v2 = i;
    }
  }
  if (max_dist2 % 3 == 1) {
    cout << "Second" << '\n';
  } else {
    cout << "First" << '\n';
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}