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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int n;
P p[2][20100];
char d;
vector<P> g[20100];
int vis[20100];

void add_edge(int u, int v, int col) {
  g[v].push_back(P(u, col));
  g[u].push_back(P(v, col));
}

int md(P a, P b) { return abs(a.first - b.first) + abs(a.second - b.second); }

bool dfs(int v, int col) {
  if (vis[v] >= 0)
    return (vis[v] == col);
  vis[v] = col;

  for (int i = 0; i < g[v].size(); i++) {
    if (!dfs(g[v][i].first, col ^ g[v][i].second))
      return false;
  }
  return true;
}

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

  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> p[0][i].first >> p[0][i].second >> d;
      p[1][i].first = p[0][i].first + (d == 'x');
      p[1][i].second = p[0][i].second + (d == 'y');
    }

    for (int i = 0; i < n; i++)
      g[i].clear();

    int gs = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (md(p[0][i], p[0][j]) <= 3) {
          bool f = true;
          for (int ii = 0; f, ii < 2; ii++) {
            for (int jj = 0; f, jj < 2; jj++) {
              if (md(p[ii][i], p[jj][j]) == 1) {
                add_edge(i, j, ii != jj);
                gs += 2;
                f = false;
              }
            }
          }
        }
      }
    }

    bool flag = true;
    memset(vis, -1, sizeof(vis));

    for (int i = 0; i < n; i++) {
      if (vis[i] < 0)
        flag &= dfs(i, 0);
      if (!flag)
        break;
    }
    cout << (flag ? "Yes" : "No") << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int n;
P p[2][20100];
char d;
vector<P> g[20100];
int vis[20100];

void add_edge(int u, int v, int col) {
  g[v].push_back(P(u, col));
  g[u].push_back(P(v, col));
}

int md(P a, P b) { return abs(a.first - b.first) + abs(a.second - b.second); }

bool dfs(int v, int col) {
  vis[v] = col;

  for (int i = 0; i < g[v].size(); i++) {
    int nxt = g[v][i].first, color = g[v][i].second;
    if (vis[nxt] >= 0 && vis[nxt] != col ^ color)
      return false;
    if (vis[nxt] < 0 && !dfs(nxt, col ^ color))
      return false;
  }
  return true;
}

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

  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> p[0][i].first >> p[0][i].second >> d;
      p[1][i].first = p[0][i].first + (d == 'x');
      p[1][i].second = p[0][i].second + (d == 'y');
    }

    for (int i = 0; i < n; i++)
      g[i].clear();

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (i == j)
          continue;
        bool f = true;
        for (int ii = 0; f, ii < 2; ii++) {
          for (int jj = 0; f, jj < 2; jj++) {
            if (md(p[ii][i], p[jj][j]) == 1) {
              add_edge(i, j, ii != jj);
              f = false;
            }
          }
        }
      }
    }

    bool flag = true;
    memset(vis, -1, sizeof(vis));

    for (int i = 0; i < n; i++) {
      if (vis[i] < 0)
        flag &= dfs(i, 0);
      if (!flag)
        break;
    }
    cout << (flag ? "Yes" : "No") << endl;
  }
}