Problem p03055 (C++)

Buggy submission (status: Runtime Error):
/*
 * =template=.cpp
 * Copyright (C) 2017 hzw <hzw@huzhenweideMacBook.local>
 *
 * Distributed under terms of the MIT license.
 */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <signal.h>
#include <string>
#include <unistd.h>
#define FR first
#define SC second
#define MP make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define ws wss
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
void read(int &x) {
  char ch;
  int fu = 1;
  while ((ch = getchar()) <= 32)
    ;
  x = 0;
  if (ch == '-')
    fu = -1;
  else
    x = ch - 48;
  while ((ch = getchar()) > 32)
    x = x * 10 + ch - 48;
  x *= fu;
}
template <class T> void upmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void upmin(T &a, T b) {
  if (a > b)
    a = b;
}

//---------template--------------
const int N = 210000, p = 1000000007;
struct Edge {
  int y, ne;
};
int n, num;
int d[N];
Edge e[N];
int last[N];
void dfs(int i) {
  for (int j = last[i]; j; j = e[j].ne)
    if (d[e[j].y] == 0) {
      d[e[j].y] = d[i] + 1;
      dfs(e[j].y);
    }
}
void add(int x, int y) {
  e[++num] = (Edge){y, last[x]};
  last[x] = num;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  d[1] = 1;
  dfs(1);
  int q = 1;
  for (int i = 2; i <= n; i++)
    if (d[i] > d[q])
      q = i;
  for (int i = 1; i <= n; i++)
    d[i] = 0;
  d[q] = 1;
  dfs(q);
  int r = 0;
  for (int i = 1; i <= n; i++)
    r = max(r, d[i]);
  puts(r % 3 == 2 ? "Second" : "First");
}
/*
   0 1 2 3 4 5
   0 1 0 1 1 0
 */


Fixed submission:
/*
 * =template=.cpp
 * Copyright (C) 2017 hzw <hzw@huzhenweideMacBook.local>
 *
 * Distributed under terms of the MIT license.
 */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <signal.h>
#include <string>
#include <unistd.h>
#define FR first
#define SC second
#define MP make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define ws wss
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
void read(int &x) {
  char ch;
  int fu = 1;
  while ((ch = getchar()) <= 32)
    ;
  x = 0;
  if (ch == '-')
    fu = -1;
  else
    x = ch - 48;
  while ((ch = getchar()) > 32)
    x = x * 10 + ch - 48;
  x *= fu;
}
template <class T> void upmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void upmin(T &a, T b) {
  if (a > b)
    a = b;
}

//---------template--------------
const int N = 210000, p = 1000000007;
struct Edge {
  int y, ne;
};
int n, num;
int d[N];
Edge e[N * 2];
int last[N];
void dfs(int i) {
  for (int j = last[i]; j; j = e[j].ne)
    if (d[e[j].y] == 0) {
      d[e[j].y] = d[i] + 1;
      dfs(e[j].y);
    }
}
void add(int x, int y) {
  e[++num] = (Edge){y, last[x]};
  last[x] = num;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  d[1] = 1;
  dfs(1);
  int q = 1;
  for (int i = 2; i <= n; i++)
    if (d[i] > d[q])
      q = i;
  for (int i = 1; i <= n; i++)
    d[i] = 0;
  d[q] = 1;
  dfs(q);
  int r = 0;
  for (int i = 1; i <= n; i++)
    r = max(r, d[i]);
  puts(r % 3 == 2 ? "Second" : "First");
}
/*
   0 1 2 3 4 5
   0 1 0 1 1 0
 */