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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/

int N, M, A[9][9];
//---------------------------------------------------------------------------------------------------
int check(int y) {
  int win = 0, non = 0;
  rep(x, 0, N) {
    if (A[y][x] == 1)
      win++;
    else if (A[y][x] < 0 and y != x)
      non++;
  }
  return win <= (N - 1) / 2 and (N - 1) / 2 <= win + non;
}
//---------------------------------------------------------------------------------------------------
int ans = 0;
void dfs(int cu) {
  int x = cu % N;
  int y = cu / N;

  if (cu == N * N) {
    ans++;
    return;
  }

  if (x == y or 0 <= A[y][x]) {
    if (check(y))
      dfs(cu + 1);
    return;
  }

  A[y][x] = 1;
  A[x][y] = 0;
  if (check(y))
    dfs(cu + 1);

  A[y][x] = 0;
  A[x][y] = 1;
  if (check(y))
    dfs(cu + 1);

  A[y][x] = A[x][y] = -1;
}
//---------------------------------------------------------------------------------------------------
void _main() {
  while (cin >> N) {
    if (N == 0)
      return;
    cin >> M;
    rep(i, 0, N) rep(j, 0, N) A[i][j] = -1;
    rep(i, 0, M) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      A[a][b] = 1;
      A[b][a] = 0;
    }

    ans = 0;
    dfs(0);
    printf("%d\\n", ans);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/

int N, M, A[9][9];
//---------------------------------------------------------------------------------------------------
int check(int y) {
  int win = 0;
  rep(x, 0, N) if (A[y][x] == 1) win++;
  return win == (N - 1) / 2;
}
//---------------------------------------------------------------------------------------------------
int ans = 0;
void dfs(int cu) {
  int x = cu % N;
  int y = cu / N;

  if (x == 0 and 0 < y) {
    if (y == N) {
      if (check(y - 1))
        ans++;
      return;
    } else {
      if (!check(y - 1))
        return;
    }
  }

  if (x == y or 0 <= A[y][x])
    dfs(cu + 1);

  A[y][x] = 1;
  A[x][y] = 0;
  dfs(cu + 1);

  A[y][x] = 0;
  A[x][y] = 1;
  dfs(cu + 1);
}
//---------------------------------------------------------------------------------------------------
void _main() {
  while (cin >> N) {
    if (N == 0)
      return;
    cin >> M;
    rep(i, 0, N) rep(j, 0, N) A[i][j] = -1;
    rep(i, 0, M) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      A[a][b] = 1;
      A[b][a] = 0;
    }

    ans = 0;
    dfs(0);
    printf("%d\\n", ans);
  }
}