Problem p03645 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RFOR(i, a, b) for (int i = (int)b - 1; i >= (int)a; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define debug(x) cerr << #x << ":" << x << endl;
#define OK(ok) cout << (ok ? "POSSIBLE" : "IMPOSSIBLE") << endl;
typedef long long ll;

void CINT() {}
template <class Head, class... Tail> void CINT(Head &&head, Tail &&...tail) {
  cin >> head;
  CINT(move(tail)...);
}
#define CIN(...)                                                               \
  int __VA_ARGS__;                                                             \
  CINT(__VA_ARGS__)
#define LCIN(...)                                                              \
  ll __VA_ARGS__;                                                              \
  CINT(__VA_ARGS__)
#define SCIN(...)                                                              \
  string __VA_ARGS__;                                                          \
  CINT(__VA_ARGS__)

const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAX_N = 1e5 + 1;

int N, M;

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

  cin >> N >> M;
  set<int> from;
  set<int> to;
  REP(i, M) {
    CIN(a, b);
    if (a == 1) {
      from.erase(b);
      from.insert(b);
    }
    if (b == N) {
      to.erase(a);
      to.insert(a);
    }
  }

  bool ok = false;
  EACH(t, to) {
    EACH(f, from) {
      if (t == f)
        ok = true;
      if (ok)
        break;
    }
    if (ok)
      break;
  }

  OK(ok);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RFOR(i, a, b) for (int i = (int)b - 1; i >= (int)a; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define debug(x) cerr << #x << ":" << x << endl;
#define OK(ok) cout << (ok ? "POSSIBLE" : "IMPOSSIBLE") << endl;
typedef long long ll;

void CINT() {}
template <class Head, class... Tail> void CINT(Head &&head, Tail &&...tail) {
  cin >> head;
  CINT(move(tail)...);
}
#define CIN(...)                                                               \
  int __VA_ARGS__;                                                             \
  CINT(__VA_ARGS__)
#define LCIN(...)                                                              \
  ll __VA_ARGS__;                                                              \
  CINT(__VA_ARGS__)
#define SCIN(...)                                                              \
  string __VA_ARGS__;                                                          \
  CINT(__VA_ARGS__)

const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAX_N = 1e5 + 1;

int N, M;

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

  cin >> N >> M;
  set<int> from;
  set<int> to;
  REP(i, M) {
    CIN(a, b);
    if (a == 1) {
      from.erase(b);
      from.insert(b);
    }
    if (b == N) {
      to.erase(a);
      to.insert(a);
    }
  }

  bool ok = false;
  EACH(t, to) {
    if (from.find(t) != from.end())
      ok = true;
    if (ok)
      break;
  }

  OK(ok);

  return 0;
}