alexjercan/bugnet huggingface dataset:
Problem p02243 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repR(i, n) for (int i = (n)-1; i > -1; i--)
#define rep1(i, n) for (int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for (int i = (n); i > 0; i--)
using namespace std;
static const int MAX = 10010;
static const int INF = INT_MAX;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

struct Node {
  int v, c;
};

bool operator>(const Node &v1, const Node &v2) { return v1.c > v2.c; }

vector<Node> adj[MAX];
int color[MAX];
int d[MAX];
int n;

void dijkstra(int s) {
  rep(u, n) {
    color[u] = WHITE;
    d[u] = INF;
  }
  d[s] = 0;

  priority_queue<Node, vector<Node>, greater<Node>> PQ;
  PQ.push(Node{s, 0});

  while (!PQ.empty()) {
    Node n = PQ.top();
    PQ.pop();
    int u = n.v;
    int c = n.c;

    color[u] = BLACK;

    if (d[u] < c)
      continue;

    rep(i, adj[u].size()) {
      Node nextNode = adj[u][i];
      int v = nextNode.v;
      if (color[v] != BLACK) {
        if (d[u] + nextNode.c < d[v]) {
          d[v] = d[u] + nextNode.c;
          color[v] = GRAY;
          PQ.push(Node{v, d[v]});
        }
      }
    }
  }
}

int main() {
  cin >> n;
  rep(i, n) {
    int u, k;
    cin >> u >> k;
    rep(j, k) {
      int v, c;
      cin >> v >> c;
      adj[u].push_back(Node{v, c});
    }
  }
  dijkstra(0);
  rep(v, n) cout << v << " " << d[v] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repR(i, n) for (int i = (n)-1; i > -1; i--)
#define rep1(i, n) for (int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for (int i = (n); i > 0; i--)
using namespace std;
static const int MAX = 10010;
static const int INF = INT_MAX;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

struct Node {
  int v, c;
};

bool operator>(const Node &v1, const Node &v2) { return v1.c > v2.c; }

static const int N = 2010;
vector<Node> adj[MAX];
int color[N];
int d[N];
int n;

void dijkstra(int s) {
  rep(u, n) {
    color[u] = WHITE;
    d[u] = INF;
  }
  d[s] = 0;

  priority_queue<Node, vector<Node>, greater<Node>> PQ;
  PQ.push(Node{s, 0});

  while (!PQ.empty()) {
    Node n = PQ.top();
    PQ.pop();
    int u = n.v;
    int c = n.c;

    color[u] = BLACK;

    if (d[u] < c)
      continue;

    rep(i, adj[u].size()) {
      Node nextNode = adj[u][i];
      int v = nextNode.v;
      if (color[v] != BLACK) {
        if (d[u] + nextNode.c < d[v]) {
          d[v] = d[u] + nextNode.c;
          color[v] = GRAY;
          PQ.push(Node{v, d[v]});
        }
      }
    }
  }
}

int main() {
  cin >> n;
  rep(i, n) {
    int u, k;
    cin >> u >> k;
    rep(j, k) {
      int v, c;
      cin >> v >> c;
      adj[u].push_back(Node{v, c});
    }
  }
  dijkstra(0);
  rep(v, n) cout << v << " " << d[v] << endl;
  return 0;
}
Problem p02243 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

#define INF 999999

int n, k;
int color[10010], d[10010];
vector<pair<int, int>> adj[INF];

int dijkstra(int s) {
  priority_queue<pair<int, int>> q;
  q.push(make_pair(0, s));
  for (int i = 0; i < n; i++) {
    d[i] = INF; // 距离初始化
  }
  d[s] = 0; // 第一个点到它本身距离是零

  while (!q.empty()) {
    pair<int, int> t = q.top();
    q.pop();
    int u = t.second;
    color[u] = 1;

    for (int i = 0; i < adj[u].size(); i++) {
      int v = adj[u][i].first;
      if (color[v] == 1) // 被染过色了，跳过
        continue;
      if (d[v] > d[u] + adj[u][i].second) {
        d[v] = d[u] + adj[u][i].second;
        q.push(make_pair(-d[v], v));
      }
    }
  }
  return 0;
}

int main(void) {
  scanf("%d", &n);
  int u, k, v, c;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &u, &k);
    for (int j = 0; j < k; j++) {
      scanf("%d%d", &c, &v);
      adj[u].push_back(make_pair(c, v));
    }
  }
  dijkstra(0);

  for (int i = 0; i < n; i++) {
    if (d[i] != INF)
      printf("%d %d", i, d[i]);
    printf("\\n");
  }
  return 0;
}


