Problem p01820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAX 2048
#define inf 1 << 29
#define linf 1e16
#define eps (1e-8)
#define mod 1000000007
#define pi acos(-1)
#define phi (1.0 + sqrt(5)) / 2.0
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pd(a) printf("%.10f\n", (double)(a))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a)-1; (b) <= i; i--)
#define equals(a, b) (fabs((a) - (b)) < eps)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef vector<int> vi;
typedef vector<pii> vpi;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};

int n;
vector<pii> e[MAX];
int a[MAX], b[MAX];
string c[MAX];
bool used[MAX];

int dfs(int v) {
  int res = 1;
  used[v] = true;
  FOR(i, 0, e[v].size()) {
    int next = e[v][i].s;
    if (!used[next]) {
      res += dfs(next);
      break;
    }
  }
  return res;
}

int solve() {
  int res = 0;
  FOR(i, 0, n) {
    FOR(j, 0, n) {
      if (i == j)
        continue;
      if (c[i] == ">" && b[i] == b[j] && a[i] < a[j])
        e[i].pb(mp(a[j] - a[i], j));
      if (c[i] == "<" && b[i] == b[j] && a[j] < a[i])
        e[i].pb(mp(a[i] - a[j], j));
      if (c[i] == "v" && a[i] == a[j] && b[i] < b[j])
        e[i].pb(mp(b[j] - b[i], j));
      if (c[i] == "^" && a[i] == a[j] && b[j] < b[i])
        e[i].pb(mp(b[i] - b[j], j));
    }
    sort(all(e[i]));
  }
  FOR(i, 0, n) {
    fill(used, used + MAX, false);
    res = max(res, dfs(i));
  }
  return res;
}

int main() {
  cin >> n;
  FOR(i, 0, n) cin >> a[i] >> b[i] >> c[i];
  cout << solve() << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MAX 3003
#define inf 1 << 29
#define linf 1e16
#define eps (1e-8)
#define mod 1000000007
#define pi acos(-1)
#define phi (1.0 + sqrt(5)) / 2.0
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pd(a) printf("%.10f\n", (double)(a))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a)-1; (b) <= i; i--)
#define equals(a, b) (fabs((a) - (b)) < eps)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef vector<int> vi;
typedef vector<pii> vpi;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};

int n;
vector<pii> e[MAX];
int a[MAX], b[MAX];
string c[MAX];
bool used[MAX];

int dfs(int v) {
  int res = 1;
  used[v] = true;
  FOR(i, 0, e[v].size()) {
    int next = e[v][i].s;
    if (!used[next]) {
      res += dfs(next);
      break;
    }
  }
  return res;
}

int solve() {
  int res = 0;
  FOR(i, 0, n) {
    FOR(j, 0, n) {
      if (i == j)
        continue;
      if (c[i] == ">" && b[i] == b[j] && a[i] < a[j])
        e[i].pb(mp(a[j] - a[i], j));
      if (c[i] == "<" && b[i] == b[j] && a[j] < a[i])
        e[i].pb(mp(a[i] - a[j], j));
      if (c[i] == "v" && a[i] == a[j] && b[i] < b[j])
        e[i].pb(mp(b[j] - b[i], j));
      if (c[i] == "^" && a[i] == a[j] && b[j] < b[i])
        e[i].pb(mp(b[i] - b[j], j));
    }
    sort(all(e[i]));
  }
  FOR(i, 0, n) {
    fill(used, used + MAX, false);
    res = max(res, dfs(i));
  }
  return res;
}

int main() {
  cin >> n;
  FOR(i, 0, n) cin >> a[i] >> b[i] >> c[i];
  cout << solve() << endl;

  return 0;
}