Problem p03055 (C++)

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

typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  cerr << #v << ":";                                                           \
  rep(i, v.size()) cerr << " " << v[i];                                        \
  cerr << endl
using namespace std;
int MOD = 998244353;
int n;
vector<int> G[200010];
int d1[200001] = {0};
int d2[200001] = {0};
void dfs1(int s, int dis) {
  for (auto x : G[s]) {
    if (d1[x] == INF) {
      d1[x] = dis + 1;
      dfs1(x, dis + 1);
    }
  }
}
void dfs2(int s, int dis) {
  for (auto x : G[s]) {
    if (d2[x] == INF) {
      d2[x] = dis + 1;
      dfs2(x, dis + 1);
    }
  }
}
signed main() {
  cin >> n;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    G[a - 1].pb(b - 1);
    G[b - 1].pb(a - 1);
  }
  rep(i, n) {
    d1[i] = INF;
    d2[i] = INF;
  }
  d1[0] = 0;
  dfs1(0, 0);
  int m = -1;
  int u = -1;
  rep(i, n) {
    if (d1[i] > m) {
      m = d1[i];
      u = i;
    }
  }
  d2[u] = 0;
  dfs2(u, 0);
  int m1 = -1;
  rep(i, n) {
    if (d2[i] > m1) {
      m1 = d2[i];
    }
  }
  if (m1 % 3 == 1)
    cout << "Second" << endl;
  else
    cout << "First" << endl;
  cerr << m1 << endl;

  return 0;
}

// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl
// https://cf17-final-open.contest.atcoder.jp/tasks/cf17_final_a rm -r -f
// test;oj dl http://agc033.contest.atcoder.jp/tasks/agc033_a

Fixed submission:
#include "bits/stdc++.h"

typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  cerr << #v << ":";                                                           \
  rep(i, v.size()) cerr << " " << v[i];                                        \
  cerr << endl
using namespace std;
int MOD = 998244353;
int n;
vector<int> G[200010];
int d1[200001] = {0};
int d2[200001] = {0};
void dfs1(int s, int dis) {
  for (auto x : G[s]) {
    if (d1[x] == INF) {
      d1[x] = dis + 1;
      dfs1(x, dis + 1);
    }
  }
}
void dfs2(int s, int dis) {
  for (auto x : G[s]) {
    if (d2[x] == INF) {
      d2[x] = dis + 1;
      dfs2(x, dis + 1);
    }
  }
}
signed main() {
  cin >> n;
  if (n == 1) {
    cout << "First" << endl;
    return 0;
  }
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    G[a - 1].pb(b - 1);
    G[b - 1].pb(a - 1);
  }
  rep(i, n) {
    d1[i] = INF;
    d2[i] = INF;
  }
  d1[0] = 0;
  dfs1(0, 0);
  int m = -1;
  int u = -1;
  rep(i, n) {
    if (d1[i] > m) {
      m = d1[i];
      u = i;
    }
  }
  d2[u] = 0;
  dfs2(u, 0);
  int m1 = -1;
  rep(i, n) {
    if (d2[i] > m1) {
      m1 = d2[i];
    }
  }
  if (m1 % 3 == 1)
    cout << "Second" << endl;
  else
    cout << "First" << endl;
  cerr << m1 << endl;

  return 0;
}

// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl
// https://cf17-final-open.contest.atcoder.jp/tasks/cf17_final_a rm -r -f
// test;oj dl http://agc033.contest.atcoder.jp/tasks/agc033_a