Error: MLE

Fixed submission:

//
//  main.cpp
//  单元最短路径
//
//  Created by 陆昱珉 on 2018/12/26.
//  Copyright © 2018 Lu. All rights reserved.
//

// dijkstra algorithm

#include <stdio.h>
#define INF 999999

int n, k;
int color[10010], d[10010];
int map[10010][10010];

int dijkstra(int s) {
  for (int i = 0; i < n; i++) {
    d[i] = INF;
  }
  d[s] = 0;
  while (1) {
    int min = INF;
    int u = -1;
    for (int i = 0; i < n; i++) {
      if (min > d[i] && color[i] != 1) {
        u = i;
        min = d[i];
      }
    }
    if (u == -1)
      break;
    color[u] = 1; // 染色，以防后面重复使用
    for (int i = 0; i < n; i++) {
      if (color[i] != 1 && map[u][i] != INF)
        if (d[i] > d[u] + map[u][i])
          d[i] = d[u] + map[u][i];
    }
  }
  return 0;
}

int main(void) {
  int x, y, v;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      map[i][j] = INF;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &x, &k);
    for (int j = 0; j < k; j++) {
      scanf("%d%d", &y, &v);
      map[x][y] = v;
    }
  }
  dijkstra(0);

  for (int i = 0; i < n; i++) {
    if (d[i] != INF)
      printf("%d %d", i, d[i]);
    printf("\\n");
  }
  return 0;
}
Problem p02243 (C++) — buggy submission, verdict: Time Limit Exceeded

// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_12_C: Single Source Shortest Path II

#include <algorithm>
#include <cstdio>
#include <queue>
#include <utility>
#include <vector>

using uint = unsigned int;

class Graph {
  using vc_t = std::pair<uint, uint>;
  std::vector<std::vector<vc_t>> g_;

public:
  Graph(const uint n) { g_.resize(n); }

  void push_to(const uint u, const uint v, const uint c) {
    g_[u].push_back(std::make_pair(v, c));
  }

  void dijkstra(uint *dist) {
    auto n = g_.size();
    const uint INFINIT = 100000 * (n - 1) + 1;
    std::fill_n(dist, n, INFINIT);
    auto cmp = [](vc_t left, vc_t right) { return left.second > right.second; };
    std::priority_queue<vc_t, std::vector<vc_t>, decltype(cmp)> q(cmp);

    q.push(vc_t(0, 0));
    dist[0] = 0u;
    while (!q.empty()) {
      auto bvc = q.top();
      q.pop();
      auto v = bvc.first;
      auto c = bvc.second;
      if (dist[v] < c) {
        continue;
      }
      dist[v] = c;
      for (auto vc : g_[v]) {
        q.push(std::make_pair(vc.first, vc.second + c));
      }
    }
  }
};

int main() {
  uint n;
  std::scanf("%u", &n);

  Graph g(n);

  uint u, k, v, c;
  for (auto i = 0u; i < n; ++i) {
    std::scanf("%u %u", &u, &k);
    for (auto j = 0u; j < k; ++j) {
      std::scanf("%u %u", &v, &c);
      g.push_to(u, v, c);
    }
  }

  uint dist[n];
  g.dijkstra(dist);
  for (auto i = 0u; i < n; ++i) {
    std::printf("%u %u\\n", i, dist[i]);
  }
}

// eof

Error: TLE

Fixed submission:

// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_12_C: Single Source Shortest Path II

