Problem p03660 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define all(x) (x).begin(), (x).end()
typedef long long ll;
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;
}
void SAY(bool YES) { cout << (YES ? "YES" : "NO") << endl; };
void Say(bool Yes) { cout << (Yes ? "Yes" : "No") << endl; };

typedef pair<ll, ll> P;

ll N;
vector<ll> path[101010];

ll from[101010];

ll priori[101010];

ll flg[101010];

int main() {

  cin >> N;
  for (ll i = 1; i < N; i++) {
    ll a, b;
    cin >> a >> b;
    path[a].push_back(b);
    path[b].push_back(a);
  }

  queue<ll> Q;
  Q.push(1);
  from[1] = -1;
  while (!Q.empty()) {
    ll idx = Q.front();
    Q.pop();
    for (ll i = 0; i < path[idx].size(); i++) {
      ll nxt = path[idx][i];
      if (from[nxt] == 0) {
        from[nxt] = 1 + from[idx];
        Q.push(nxt);
      }
    }
  }
  for (ll i = from[N]; i > 1; i = from[i])
    priori[i] = 1;

  priority_queue<P> PQ[2];
  PQ[0].push(P(1, 1));
  PQ[1].push(P(1, N));
  for (ll i = 0;; i++) {
    ll p = i % 2;
    ll idx = -1;
    while (!PQ[p].empty()) {
      ll nxt = PQ[p].top().second;
      PQ[p].pop();
      if (flg[nxt])
        continue;
      idx = nxt;
      break;
    }
    if (idx == -1) {
      if (p == 0)
        cout << "Snuke" << endl;
      else
        cout << "Fennec" << endl;
      return 0;
    }
    flg[idx] = 1;
    for (ll j = 0; j < path[idx].size(); j++) {
      ll nxt = path[idx][j];
      if (flg[nxt])
        continue;
      if (priori[nxt])
        PQ[p].push(P(1, nxt));
      else
        PQ[p].push(P(0, nxt));
    }
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define all(x) (x).begin(), (x).end()
typedef long long ll;
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;
}
void SAY(bool YES) { cout << (YES ? "YES" : "NO") << endl; };
void Say(bool Yes) { cout << (Yes ? "Yes" : "No") << endl; };

typedef pair<ll, ll> P;

ll N;
vector<ll> path[101010];

ll from[101010];

ll priori[101010];

ll flg[101010];

int main() {

  cin >> N;
  for (ll i = 1; i < N; i++) {
    ll a, b;
    cin >> a >> b;
    path[a].push_back(b);
    path[b].push_back(a);
  }

  queue<ll> Q;
  Q.push(1);
  from[1] = -1;
  while (!Q.empty()) {
    ll idx = Q.front();
    Q.pop();
    for (ll i = 0; i < path[idx].size(); i++) {
      ll nxt = path[idx][i];
      if (from[nxt] == 0) {
        from[nxt] = idx;
        Q.push(nxt);
      }
    }
  }
  for (ll i = from[N]; i > 1; i = from[i])
    priori[i] = 1;

  priority_queue<P> PQ[2];
  PQ[0].push(P(1, 1));
  PQ[1].push(P(1, N));
  for (ll i = 0;; i++) {
    ll p = i % 2;
    ll idx = -1;
    while (!PQ[p].empty()) {
      ll nxt = PQ[p].top().second;
      PQ[p].pop();
      if (flg[nxt])
        continue;
      idx = nxt;
      break;
    }
    if (idx == -1) {
      if (p == 0)
        cout << "Snuke" << endl;
      else
        cout << "Fennec" << endl;
      return 0;
    }
    flg[idx] = 1;
    for (ll j = 0; j < path[idx].size(); j++) {
      ll nxt = path[idx][j];
      if (flg[nxt])
        continue;
      if (priori[nxt])
        PQ[p].push(P(1, nxt));
      else
        PQ[p].push(P(0, nxt));
    }
  }

  return 0;
}