Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>     // minmax, sort, swap
#include <climits>       // INT_MIN, LLONG_MIN
#include <cmath>         // long, trig, pow
#include <cstdio>        // printf, scanf
#include <deque>         // deque
#include <functional>    // std::function<void(int)>
#include <iomanip>       // cout<<setprecision(n)
#include <iostream>      // cin, cout, cerr, clog
#include <map>           // key-value pairs sorted by keys
#include <numeric>       // iota, accumulate, inner_product
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, stoi, to_string
#include <unordered_map> // hashed by keys
#include <unordered_set> // hashed by keys
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ENDL '\n'
#define print(i) std::cout << (i) << '\n'

#define int long long // at least int64 > 9*10^18
#define all(v) (v).begin(), (v).end()
/* libraries */

signed main() {
  int n;
  std::cin >> n;
  std::vector<std::vector<int>> t(n, std::vector<int>());
  rep(i, n - 1) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    t[a].emplace_back(b);
    t[b].emplace_back(a);
  }

  int v;         // maxi
  int fmaxd = 0; // first max
  std::vector<int> went(n, 0);
  std::function<void(int, int)> dfs = [&](int i, int d) {
    went[i] = true;
    if (fmaxd < d) {
      fmaxd = d;
      v = i;
    }
    for (auto to : t[i]) {
      if (!went[to]) {
        dfs(to, d + 1);
      }
    }
  };
  dfs(0, 0);
  rep(i, n) went[i] = false;
  dfs(v, 0);
  if (fmaxd % 3 == 1) {
    print("Second");
  } else {
    print("First");
  }

  return 0;
}


Fixed submission:
#include <algorithm>     // minmax, sort, swap
#include <climits>       // INT_MIN, LLONG_MIN
#include <cmath>         // long, trig, pow
#include <cstdio>        // printf, scanf
#include <deque>         // deque
#include <functional>    // std::function<void(int)>
#include <iomanip>       // cout<<setprecision(n)
#include <iostream>      // cin, cout, cerr, clog
#include <map>           // key-value pairs sorted by keys
#include <numeric>       // iota, accumulate, inner_product
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, stoi, to_string
#include <unordered_map> // hashed by keys
#include <unordered_set> // hashed by keys
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ENDL '\n'
#define print(i) std::cout << (i) << '\n'

#define int long long // at least int64 > 9*10^18
#define all(v) (v).begin(), (v).end()
/* libraries */

signed main() {
  int n;
  std::cin >> n;
  std::vector<std::vector<int>> t(n, std::vector<int>());
  rep(i, n - 1) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    t[a].emplace_back(b);
    t[b].emplace_back(a);
  }

  int v = 0;     // maxi
  int fmaxd = 0; // first max
  std::vector<int> went(n, 0);
  std::function<void(int, int)> dfs = [&](int i, int d) {
    went[i] = true;
    if (fmaxd < d) {
      fmaxd = d;
      v = i;
    }
    for (auto to : t[i]) {
      if (!went[to]) {
        dfs(to, d + 1);
      }
    }
  };
  dfs(0, 0);
  rep(i, n) went[i] = false;
  dfs(v, 0);
  if (fmaxd % 3 == 1) {
    print("Second");
  } else {
    print("First");
  }

  return 0;
}