#include <algorithm>
#include <cstdio>
#include <queue>
#include <utility>
#include <vector>

using uint = unsigned int;

class Graph {
  using vc_t = std::pair<uint, uint>;
  std::vector<std::vector<vc_t>> g_;

public:
  Graph(const uint n) { g_.resize(n); }

  void push_to(const uint u, const uint v, const uint c) {
    g_[u].push_back(std::make_pair(v, c));
  }

  void dijkstra(uint *dist) {
    auto n = g_.size();
    const uint INFINIT = 100000 * (n - 1) + 1;
    std::fill_n(dist, n, INFINIT);
    auto cmp = [](vc_t left, vc_t right) { return left.second < right.second; };
    std::priority_queue<vc_t, std::vector<vc_t>, decltype(cmp)> q(cmp);

    q.push(vc_t(0, 0));
    dist[0] = 0u;
    while (!q.empty()) {
      auto bvc = q.top();
      q.pop();
      auto v = bvc.first;
      auto c = bvc.second;
      if (dist[v] < c) {
        continue;
      }
      dist[v] = c;
      for (auto vc : g_[v]) {
        q.push(std::make_pair(vc.first, vc.second + c));
      }
    }
  }
};

int main() {
  uint n;
  std::scanf("%u", &n);

  Graph g(n);

  uint u, k, v, c;
  for (auto i = 0u; i < n; ++i) {
    std::scanf("%u %u", &u, &k);
    for (auto j = 0u; j < k; ++j) {
      std::scanf("%u %u", &v, &c);
      g.push_to(u, v, c);
    }
  }

  uint dist[n];
  g.dijkstra(dist);
  for (auto i = 0u; i < n; ++i) {
    std::printf("%u %u\\n", i, dist[i]);
  }
}

// eof
Problem p02244 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string.h>

namespace {

const int N = 8;
// the positions of k-queens are fixed.
// Once initialized, the values remain unchanged.
bool row[N] = {false};

bool col[N] = {false};
bool down[2 * N - 1] = {false};
bool up[2 * N - 1] = {false};
int pos[N];

void Print() {
  for (int r = 0; r < N; ++r) {
    const int qc = pos[r];
    for (int c = 0; c < N; ++c) {
      if (c == qc)
        printf("Q");
      else
        printf(".");
    }
    printf("\\n");
  }
}

// OK returns true if we can put a queen at (r, c)
inline bool OK(int r, int c) {
  return !col[c] && !down[r - c + N - 1] && !up[r + c];
}

inline void Update(int r, int c) {
  pos[r] = c;
  col[c] = true;
  down[r - c + N - 1] = true;
  up[r + c] = true;
}

inline void Cancel(int r, int c) {
  pos[r] = -1;
  col[c] = false;
  down[r - c + N - 1] = false;
  up[r + c] = false;
}

bool Put(int r) {
  if (row[r])
    return r < 7 ? Put(r + 1) : true;

  for (int c = 0; c < 8; ++c) {
    if (!OK(r, c))
      continue;

    Update(r, c);

    if (r == 7)
      return true;

    if (Put(r + 1))
      return true;
    else
      Cancel(r, c);
  }
  return false;
}

} // namespace

