Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// #include <boost/multiprecision/cpp_int.hpp>
// using multiInt = boost::multiprecision::cpp_int;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_temp>
using smaller_queue = priority_queue<Q_temp, vector<Q_temp>, greater<Q_temp>>;

const int INF = (int)1e9;
const ll LINF = (ll)4e18;
const ll MOD = (ll)(1e9 + 7);
const double PI = acos(-1.0);
const int limit = 100010;

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbg2(x_)                                                               \
  for (auto a_ : x_)                                                           \
    cerr << a_ << " ";                                                         \
  cerr << endl;
#define dbg3(x_, sx_)                                                          \
  rep(i, sx_) cerr << x_[i] << " ";                                            \
  cerr << endl;
vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};

inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; }

//------------------------------------------------------
const ll MAX_V = 200010;

class dijkstra {
public:
  struct edge {
    ll to;
    ll cost;
  };

  vector<edge> E[MAX_V];
  ll d[MAX_V];

  void calc(ll s) {
    priority_queue<pll, vector<pll>, greater<pll>> que;
    rep(i, MAX_V) d[i] = LINF;
    que.emplace(0LL, s);
    d[s] = 0LL;
    while (!que.empty()) {
      pll p = que.top();
      que.pop();
      ll v = p.second;
      if (d[v] < p.first)
        continue;

      for (auto &&e : E[v]) {
        if (d[e.to] > d[v] + e.cost) {
          d[e.to] = d[v] + e.cost;
          que.emplace(d[e.to], e.to);
        }
      }
    }
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  dijkstra ds;

  rep(i, n) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    ds.E[a].push_back({b, 1});
    ds.E[b].push_back({a, 1});
  }
  ds.calc(0);
  int v, max_d = -1;

  rep(i, n) {
    if (ds.d[i] > max_d) {
      max_d = ds.d[i];
      v = i;
    }
  }
  ds.calc(v);
  ll diameter = 0;
  rep(i, n) diameter = max(diameter, ds.d[i]);
  cout << (diameter % 3 == 1 ? "Second" : "First") << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #include <boost/multiprecision/cpp_int.hpp>
// using multiInt = boost::multiprecision::cpp_int;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_temp>
using smaller_queue = priority_queue<Q_temp, vector<Q_temp>, greater<Q_temp>>;

const int INF = (int)1e9;
const ll LINF = (ll)4e18;
const ll MOD = (ll)(1e9 + 7);
const double PI = acos(-1.0);
const int limit = 100010;

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbg2(x_)                                                               \
  for (auto a_ : x_)                                                           \
    cerr << a_ << " ";                                                         \
  cerr << endl;
#define dbg3(x_, sx_)                                                          \
  rep(i, sx_) cerr << x_[i] << " ";                                            \
  cerr << endl;
vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};

inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; }

//------------------------------------------------------
const ll MAX_V = 200010;

class dijkstra {
public:
  struct edge {
    ll to;
    ll cost;
  };

  vector<edge> E[MAX_V];
  ll d[MAX_V];

  void calc(ll s) {
    priority_queue<pll, vector<pll>, greater<pll>> que;
    rep(i, MAX_V) d[i] = LINF;
    que.emplace(0LL, s);
    d[s] = 0LL;
    while (!que.empty()) {
      pll p = que.top();
      que.pop();
      ll v = p.second;
      if (d[v] < p.first)
        continue;

      for (auto &&e : E[v]) {
        if (d[e.to] > d[v] + e.cost) {
          d[e.to] = d[v] + e.cost;
          que.emplace(d[e.to], e.to);
        }
      }
    }
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  dijkstra ds;

  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    ds.E[a].push_back({b, 1});
    ds.E[b].push_back({a, 1});
  }
  ds.calc(0);
  int v, max_d = -1;

  rep(i, n) {
    if (ds.d[i] > max_d) {
      max_d = ds.d[i];
      v = i;
    }
  }
  ds.calc(v);
  ll diameter = 0;
  rep(i, n) diameter = max(diameter, ds.d[i]);
  cout << (diameter % 3 == 1 ? "Second" : "First") << endl;
  return 0;
}