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

#include <cstdio>
#include <cstdlib>
#include <map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

struct Point {
  int x, y;
  bool operator<(const Point &p) const {
    return make_pair(x, y) < make_pair(p.x, p.y);
  }
};

typedef pair<Point, Point> Futon;

int n, mark[40000];
vector<int> adj[40000];

bool issame(int u, int v) { return abs(u - v) == n; }

bool dfs(int u) {
  rep(i, adj[u].size()) {
    int v = adj[u][i];
    if (mark[v] == -1) {
      if (issame(u, v))
        mark[v] = 1 - mark[u];
      else
        mark[v] = mark[u];
      if (!dfs(v))
        return false;
    } else {
      if (!issame(u, v) && mark[u] != mark[v] ||
          issame(u, v) && mark[u] == mark[v])
        return false;
    }
  }

  return true;
}

int main() {
  for (; scanf("%d", &n), n;) {
    Point futon[40000];
    map<Point, int> f;
    rep(j, n) {
      int x, y;
      char dir;
      scanf("%d%d %c", &x, &y, &dir);

      futon[j] = (Point){x, y};
      if (dir == 'x')
        futon[n + j] = (Point){x + 1, y};
      if (dir == 'y')
        futon[n + j] = (Point){x, y + 1};

      f[futon[j]] = j;
      f[futon[n + j]] = n + j;
    }

    rep(u, 2 * n) {
      adj[u].clear();
      rep(k, 4) {
        int x = futon[u].x + dx[k];
        int y = futon[u].y + dy[k];
        Point p = {x, y};
        if (f.count(p))
          adj[u].push_back(f[p]);
      }
    }

    bool ok = true;
    rep(u, 2 * n) mark[u] = -1;
    rep(u, 2 * n) if (mark[u] == -1) {
      mark[u] = 0;
      if (!dfs(u)) {
        ok = false;
        break;
      }
    }

    puts(ok ? "Yes" : "No");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

struct Point {
  int x, y;
  bool operator<(const Point &p) const {
    return make_pair(x, y) < make_pair(p.x, p.y);
  }
};

typedef pair<Point, Point> Futon;

int n, mark[40000];
vector<int> adj[40000];

// u Æ v ª¯¶ÓÆñÉ®·é©Ç¤©
bool issame(int u, int v) { return abs(u - v) == n; }

bool dfs(int u) {
  bool ok = true;
  rep(i, adj[u].size()) {
    int v = adj[u][i];
    if (mark[v] == -1) {
      if (issame(u, v))
        mark[v] = 1 - mark[u];
      else
        mark[v] = mark[u];
      if (!dfs(v)) {
        ok = false;
        break;
      }
    } else {
      if (!issame(u, v) && mark[u] != mark[v]) {
        ok = false;
        break;
      }
    }
  }

  mark[u] = -1;
  return ok;
}

int main() {
  for (; scanf("%d", &n), n;) {
    Point futon[40000];
    map<Point, int> f;
    rep(j, n) {
      int x, y;
      char dir;
      scanf("%d%d %c", &x, &y, &dir);

      futon[j] = (Point){x, y};
      if (dir == 'x')
        futon[n + j] = (Point){x + 1, y};
      if (dir == 'y')
        futon[n + j] = (Point){x, y + 1};

      f[futon[j]] = j;
      f[futon[n + j]] = n + j;
    }

    rep(u, 2 * n) {
      adj[u].clear();
      rep(k, 4) {
        int x = futon[u].x + dx[k];
        int y = futon[u].y + dy[k];
        Point p = {x, y};
        if (f.count(p))
          adj[u].push_back(f[p]);
      }
    }

    bool ok = true;
    rep(u, 2 * n) mark[u] = -1;
    rep(u, 2 * n) if (mark[u] == -1) {
      mark[u] = 0;
      if (!dfs(u)) {
        ok = false;
        break;
      }
    }

    puts(ok ? "Yes" : "No");
  }

  return 0;
}