int main() {
  int k;
  std::cin >> k;

  memset(pos, -1, sizeof(int) * N);
  for (int i = 0; i < k; ++i) {
    int r, c;
    std::cin >> r >> c;
    Update(r, c);
    row[r] = true;
  }

  if (!Put(0)) {
    std::cerr << "ERROR: not found" << std::endl;
    Print();
    std::exit(1);
  }

  Print();

  return 0;
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string.h>

namespace {

const int N = 8;
// the positions of k-queens are fixed.
// Once initialized, the values remain unchanged.
bool row[N] = {false};

bool col[N] = {false};
bool down[2 * N - 1] = {false};
bool up[2 * N - 1] = {false};
int pos[N];

void Print() {
  for (int r = 0; r < N; ++r) {
    const int qc = pos[r];
    for (int c = 0; c < N; ++c) {
      if (c == qc)
        printf("Q");
      else
        printf(".");
    }
    printf("\\n");
  }
}

// OK returns true if we can put a queen at (r, c)
inline bool OK(int r, int c) {
  return !col[c] && !down[r - c + N - 1] && !up[r + c];
}

inline void Update(int r, int c) {
  pos[r] = c;
  col[c] = true;
  down[r - c + N - 1] = true;
  up[r + c] = true;
}

inline void Cancel(int r, int c) {
  pos[r] = -1;
  col[c] = false;
  down[r - c + N - 1] = false;
  up[r + c] = false;
}

bool Put(int r) {
  if (row[r])
    return Put(r + 1);

  for (int c = 0; c < 8; ++c) {
    if (!OK(r, c))
      continue;

    Update(r, c);

    if (r == 7)
      return true;

    if (Put(r + 1))
      return true;
    else
      Cancel(r, c);
  }
  return false;
}

} // namespace

int main() {
  int k;
  std::cin >> k;

  memset(pos, -1, sizeof(int) * N);
  for (int i = 0; i < k; ++i) {
    int r, c;
    std::cin >> r >> c;
    Update(r, c);
    row[r] = true;
  }

  if (!Put(0)) {
    std::cerr << "ERROR: not found" << std::endl;
    Print();
    std::exit(1);
  }

  Print();

  return 0;
}
Problem p02244 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;

const int INF = -100;
int k, r, c;
int a[8] = {INF, INF, INF, INF, INF, INF, INF, INF};
bool b[8] = {0, 0, 0, 0, 0, 0, 0, 0};

bool check() {
  for (int i = 0; i < 7; i++) {
    for (int j = i + 1; j < 8; j++)
      if (a[i] == a[j] || j - i == abs(a[i] - a[j]))
        return false;
  }
  return true;
}

void set(int i) {
  if (i == 8) {
    if (check()) {
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
          if (a[i] == j)
            cout << "Q";
          else
            cout << ".";
        }
        cout << endl;
      }
    }
    return;
  }

  if (b[i])
    set(i + 1);
  else {
    for (int j = 0; j < 8; j++) {
      a[i] = j;
      set(i + 1);
    }
  }
}

int main() {
  cin >> k;

  for (int i = 0; i < k; i++) {
    cin >> r >> c;
    a[r] = c;
    b[r] = 1;
  }

  set(0);

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool a[8][8];
int rv[8][8];

void flag(int rr, int cc) {
  if (rr > -1 && cc > -1) {
    for (int j = 0; j < 8; j++) {
      rv[rr][j] = 0;
      rv[j][cc] = 0;
      if (cc - rr + j > -1 && cc - rr + j < 8)
        rv[j][cc - rr + j] = 0;
      if (cc + rr - j > -1 && cc + rr - j < 8)
        rv[j][cc + rr - j] = 0;
    }
  }
}

void rrv() {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      int ctr = 0;
      if (rv[i][j] != 0) {
        for (int k = 0; k < 8; k++) {
          if (rv[i][k] != 0)
            ctr++;
          if (rv[k][j] != 0)
            ctr++;
          if (j - i + k > -1 && j - i + k < 8 && rv[k][j - i + k] != 0)
            ctr++;
          if (j + i - k > -1 && j + i - k < 8 && rv[k][j + i - k] != 0)
            ctr++;
        }
        rv[i][j] = ctr - 3;
      }
    }
  }
}

