Problem p03660 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define countof(array) (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rep2(i, n) for (int i = 1; i <= (n); ++i)
#define rrep2(i, n) for (int i = (n); i > 0; --i)
#define srep(i, s, n) for (int i = s; i < (n); ++i)
#define rsrep(i, s, n) for (int i = (n)-1; i >= s; --i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define aall(a) (a), (a) + countof(a) // for array sorting
#define raall(a) (a), (a) + countof(a), greater<>()
#define show(x) cout << #x << " = " << (x) << endl;
#define vfind(v, a) find(all(v), a) != v.end()
#define yn(f)                                                                  \
  {                                                                            \
    if (f)                                                                     \
      puts("YES");                                                             \
    else                                                                       \
      puts("NO");                                                              \
  }
#define yns(f)                                                                 \
  {                                                                            \
    if (f)                                                                     \
      puts("Yes");                                                             \
    else                                                                       \
      puts("No");                                                              \
  }
#define show_ary(...)                                                          \
  {                                                                            \
    cout << #__VA_ARGS__ << " = ";                                             \
    for (const auto &x : (__VA_ARGS__)) {                                      \
      cout << x << " ";                                                        \
    }                                                                          \
    cout << endl;                                                              \
  }
#define show_pair(...)                                                         \
  cout << #__VA_ARGS__ << " = " << endl;                                       \
  for (const auto &x : (__VA_ARGS__)) {                                        \
    cout << "  " << x.fi << " : " << x.se << endl;                             \
  }
#define out_ary(...)                                                           \
  {                                                                            \
    int n = (__VA_ARGS__).size();                                              \
    rep(i, n) printf("%d%s", (__VA_ARGS__)[i], i != n - 1 ? " " : "\n");       \
  }
#define argmax(v) distance((v).begin(), max_element(all(v)))
#define argmin(v) distance((v).begin(), min_element(all(v)))
#define vmax(v) *max_element(all(v))
#define vmin(v) *min_element(all(v))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<P> vpair;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<string> vstr;
typedef vector<bool> vbool;
typedef vector<vint> vvint;
typedef vector<vll> vvll;
typedef vector<vstr> vvstr;
typedef vector<vbool> vvbool;
const ll LINF = 2000000000000000000ll;
const int INF = 1000000100;
const ll MOD = 1e9 + 7;

int n;
vint e[100005];
vint g;
vint p;

void dfs_f(int u, int prev) {
  if (g[u] == 1)
    return;
  g[u] = 0;
  for (int v : e[u]) {
    if (v == prev)
      continue;
    dfs_f(v, u);
  }
}

void dfs_s(int u, int prev) {
  if (g[u] == 0)
    return;
  g[u] = 1;
  for (int v : e[u]) {
    if (v == prev)
      continue;
    dfs_s(v, u);
  }
}

bool dfs(int u, int prev) {
  bool res = false;
  if (u == n - 1) {
    res = true;
    p.push_back(u);
  }
  for (int v : e[u]) {
    if (v == prev)
      continue;
    bool res2 = dfs(v, u);
    if (res2) {
      res = true;
      p.push_back(u);
    }
  }
  return res;
}

int main() {
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    e[a].push_back(b);
    e[b].push_back(a);
  }

  g.resize(n, -1);
  g[0] = 0;
  g[n - 1] = 1;

  dfs(0, -1);

  int psz = p.size();
  rrep(i, psz / 2) { g[p[i]] = 0; }
  if (psz & 1)
    g[p[psz / 2]] = 0;
  rep(i, psz / 2) { g[p[i]] = 1; }
  vint g_c = g;
  rep(i, n) {
    if (g_c[i] == 0)
      dfs_f(i, -1);
    if (g_c[i] == 1)
      dfs_s(i, -1);
  }

  int b = 0;
  rep(i, n) if (!g[i]) b++;
  if (b > n - b)
    puts("Fennec");
  else
    puts("Snuke");

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define countof(array) (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rep2(i, n) for (int i = 1; i <= (n); ++i)
#define rrep2(i, n) for (int i = (n); i > 0; --i)
#define srep(i, s, n) for (int i = s; i < (n); ++i)
#define rsrep(i, s, n) for (int i = (n)-1; i >= s; --i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define aall(a) (a), (a) + countof(a) // for array sorting
#define raall(a) (a), (a) + countof(a), greater<>()
#define show(x) cout << #x << " = " << (x) << endl;
#define vfind(v, a) find(all(v), a) != v.end()
#define yn(f)                                                                  \
  {                                                                            \
    if (f)                                                                     \
      puts("YES");                                                             \
    else                                                                       \
      puts("NO");                                                              \
  }
#define yns(f)                                                                 \
  {                                                                            \
    if (f)                                                                     \
      puts("Yes");                                                             \
    else                                                                       \
      puts("No");                                                              \
  }
#define show_ary(...)                                                          \
  {                                                                            \
    cout << #__VA_ARGS__ << " = ";                                             \
    for (const auto &x : (__VA_ARGS__)) {                                      \
      cout << x << " ";                                                        \
    }                                                                          \
    cout << endl;                                                              \
  }
#define show_pair(...)                                                         \
  cout << #__VA_ARGS__ << " = " << endl;                                       \
  for (const auto &x : (__VA_ARGS__)) {                                        \
    cout << "  " << x.fi << " : " << x.se << endl;                             \
  }
#define out_ary(...)                                                           \
  {                                                                            \
    int n = (__VA_ARGS__).size();                                              \
    rep(i, n) printf("%d%s", (__VA_ARGS__)[i], i != n - 1 ? " " : "\n");       \
  }
#define argmax(v) distance((v).begin(), max_element(all(v)))
#define argmin(v) distance((v).begin(), min_element(all(v)))
#define vmax(v) *max_element(all(v))
#define vmin(v) *min_element(all(v))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<P> vpair;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<string> vstr;
typedef vector<bool> vbool;
typedef vector<vint> vvint;
typedef vector<vll> vvll;
typedef vector<vstr> vvstr;
typedef vector<vbool> vvbool;
const ll LINF = 2000000000000000000ll;
const int INF = 1000000100;
const ll MOD = 1e9 + 7;

int n;
vint e[100005];
vint g;
vint p;

void dfs_f(int u, int prev) {
  if (g[u] == 1)
    return;
  g[u] = 0;
  for (int v : e[u]) {
    if (v == prev)
      continue;
    dfs_f(v, u);
  }
}

void dfs_s(int u, int prev) {
  if (g[u] == 0)
    return;
  g[u] = 1;
  for (int v : e[u]) {
    if (v == prev)
      continue;
    dfs_s(v, u);
  }
}

bool dfs(int u, int prev) {
  bool res = false;
  if (u == n - 1) {
    res = true;
    p.push_back(u);
  }
  for (int v : e[u]) {
    if (v == prev)
      continue;
    bool res2 = dfs(v, u);
    if (res2) {
      res = true;
      p.push_back(u);
    }
  }
  return res;
}

int main() {
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    e[a].push_back(b);
    e[b].push_back(a);
  }

  g.resize(n, -1);
  g[0] = 0;
  g[n - 1] = 1;

  dfs(0, -1);

  int psz = p.size();
  rrep(i, psz / 2) { g[p[i]] = 0; }
  if (psz & 1)
    g[p[psz / 2]] = 0;
  rep(i, psz / 2) { g[p[i]] = 1; }

  dfs_f(0, -1);
  dfs_s(n - 1, -1);

  int b = 0;
  rep(i, n) if (!g[i]) b++;
  if (b > n - b)
    puts("Fennec");
  else
    puts("Snuke");

  return 0;
}