Problem p01296 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
#define X first
#define Y second
signed main() {
  int n;
  while (cin >> n, n) {
    P p[n];
    char d[n];
    for (int i = 0; i < n; i++)
      cin >> p[i].X >> p[i].Y >> d[i];
    map<P, int> m;
    for (int i = 0; i < n; i++)
      m[p[i]] = i;
    int used[n], visit[n];
    memset(used, 0, sizeof(used));
    bool f = 1;

    int ax1[] = {-1, 0, 1, 2, -2, -1, 0, 1};
    int ay1[] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int ad1[] = {1, 0, 1, 1, 1, 1, 0, 1};

    int ax2[] = {-1, 0, 1, 2, 2, 1, 0, -1};
    int ay2[] = {0, 1, 1, 0, -1, -2, -2, -1};
    int ad2[] = {0, 0, 1, 1, 0, 0, 1, 1};

    int ax3[] = {0, 1, 1, 1, 0, -1, -1, -1};
    int ay3[] = {2, 1, 0, -1, -2, -1, 0, 1};
    int ad3[] = {1, 1, 0, 1, 1, 1, 0, 1};

    int ax4[] = {-1, 0, 1, 1, 0, -1, -2, -2};
    int ay4[] = {-1, -1, 0, 1, 2, 2, 1, 0};
    int ad4[] = {1, 0, 0, 1, 1, 0, 0, 1};

    for (int i = 0; i < n; i++) {
      if (used[i])
        continue;
      bool ff = 0;
      for (int k = 0; k < 2; k++) {
        bool fff = 1;

        memset(visit, -1, sizeof(visit));
        queue<int> q;
        q.push(i);
        visit[i] = k;

        while (!q.empty()) {
          int t = q.front();
          q.pop();
          // cout<<t<<" "<<visit[t]<<endl;
          if (d[t] == 'x') {
            for (int j = 0; j < 8; j++) {
              int nx = p[t].X + ax1[j], ny = p[t].Y + ay1[j], nv = visit[t];
              if (!m.count(P(nx, ny)))
                continue;
              int u = m[P(nx, ny)];
              if (d[u] == 'y')
                continue;
              // cout<<"a1:"<<u<<endl;
              if (ad1[j])
                nv = !nv;
              if (~visit[u]) {
                if (visit[u] != nv) {
                  fff = 0;
                  goto END;
                }
              } else {
                visit[u] = nv;
                q.push(u);
              }
            }
            for (int j = 0; j < 8; j++) {
              int nx = p[t].X + ax2[j], ny = p[t].Y + ay2[j], nv = visit[t];
              if (!m.count(P(nx, ny)))
                continue;
              int u = m[P(nx, ny)];
              if (d[u] == 'x')
                continue;
              // cout<<"a2:"<<u<<endl;
              if (ad2[j])
                nv = !nv;
              if (~visit[u]) {
                if (visit[u] != nv) {
                  fff = 0;
                  goto END;
                }
              } else {
                visit[u] = nv;
                q.push(u);
              }
            }
          }

          if (d[t] == 'y') {
            for (int j = 0; j < 8; j++) {
              int nx = p[t].X + ax3[j], ny = p[t].Y + ay3[j], nv = visit[t];
              if (!m.count(P(nx, ny)))
                continue;
              int u = m[P(nx, ny)];
              if (d[u] == 'x')
                continue;
              // cout<<"a3:"<<u<<endl;
              if (ad3[j])
                nv = !nv;
              if (~visit[u]) {
                if (visit[u] != nv) {
                  fff = 0;
                  goto END;
                }
              } else {
                visit[u] = nv;
                q.push(u);
              }
            }
            for (int j = 0; j < 8; j++) {
              int nx = p[t].X + ax4[j], ny = p[t].Y + ay4[j], nv = visit[t];
              if (!m.count(P(nx, ny)))
                continue;
              int u = m[P(nx, ny)];
              if (d[u] == 'y')
                continue;
              // cout<<"a4:"<<u<<endl;
              if (ad4[j])
                nv = !nv;
              if (~visit[u]) {
                if (visit[u] != nv) {
                  fff = 0;
                  goto END;
                }
              } else {
                visit[u] = nv;
                q.push(u);
              }
            }
          }
        }

      END:

        // cout<<"fff:"<<fff<<endl;

        if (fff) {
          for (int j = 0; j < n; j++)
            if (~visit[j])
              used[j] = 1;
          ff = 1;
          break;
        }
      }
      if (!ff) {
        f = 0;
        break;
      }
    }
    cout << (f ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
#define X first
#define Y second
signed main() {
  int n;
  while (cin >> n, n) {
    P p[n];
    char d[n];
    for (int i = 0; i < n; i++)
      cin >> p[i].X >> p[i].Y >> d[i];
    map<P, int> m;
    for (int i = 0; i < n; i++)
      m[p[i]] = i;
    int used[n], visit[n];
    memset(used, 0, sizeof(used));
    bool f = 1;

    int ax1[] = {-1, 0, 1, 2, -2, -1, 0, 1};
    int ay1[] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int ad1[] = {1, 0, 1, 1, 1, 1, 0, 1};

    int ax2[] = {-1, 0, 1, 2, 2, 1, 0, -1};
    int ay2[] = {0, 1, 1, 0, -1, -2, -2, -1};
    int ad2[] = {0, 0, 1, 1, 0, 0, 1, 1};

    int ax3[] = {0, 1, 1, 1, 0, -1, -1, -1};
    int ay3[] = {2, 1, 0, -1, -2, -1, 0, 1};
    int ad3[] = {1, 1, 0, 1, 1, 1, 0, 1};

    int ax4[] = {-1, 0, 1, 1, 0, -1, -2, -2};
    int ay4[] = {-1, -1, 0, 1, 2, 2, 1, 0};
    int ad4[] = {1, 0, 0, 1, 1, 0, 0, 1};

    for (int i = 0; i < n; i++) {
      if (used[i])
        continue;
      bool ff = 0;
      for (int k = 0; k < 2; k++) {
        bool fff = 1;

        memset(visit, -1, sizeof(visit));
        queue<int> q;
        q.push(i);
        visit[i] = k;

        while (!q.empty()) {
          int t = q.front();
          q.pop();
          // cout<<t<<" "<<visit[t]<<endl;
          if (d[t] == 'x') {
            for (int j = 0; j < 8; j++) {
              int nx = p[t].X + ax1[j], ny = p[t].Y + ay1[j], nv = visit[t];
              if (!m.count(P(nx, ny)))
                continue;
              int u = m[P(nx, ny)];
              if (d[u] == 'y')
                continue;
              // cout<<"a1:"<<u<<endl;
              if (ad1[j])
                nv = !nv;
              if (~visit[u]) {
                if (visit[u] != nv) {
                  fff = 0;
                  goto END;
                }
              } else {
                visit[u] = nv;
                q.push(u);
              }
            }
            for (int j = 0; j < 8; j++) {
              int nx = p[t].X + ax2[j], ny = p[t].Y + ay2[j], nv = visit[t];
              if (!m.count(P(nx, ny)))
                continue;
              int u = m[P(nx, ny)];
              if (d[u] == 'x')
                continue;
              // cout<<"a2:"<<u<<endl;
              if (ad2[j])
                nv = !nv;
              if (~visit[u]) {
                if (visit[u] != nv) {
                  fff = 0;
                  goto END;
                }
              } else {
                visit[u] = nv;
                q.push(u);
              }
            }
          }

          if (d[t] == 'y') {
            for (int j = 0; j < 8; j++) {
              int nx = p[t].X + ax3[j], ny = p[t].Y + ay3[j], nv = visit[t];
              if (!m.count(P(nx, ny)))
                continue;
              int u = m[P(nx, ny)];
              if (d[u] == 'x')
                continue;
              // cout<<"a3:"<<u<<endl;
              if (ad3[j])
                nv = !nv;
              if (~visit[u]) {
                if (visit[u] != nv) {
                  fff = 0;
                  goto END;
                }
              } else {
                visit[u] = nv;
                q.push(u);
              }
            }
            for (int j = 0; j < 8; j++) {
              int nx = p[t].X + ax4[j], ny = p[t].Y + ay4[j], nv = visit[t];
              if (!m.count(P(nx, ny)))
                continue;
              int u = m[P(nx, ny)];
              if (d[u] == 'y')
                continue;
              // cout<<"a4:"<<u<<endl;
              if (ad4[j])
                nv = !nv;
              if (~visit[u]) {
                if (visit[u] != nv) {
                  fff = 0;
                  goto END;
                }
              } else {
                visit[u] = nv;
                q.push(u);
              }
            }
          }
        }

      END:

        // cout<<"fff:"<<fff<<endl;

        if (fff) {
          for (int j = 0; j < n; j++)
            if (~visit[j])
              used[i] = 1;
          ff = 1;
          break;
        }
      }
      if (!ff) {
        f = 0;
        break;
      }
    }
    cout << (f ? "Yes" : "No") << endl;
  }
  return 0;
}