int main() {
  int k;
  int r[8], c[8];

  cin >> k;

  for (int i = 0; i < k; i++)
    cin >> r[i] >> c[i];

  int u = 0, t = k;

  while (t < 8) {

    for (int i = 0; i < 8; i++)
      for (int j = 0; j < 8; j++) {
        a[i][j] = false;
        rv[i][j] = 30;
      }

    for (int i = 0; i < k; i++)
      a[r[i]][c[i]] = true;

    for (int i = 0; i < k; i++)
      flag(r[i], c[i]);
    rrv();

    while (u < 30) {
      int ik, jk;
      u = 30;
      int w = 0;

      for (int i = 0; i < 8; i++)
        for (int j = 0; j < 8; j++)
          if (rv[i][j] > 0) {
            if (u > rv[i][j]) {
              u = rv[i][j];
              ik = i;
              jk = j;
            } else if (u == rv[i][j]) {
              if (w == 0) {
                ik = i;
                jk = j;
                w = 1;
              } else
                w = 0;
            }
          }

      if (u < 30) {
        a[ik][jk] = true;
        flag(ik, jk);
        rrv();
        t++;
      }
    }
  }

  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      if (a[i][j])
        cout << "Q";
      else
        cout << ".";
    }
    cout << endl;
  }

  return 0;
}
Problem p02244 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;

  int x, y;

  vector<int> y_x(8, -1);

  for (int i = 0; i < N; i++) {
    cin >> y >> x;

    y_x[y] = x;
  }

  vector<int> perm(8);

  for (int i = 0; i < 8; i++) {
    perm[i] = i;
  }

  bool ok = true;

  do {
    ok = true;

    for (int y = 0; y < 8; y++) {
      if (y_x[y] != -1 && perm[y] != y_x[y]) {
        ok = false;
      }
    }

    if (ok == true) {
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
          if (i != j) {
            if (i - perm[i] == j - perm[j] || i + perm[i] == j + perm[j]) {
              ok = false;
            }
          }
        }
      }
    }

    if (ok == true) {
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
          if (perm[i] == j) {
            cout << 'Q';
          } else {
            cout << '.';
          }
        }

        cout << endl;
      }
    }
  } while (next_permutation(perm.begin(), perm.end()));

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <queue>

using namespace std;

int main() {
  char Query[7];
  int Number;

  priority_queue<int> que;

  while (Query[2] != 'd') {
    scanf("%s", Query);

    if (Query[0] == 'i') {
      scanf("%d", &Number);
      que.push(Number);
    }
    if (Query[1] == 'x') {
      printf("%d\\n", que.top());
      que.pop();
    }
  }

  return 0;
}
Problem p02244 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
vector<vector<int>> ans;

vector<vector<int>> check(vector<vector<int>> b, int r, int c) {
  if (b[r][c] == -1)
    return b;
  vector<vector<int>> bb = b;
  for (int d = 0; d < 8; d++) {
    int x = c;
    int y = r;
    while (true) {
      x += dx[d];
      y += dy[d];
      if (x < 0 || y < 0 || x >= 8 || y >= 8)
        break;
      if (bb[y][x] == 1) {
        return b;
      }
      bb[y][x] = -1;
    }
  }
  bb[r][c] = 1;
  return bb;
}
bool f = false;
vector<vector<int>> dfs(vector<vector<int>> b, int q) {
  if (f)
    return b;
  if (q == 8) {
    f = true;
    ans = b;
    return b;
  }
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      vector<vector<int>> bb = check(b, i, j);
      if (b == bb)
        continue;
      dfs(bb, q + 1);
    }
  }
  return b;
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> b(8, vector<int>(8, 0));
  for (int i = 0; i < n; i++) {
    int r, c;
    cin >> r >> c;
    b[r][c] = 1;
    b = check(b, r, c);
  }
  dfs(b, n);
  for (int i = 0; i < ans.size(); i++) {
    for (int j = 0; j < ans[i].size(); j++) {
      if (ans[i][j] == -1)
        cout << ".";
      else if (ans[i][j] == 1)
        cout << "Q";
      else
        cout << ".";
    }
    cout << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
vector<vector<int>> ans;

vector<vector<int>> check(vector<vector<int>> b, int r, int c) {
  if (b[r][c] == -1)
    return b;
  vector<vector<int>> bb = b;
  for (int d = 0; d < 8; d++) {
    int x = c;
    int y = r;
    while (true) {
      x += dx[d];
      y += dy[d];
      if (x < 0 || y < 0 || x >= 8 || y >= 8)
        break;
      if (bb[y][x] == 1) {
        return b;
      }
      bb[y][x] = -1;
    }
  }
  bb[r][c] = 1;
  return bb;
}
bool f = false;
vector<vector<int>> dfs(vector<vector<int>> b, int q) {
  if (f)
    return b;
  if (q == 8) {
    f = true;
    ans = b;
    return b;
  }
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      vector<vector<int>> bb = check(b, i, j);
      if (b == bb)
        continue;
      dfs(bb, q + 1);
    }
  }
  return b;
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> b(8, vector<int>(8, 0));
  for (int i = 0; i < n; i++) {
    int r, c;
    cin >> r >> c;
    b[r][c] = 1;
    b = check(b, r, c);
  }
  dfs(b, n);
  for (int i = 0; i < ans.size(); i++) {
    for (int j = 0; j < ans[i].size(); j++) {
      if (ans[i][j] == -1)
        cerr << ".";
      else if (ans[i][j] == 1)
        cerr << "Q";
      else
        cerr << ".";
    }
    cerr << endl;
  }

  return 0;
}
Problem p02244 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cassert>
#include <iostream>
using namespace std;
#define N 8
#define FREE -1
#define NOT_FREE 1
int row[N], col[N], dpos[2 * N - 1], dneg[2 * N - 1];
bool X[N][N];

