Problem p03660 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#pragma region atcoder
/*#include <atcoder/dsu>
using namespace atcoder;*/
#pragma endregion
#pragma region macros
using ll = long long;
using vi = vector<int>;
using vs = vector<string>;
using vl = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvc = vector<vector<char>>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#pragma endregion
#pragma region debug for var, v, vv
#define debug(var)                                                             \
  do {                                                                         \
    std::cout << #var << " : ";                                                \
    view(var);                                                                 \
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  cout << endl;
  int cnt = 0;
  for (const auto &v : vv) {
    cout << cnt << "th : ";
    view(v);
    cnt++;
  }
  cout << endl;
}
#pragma endregion
const ll mod = 1000000007;
const int inf = 1001001001;
const ll INF = 1001001001001001001;
const int MAX = 2000005;
template <class T> bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll modpow(ll a, ll p) {
  ll ret = 1;
  while (p) {
    if (p & 1) {
      ret = ret * a % mod;
    }
    a = a * a % mod;
    p >>= 1;
  }
  return ret;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

/*-----------------------------------------------------------------------------------------------------*/
const int MAX_V = 10002; // ツリーのサイズのありうる最大値
int dist_1 = 0, dist_n = 0;
int N;                   // ツリーのサイズ
vector<int> tree[MAX_V]; // ツリーを隣接リスト形式のグラフ構造で表したもの

int sizeSubtree[MAX_V]; // sizeSubtree[v] := v を根とする部分ツリーのサイズ
vector<int> centroids; // 重心列挙の答えがここに入る

/* ツリーDP */
void SubFindCentroids(int v, int parent_of_v = -1) {
  sizeSubtree[v] = 1;
  bool isCentroid = true;
  for (auto ch : tree[v]) {
    if (ch == parent_of_v)
      continue;
    SubFindCentroids(ch, v);
    if (sizeSubtree[ch] > N / 2)
      isCentroid = false;
    sizeSubtree[v] += sizeSubtree[ch];
  }
  if (N - sizeSubtree[v] > N / 2)
    isCentroid = false;
  if (isCentroid)
    centroids.push_back(v);
}

void FindCentroids() {
  centroids.clear();
  SubFindCentroids(0,
                   N); // これを呼び出すと centoroids に重心を列挙したものが入る
}

int dist(int v, int c1, int c2) {
  if (v == c1 || v == c2)
    return 0;
  queue<int> que;
  vector<int> dist(N, -1);
  que.push(v);
  dist[v] = 0;
  while (!que.empty()) {
    v = que.front();
    que.pop();
    for (int nv : tree[v]) {
      if (dist[nv] != -1)
        continue;
      if (nv == c1 || nv == c2)
        return dist[v] + 1;
      dist[nv] = dist[v] + 1;
      que.push(nv);
    }
  }
  return -1;
}
int main() {
  cin >> N;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  FindCentroids();
  int c1 = centroids[0], c2 = -inf;
  if (sz(centroids) == 2)
    c2 = centroids[1];
  dist_1 = dist(0, c1, c2);
  dist_n = dist(N - 1, c1, c2);
  if (dist_1 < dist_n) {
    cout << "Fennec" << endl;
  } else if (dist_1 > dist_n)
    cout << "Snuke" << endl;
  else {
    if (sz(centroids) == 2)
      cout << "Snuke" << endl;
    else
      cout << "Fennec" << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma region atcoder
/*#include <atcoder/dsu>
using namespace atcoder;*/
#pragma endregion
#pragma region macros
using ll = long long;
using vi = vector<int>;
using vs = vector<string>;
using vl = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvc = vector<vector<char>>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#pragma endregion
#pragma region debug for var, v, vv
#define debug(var)                                                             \
  do {                                                                         \
    std::cout << #var << " : ";                                                \
    view(var);                                                                 \
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  cout << endl;
  int cnt = 0;
  for (const auto &v : vv) {
    cout << cnt << "th : ";
    view(v);
    cnt++;
  }
  cout << endl;
}
#pragma endregion
const ll mod = 1000000007;
const int inf = 1001001001;
const ll INF = 1001001001001001001;
const int MAX = 2000005;
template <class T> bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll modpow(ll a, ll p) {
  ll ret = 1;
  while (p) {
    if (p & 1) {
      ret = ret * a % mod;
    }
    a = a * a % mod;
    p >>= 1;
  }
  return ret;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

/*-----------------------------------------------------------------------------------------------------*/
const int MAX_V = 100002; // ツリーのサイズのありうる最大値
int dist_1 = 0, dist_n = 0;
int N;                   // ツリーのサイズ
vector<int> tree[MAX_V]; // ツリーを隣接リスト形式のグラフ構造で表したもの

int sizeSubtree[MAX_V]; // sizeSubtree[v] := v を根とする部分ツリーのサイズ
vector<int> centroids; // 重心列挙の答えがここに入る

/* ツリーDP */
void SubFindCentroids(int v, int parent_of_v = -1) {
  sizeSubtree[v] = 1;
  bool isCentroid = true;
  for (auto ch : tree[v]) {
    if (ch == parent_of_v)
      continue;
    SubFindCentroids(ch, v);
    if (sizeSubtree[ch] > N / 2)
      isCentroid = false;
    sizeSubtree[v] += sizeSubtree[ch];
  }
  if (N - sizeSubtree[v] > N / 2)
    isCentroid = false;
  if (isCentroid)
    centroids.push_back(v);
}

void FindCentroids() {
  centroids.clear();
  SubFindCentroids(0,
                   N); // これを呼び出すと centoroids に重心を列挙したものが入る
}

int dist(int v, int c1, int c2) {
  if (v == c1 || v == c2)
    return 0;
  queue<int> que;
  vector<int> dist(N, -1);
  que.push(v);
  dist[v] = 0;
  while (!que.empty()) {
    v = que.front();
    que.pop();
    for (int nv : tree[v]) {
      if (dist[nv] != -1)
        continue;
      if (nv == c1 || nv == c2)
        return dist[v] + 1;
      dist[nv] = dist[v] + 1;
      que.push(nv);
    }
  }
  return -1;
}
int main() {
  cin >> N;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  FindCentroids();
  int c1 = centroids[0], c2 = -inf;
  if (sz(centroids) == 2)
    c2 = centroids[1];
  dist_1 = dist(0, c1, c2);
  dist_n = dist(N - 1, c1, c2);
  if (dist_1 < dist_n) {
    cout << "Fennec" << endl;
  } else if (dist_1 > dist_n)
    cout << "Snuke" << endl;
  else {
    if (sz(centroids) == 2)
      cout << "Snuke" << endl;
    else
      cout << "Fennec" << endl;
  }
}