Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const long long int MOD = 1000000007;
// const int MOD = 1000000007;
// const int MOD = 998244353;
// const long long int MOD = 998244353;

long long int N, M, K, H, W, L, R;
// int N, M, K, H, W, L, R;

void Calculate_Depth(vector<vector<int>> &edge, vector<int> &depth,
                     int stnode) {
  depth[stnode] = 0;
  queue<int> Q;
  Q.push(stnode);
  while (!Q.empty()) {
    int cn = Q.front();
    Q.pop();
    for (auto i : edge[cn]) {
      if (depth[i] > depth[cn] + 1) {
        depth[i] = depth[cn] + 1;
        Q.push(i);
      }
    }
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N;
  vector<vector<int>> edge(N);
  for (int i = 1; i < N; i++) {
    cin >> L >> R;
    L--;
    R--;
    edge[L].push_back(R);
    edge[R].push_back(L);
  }
  vector<int> depth(N, MOD);
  Calculate_Depth(edge, depth, 0);
  int index = 1;
  for (int i = 2; i < N; i++)
    if (depth[i] > depth[index])
      index = i;
  for (auto &i : depth)
    i = MOD;
  Calculate_Depth(edge, depth, index);
  int num = *max_element(depth.begin(), depth.end());
  vector<int> dp(N + 1);
  dp[0] = 1;
  for (int i = 2; i <= N; i++) {
    dp[i] = 1;
    if (dp[i - 2] && dp[i - 1])
      dp[i] = 0;
  }
  if (dp[num])
    cout << "First\n";
  else
    cout << "Second\n";
  return 0;
}

Fixed submission:
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const long long int MOD = 1000000007;
// const int MOD = 1000000007;
// const int MOD = 998244353;
// const long long int MOD = 998244353;

long long int N, M, K, H, W, L, R;
// int N, M, K, H, W, L, R;

void Calculate_Depth(vector<vector<int>> &edge, vector<int> &depth,
                     int stnode) {
  depth[stnode] = 0;
  queue<int> Q;
  Q.push(stnode);
  while (!Q.empty()) {
    int cn = Q.front();
    Q.pop();
    for (auto i : edge[cn]) {
      if (depth[i] > depth[cn] + 1) {
        depth[i] = depth[cn] + 1;
        Q.push(i);
      }
    }
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N;
  vector<vector<int>> edge(N);
  for (int i = 1; i < N; i++) {
    cin >> L >> R;
    L--;
    R--;
    edge[L].push_back(R);
    edge[R].push_back(L);
  }
  vector<int> depth(N, MOD);
  Calculate_Depth(edge, depth, 0);
  int index = 0;
  for (int i = 1; i < N; i++)
    if (depth[i] > depth[index])
      index = i;
  for (auto &i : depth)
    i = MOD;
  Calculate_Depth(edge, depth, index);
  int num = *max_element(depth.begin(), depth.end());
  vector<int> dp(N + 1);
  dp[0] = 1;
  for (int i = 2; i <= N; i++) {
    dp[i] = 1;
    if (dp[i - 2] && dp[i - 1])
      dp[i] = 0;
  }
  if (dp[num])
    cout << "First\n";
  else
    cout << "Second\n";
  return 0;
}