void initialize() {
  for (int i = 0; i < N; i++) {
    row[i] = FREE, col[i] = FREE;
  }
  for (int i = 0; i < 2 * N - 1; i++) {
    dpos[i] = FREE;
    dneg[i] = FREE;
  }
}

void printBoard() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (X[i][j]) {
        if (row[i] != j)
          return;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cout << ((row[i] == j) ? "Q" : ".");
    }
    cout << endl;
  }
}

void recursive(int i) {
  if (i == N) {
    printBoard();
    return;
  }
  for (int j = 0; j < N; j++) {
    if (NOT_FREE == col[j] || NOT_FREE == dpos[i + j] ||
        NOT_FREE == dneg[i - j + N - 1])
      continue;

    row[i] = j;
    col[j] = dpos[i + j] = dneg[i - j + N - 1] = NOT_FREE;
    recursive(i + 1);
    row[i] = col[j] = dpos[i + j] = dneg[i - j + N - 1] = FREE;
  }
}

int main() {
  initialize();
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      X[i][j] = false;
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    int r, c;
    cin >> r >> c;
    X[r][c] = true;
  }
  recursive(0);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>
#define N 8
int n, book[N], lie[N], xx[2 * N - 1], yy[2 * N - 1];
char map[N][N];
void dfs(int i) {
  if (i > N)
    return;
  if (i == N) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++)
        printf("%c", map[j][k]);
      printf("\\n");
    }
    return;
  }
  int j;
  if (book[i] == 0) {
    int flag = 0;
    for (j = 0; j < N; j++) {
      if (lie[j] == 0 && xx[i + j] == -1 && yy[i - j + 7] == -1) {
        flag = 1;
        lie[j] = 1;
        xx[i + j] = 1;
        yy[i - j + 7] = 1;
        map[i][j] = 'Q';
        dfs(i + 1);
        lie[j] = 0;
        xx[i + j] = -1;
        yy[i - j + 7] = -1;
        map[i][j] = '.';
      }
    }
    if (flag == 0)
      return;
  } else if (i < 8)
    dfs(i + 1);
}

int main() {
  while (scanf("%d", &n) && n) {
    memset(book, 0, sizeof(book));
    memset(lie, 0, sizeof(lie));
    memset(xx, -1, sizeof(xx));
    memset(yy, -1, sizeof(yy));
    int i, j, a, b;
    for (i = 0; i < N; i++)
      for (j = 0; j < N; j++)
        map[i][j] = '.';
    for (i = 0; i < n; i++) {
      scanf("%d%d", &a, &b);
      book[a] = 1;
      lie[b] = 1;
      xx[a + b] = 1;
      yy[a - b + 7] = 1;
      map[a][b] = 'Q';
    }
    dfs(0);
  }
  return 0;
}