prithivMLmods/Coder-Stat huggingface dataset:
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

enum State {
  NOT,
  VISITED,
};

class E {
public:
  E(int i_in) : i(i_in) {
    for (int j = 0; j < 8; ++j) {
      s[j] = NOT;
    }
  }

  E(const E &e) {
    this->i = e.i;
    for (int j = 0; j < 8; ++j) {
      this->s[j] = e.s[j];
    }
  }

  int s[8];
  int i;
};

int main() {
  int N, M;
  std::cin >> N >> M;

  std::vector<int> v[N];
  int a, b;
  for (int i = 0; i < N; ++i) {
    std::cin >> a >> b;
    --a;
    --b;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  std::queue<E> que;
  que.push(E(0));
  int cnt = 0;
  while (que.empty() == false) {
    E e = que.front();
    que.pop();
    e.s[e.i] = VISITED;

    bool complete = true;
    for (int i = 0; i < N; ++i) {
      if (e.s[i] != VISITED) {
        complete = false;
        break;
      }
    }

    if (complete) {
      ++cnt;
      continue;
    }

    for (int d : v[e.i]) {
      if (e.s[d] == NOT) {
        E e1(e);
        e1.i = d;
        que.push(e1);
      }
    }
  }

  std::cout << cnt << std::endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

enum State {
  NOT,
  VISITED,
};

class E {
public:
  E(int i_in) : i(i_in) {
    for (int j = 0; j < 8; ++j) {
      s[j] = NOT;
    }
  }

  E(const E &e) {
    this->i = e.i;
    for (int j = 0; j < 8; ++j) {
      this->s[j] = e.s[j];
    }
  }

  int s[8];
  int i;
};

int main() {
  int N, M;
  std::cin >> N >> M;

  std::vector<int> v[N];
  int a, b;
  for (int i = 0; i < M; ++i) {
    std::cin >> a >> b;
    --a;
    --b;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  std::queue<E> que;
  que.push(E(0));
  int cnt = 0;
  while (que.empty() == false) {
    E e = que.front();
    que.pop();
    e.s[e.i] = VISITED;

    bool complete = true;
    for (int i = 0; i < N; ++i) {
      if (e.s[i] != VISITED) {
        complete = false;
        break;
      }
    }

    if (complete) {
      ++cnt;
      continue;
    }

    for (int d : v[e.i]) {
      if (e.s[d] == NOT) {
        E e1(e);
        e1.i = d;
        que.push(e1);
      }
    }
  }

  std::cout << cnt << std::endl;

  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

int n, m;
int A[8][8] = {};

int loop(int ind, vector<int> used) {
  int flag = 0;
  for (int i = 1; i < n; i++) {
    if (used[i] == 0) {
      flag = 1;
      break;
    }
  }
  if (flag == 0) {
    return 1;
  }
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (used[i] == 1)
      continue;
    if (A[ind][i] == 1) {
      used[i] = 1;
      ans += loop(i, used);
      used[i] = 0;
    }
  }
  return ans;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    A[a - 1][b - 1] = A[b - 1][a - 1] = 1;
  }
  int ans = 0;
  vector<int> used(n, 0);
  used[0] = 1;
  for (int i = 1; i < m; i++) {
    if (A[0][i] == 1) {
      used[i] = 1;
      ans += loop(i, used);
      used[i] = 0;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

int n, m;
int A[8][8] = {};

int loop(int ind, vector<int> used) {
  int flag = 0;
  for (int i = 1; i < n; i++) {
    if (used[i] == 0) {
      flag = 1;
      break;
    }
  }
  if (flag == 0) {
    return 1;
  }
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (used[i] == 1)
      continue;
    if (A[ind][i] == 1) {
      used[i] = 1;
      ans += loop(i, used);
      used[i] = 0;
    }
  }
  return ans;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    A[a - 1][b - 1] = A[b - 1][a - 1] = 1;
  }
  int ans = 0;
  vector<int> used(n, 0);
  used[0] = 1;
  for (int i = 1; i < n; i++) {
    if (A[0][i] == 1) {
      used[i] = 1;
      ans += loop(i, used);
      used[i] = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;
int n, m;
bool vi[100];
vector<vector<int>> g;
int ans = 0;

bool check() {
  bool ret = true;
  REP(i, n) if (!vi[i]) ret = false;
  return ret;
}

void dfs(int x) {
  if (vi[x])
    return;
  vi[x] = true;
  if (check())
    ++ans;

  for (auto &&e : g[x]) {
    dfs(e);
  }
  vi[x] = false;
  return;
}

int main(int argc, char const *argv[]) {
  cin >> n >> m;
  g.resize(n);
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  REP(i, 100) vi[i] = false;
  dfs(0);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;
int n, m;
bool vi[100];
vector<vector<int>> g;
int ans = 0;

bool check() {
  bool ret = true;
  REP(i, n) if (!vi[i]) ret = false;
  return ret;
}

void dfs(int x) {
  if (vi[x])
    return;
  vi[x] = true;
  if (check())
    ++ans;

  for (auto &&e : g[x]) {
    dfs(e);
  }
  vi[x] = false;
  return;
}

int main(int argc, char const *argv[]) {
  cin >> n >> m;
  g.resize(n);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  REP(i, 100) vi[i] = false;
  dfs(0);
  cout << ans << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (ll i = n; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const int INF = 1e9;
const ll LLINF = 1e16;

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int dfscnt(vector<vector<int>> tree, vector<int> color, int v) {
  color[v] = 1;
  int sum = 0;
  REP(i, tree[v].size()) {
    if (color[tree[v][i]] == 0) {
      sum += dfscnt(tree, color, tree[v][i]);
    }
  }
  bool flag = true;
  REP(i, color.size()) {
    if (color[i] == 0) {
      flag = false;
    }
  }
  if (flag) {
    return sum + 1;
  } else {
    return sum;
  }
}

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> tree(n);
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }

  cout << dfscnt(tree, vector<int>(n, 0), 0);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (ll i = n; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const int INF = 1e9;
const ll LLINF = 1e16;

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int dfscnt(vector<vector<int>> tree, vector<int> color, int v) {
  color[v] = 1;
  int sum = 0;
  REP(i, tree[v].size()) {
    if (color[tree[v][i]] == 0) {
      sum += dfscnt(tree, color, tree[v][i]);
    }
  }
  bool flag = true;
  REP(i, color.size()) {
    if (color[i] == 0) {
      flag = false;
    }
  }
  if (flag) {
    return sum + 1;
  } else {
    return sum;
  }
}

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> tree(n);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }

  cout << dfscnt(tree, vector<int>(n, 0), 0);

  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<bool>> g(m, vector<bool>(m, false));
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g.at(a).at(b) = g.at(b).at(a) = true;
  }
  vector<int> per(n);
  for (int i = 0; i < n; i++)
    per.at(i) = i;

  int cnt = 0;
  do {
    if (per.at(0) != 0)
      break;
    bool flag = true;
    for (int i = 0; i < n - 1; i++) {
      int from = per.at(i);
      int to = per.at(i + 1);
      if (!g.at(from).at(to))
        flag = false;
    }
    if (flag)
      cnt++;
  } while (next_permutation(per.begin(), per.end()));

  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<bool>> g(2 * m, vector<bool>(2 * m, false));
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g.at(a).at(b) = g.at(b).at(a) = true;
  }
  vector<int> per(n);
  for (int i = 0; i < n; i++)
    per.at(i) = i;

  int cnt = 0;
  do {
    if (per.at(0) != 0)
      break;
    bool flag = true;
    for (int i = 0; i < n - 1; i++) {
      int from = per.at(i);
      int to = per.at(i + 1);
      if (!g.at(from).at(to))
        flag = false;
    }
    if (flag)
      cnt++;
  } while (next_permutation(per.begin(), per.end()));

  cout << cnt << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int maxn = 8;
bool graph[maxn][maxn];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a - 1][b - 1] = graph[b - 1][a - 1] = true;
  }
  vector<int> v(n - 1);
  for (int i = 0; i < n - 1; i++) {
    v.at(i) = i + 1;
  }
  bool flag = 1;
  int count = 0;
  do {
    flag = 1;
    for (int j = 0; j < m - 2; j++) {
      if (graph[0][v.at(0)]) {
        if (graph[v.at(j)][v.at(j + 1)] == 0) {
          flag = 0;
        }
      } else {
        flag = 0;
      }
    }
    if (flag) {
      count++;
    }
  } while (next_permutation(v.begin(), v.end()));

  cout << count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int maxn = 8;
bool graph[maxn][maxn];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a - 1][b - 1] = graph[b - 1][a - 1] = true;
  }
  vector<int> v(n - 1);
  for (int i = 0; i < n - 1; i++) {
    v.at(i) = i + 1;
  }
  bool flag = 1;
  int count = 0;
  do {
    flag = 1;
    for (int j = 0; j < n - 2; j++) {
      if (graph[0][v.at(0)]) {
        if (graph[v.at(j)][v.at(j + 1)] == 0) {
          flag = 0;
        }
      } else {
        flag = 0;
      }
    }
    if (flag) {
      count++;
    }
  } while (next_permutation(v.begin(), v.end()));

  cout << count << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
const int maxn = 10000;
const int INF32 = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7 = 1'000'000'007;
const int MOD9 = 1'000'000'009;

void ERROR(int num) { printf("ERROR%d!\n", num); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[8] = {0, -1, 0, 1, 1, -1, 1, -1};

int N, M;
vector<vector<int>> G;

int main() {
  cin >> N >> M;
  G.resize(N);
  rep(i, N) G[i].resize(M);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a][b] = G[b][a] = 1;
  }

  vector<int> array(N);
  rep(i, N) array[i] = i;

  int cnt = 0;
  do {
    if (array[0] != 0)
      break;
    int ok = 1;
    rep(i, N) {
      int from = array[i];
      int to = array[i + 1];
      if (G[from][to] == 0)
        ok = 0;
    }
    if (ok)
      cnt++;
  } while (next_permutation(array.begin(), array.end()));

  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
const int maxn = 10000;
const int INF32 = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7 = 1'000'000'007;
const int MOD9 = 1'000'000'009;

void ERROR(int num) { printf("ERROR%d!\n", num); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[8] = {0, -1, 0, 1, 1, -1, 1, -1};

int N, M;
vector<vector<int>> G;

int main() {
  cin >> N >> M;
  G.resize(N);
  rep(i, N) G[i].resize(M);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a][b] = G[b][a] = 1;
  }

  vector<int> array(N);
  rep(i, N) array[i] = i;

  int cnt = 0;
  do {
    if (array[0] != 0)
      break;
    int ok = 1;
    for (int i = 0; i + 1 < N; i++) {
      int from = array[i];
      int to = array[i + 1];
      if (G[from][to] == 0)
        ok = 0;
    }
    if (ok)
      cnt++;
  } while (next_permutation(array.begin(), array.end()));

  cout << cnt << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, m;
  bool way[9][9] = {false};
  int64_t ans = 0;
  cin >> n >> m;
  vector<int> a(8), b(8);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    way[a[i]][b[i]] = true;
    way[b[i]][a[i]] = true;
  }
  vector<int> order(n);
  for (int i = 0; i < n; i++)
    order[i] = i + 1;
  do {
    if (order[0] != 1)
      break;
    bool exist = true;
    for (int i = 1; i < n && exist; i++) {
      if (!(way[order[i - 1]][order[i]]))
        exist = false;
    }
    if (exist)
      ans++;
  } while (next_permutation(order.begin(), order.end()));
  std::cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, m;
  bool way[9][9] = {false};
  int64_t ans = 0;
  cin >> n >> m;
  vector<int> a(30), b(30);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    way[a[i]][b[i]] = true;
    way[b[i]][a[i]] = true;
  }
  vector<int> order(n);
  for (int i = 0; i < n; i++)
    order[i] = i + 1;
  do {
    if (order[0] != 1)
      break;
    bool exist = true;
    for (int i = 1; i < n && exist; i++) {
      if (!(way[order[i - 1]][order[i]]))
        exist = false;
    }
    if (exist)
      ans++;
  } while (next_permutation(order.begin(), order.end()));
  std::cout << ans;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, m;
int fin = 1;
vector<int> v[10];

int dfs(int a, int bit) {
  if (bit == fin) {
    return 1;
  }

  int ans = 0;
  for (int i = 0; i < v[a].size(); i++) {
    if ((bit & (1 << v[a][i])) == 0) {
      ans += dfs(v[a][i], bit | (1 << v[a][i]));
    }
  }
  return ans;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    fin *= 2;
  fin--;
  int a, b;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  cout << dfs(0, 1) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n, m;
int fin = 1;
vector<int> v[10];

int dfs(int a, int bit) {
  if (bit == fin) {
    return 1;
  }

  int ans = 0;
  for (int i = 0; i < v[a].size(); i++) {
    if ((bit & (1 << v[a][i])) == 0) {
      ans += dfs(v[a][i], bit | (1 << v[a][i]));
    }
  }
  return ans;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    fin *= 2;
  fin--;
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  cout << dfs(0, 1) << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
void OnestrokePath(int n, vector<vector<int>> &ABC, vector<bool> &ok,
                   int &ans) {
  bool ok2 = true;
  for (int i = 0; i < ok.size(); i++) {
    if (ok[i] == false && i != n) {
      ok2 = false;
    }
  }
  if (ok2) {
    ans++;
    return;
  }
  ok[n] = true;
  for (int i = 0; i < ABC[n].size(); i++) {
    if (ABC[n][i] == true) {
      continue;
    }
    OnestrokePath(ABC[n][i], ABC, ok, ans);
  }
  ok[n] = false;
  return;
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> ABC(N);
  vector<bool> ok(N, false);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ABC[a].push_back(b);
    ABC[b].push_back(a);
  }
  int ans = 0;
  OnestrokePath(0, ABC, ok, ans);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
void OnestrokePath(int n, vector<vector<int>> &ABC, vector<bool> &ok,
                   int &ans) {
  bool ok2 = true;
  for (int i = 0; i < ok.size(); i++) {
    if (ok[i] == false && i != n) {
      ok2 = false;
    }
  }
  if (ok2) {
    ans++;
    return;
  }
  ok[n] = true;
  for (int i = 0; i < ABC[n].size(); i++) {
    if (ok[ABC[n][i]] == true) {
      continue;
    }
    OnestrokePath(ABC[n][i], ABC, ok, ans);
  }
  ok[n] = false;
  return;
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> ABC(N);
  vector<bool> ok(N, false);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ABC[a].push_back(b);
    ABC[b].push_back(a);
  }
  int ans = 0;
  OnestrokePath(0, ABC, ok, ans);
  cout << ans << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  const int MAX_N = 10;
  int N, M, E[MAX_N][MAX_N];
  vector<int> v;
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < M; ++j) {
      E[i][j] = 0;
    }
  }
  for (int i = 0; i < N; ++i) {
    v.push_back(i);
  }
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    E[a - 1][b - 1] = 1;
    E[b - 1][a - 1] = 1;
  }

  int ans = 0;
  do {
    if (v[0] != 0)
      break;
    // for (int i = 0; i < N; ++i){
    //     cout << v[i] + 1;
    // }cout << endl;

    bool f = true;
    for (int i = 0; i < N - 1; ++i) {
      if (E[v[i]][v[i + 1]] == 0) {
        f = false;
        break;
      }
    }
    if (f)
      ans++;

  } while (next_permutation(v.begin(), v.end()));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  const int MAX_N = 10;
  int N, M, E[MAX_N][MAX_N];
  vector<int> v;
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      E[i][j] = 0;
    }
  }
  for (int i = 0; i < N; ++i) {
    v.push_back(i);
  }
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    E[a - 1][b - 1] = 1;
    E[b - 1][a - 1] = 1;
  }

  int ans = 0;
  do {
    if (v[0] != 0)
      break;
    // for (int i = 0; i < N; ++i){
    //     cout << v[i] + 1;
    // }cout << endl;

    bool f = true;
    for (int i = 0; i < N - 1; ++i) {
      if (E[v[i]][v[i + 1]] == 0) {
        f = false;
        break;
      }
    }
    if (f)
      ans++;

  } while (next_permutation(v.begin(), v.end()));
  cout << ans << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); ++(i))
#define all(x) x.begin(), x.end()

int n, m, ans = 0;
vector<bool> passed;
vector<vector<int>> vec;

void dfs(int now, int cnt) {
  if (passed[now])
    return;
  if (cnt == n) {
    ++ans;
    return;
  }
  passed[now] = true;
  for (int x : vec[now]) {
    dfs(x, cnt + 1);
  }
  passed[now] = false;
  return;
}

int main() {
  cin >> n >> m;
  vec.resize(m + 1);
  rep(i, 1, m) {
    int a, b;
    cin >> a >> b;
    vec[a].push_back(b);
    vec[b].push_back(a);
  }
  passed.resize(n + 1);
  dfs(1, 1);
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); ++(i))
#define all(x) x.begin(), x.end()

int n, m, ans = 0;
vector<bool> passed;
vector<vector<int>> vec;

void dfs(int now, int cnt) {
  if (passed[now])
    return;
  if (cnt == n) {
    ++ans;
    return;
  }
  passed[now] = true;
  for (int x : vec[now]) {
    dfs(x, cnt + 1);
  }
  passed[now] = false;
  return;
}

int main() {
  cin >> n >> m;
  vec.resize(n + 1);
  rep(i, 1, m) {
    int a, b;
    cin >> a >> b;
    vec[a].push_back(b);
    vec[b].push_back(a);
  }
  passed.resize(n + 1);
  dfs(1, 1);
  cout << ans << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000

int MOD = 1000000007;
int vector_finder(std::vector<int> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}
int modpow(int a, int x) {
  if (a < 0)
    a += MOD;
  int ans = 1;
  while (x > 0) {
    if (x & 1)
      ans = ans * a % MOD;
    a = a * a % MOD;
    x >>= 1;
  }
  return ans;
}
int gyaku(int n) { return modpow(n, MOD - 2); }
signed main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(m, vector<int>(0));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int ans = 0;
  vector<int> array(0);
  rep(i, n) array.push_back(i);
  do {
    rep(i, n - 1) {
      if (vector_finder(g[array[i]], array[i + 1]) != 1)
        break;
      if (i == n - 2)
        ans++;
    }
  } while (next_permutation(array.begin() + 1, array.end()));
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000

int MOD = 1000000007;
int vector_finder(std::vector<int> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}
int modpow(int a, int x) {
  if (a < 0)
    a += MOD;
  int ans = 1;
  while (x > 0) {
    if (x & 1)
      ans = ans * a % MOD;
    a = a * a % MOD;
    x >>= 1;
  }
  return ans;
}
int gyaku(int n) { return modpow(n, MOD - 2); }
signed main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n, vector<int>(0));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int ans = 0;
  vector<int> array(0);
  rep(i, n) array.push_back(i);
  do {
    rep(i, n - 1) {
      if (vector_finder(g[array[i]], array[i + 1]) != 1)
        break;
      if (i == n - 2)
        ans++;
    }
  } while (next_permutation(array.begin() + 1, array.end()));
  cout << ans << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int used[8];
int connect[8][8];

int N, M;

int dfs(int now, int depth) {
  if (used[now])
    return 0;
  if (depth == N)
    return 1;
  used[now] = 1;
  int ans = 0;

  for (int i = 0; i < N; i++) {
    if (connect[now][i]) {
      ans += dfs(i, depth + 1);
    }
  }

  used[now] = 0;
  return ans;
}

int main() {
  cin >> N >> M;
  int a[8], b[8];
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  for (int i = 0; i < M; i++) {
    connect[a[i]][b[i]] = connect[b[i]][a[i]] = 1;
  }

  cout << dfs(0, 1) << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int used[8];
int connect[8][8];

int N, M;

int dfs(int now, int depth) {
  if (used[now])
    return 0;
  if (depth == N)
    return 1;
  used[now] = 1;
  int ans = 0;

  for (int i = 0; i < N; i++) {
    if (connect[now][i]) {
      ans += dfs(i, depth + 1);
    }
  }

  used[now] = 0;
  return ans;
}

int main() {
  cin >> N >> M;
  int a[30], b[30];
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  for (int i = 0; i < M; i++) {
    connect[a[i]][b[i]] = connect[b[i]][a[i]] = 1;
  }

  cout << dfs(0, 1) << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int answer = 0;
int N;
vector<vector<int>> edges;
void dfs(int currentNode, vector<bool> visited) {
  visited.at(currentNode) = true;
  bool allVisited = true;
  for (bool ed : visited) {
    if (!ed) {
      allVisited = false;
      break;
    }
  }
  if (allVisited) {
    answer++;
    return;
  }
  for (int nextNode : edges.at(currentNode)) {
    if (visited.at(nextNode)) {
      continue;
    }
    dfs(nextNode, visited);
  }
}

int main() {
  int M;
  cin >> N >> M;
  edges = vector<vector<int>>(N);
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges.at(a).push_back(b);
    edges.at(b).push_back(a);
  }
  vector<bool> visited(N, false);
  dfs(0, visited);
  cout << answer << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int answer = 0;
int N;
vector<vector<int>> edges;
void dfs(int currentNode, vector<bool> visited) {
  visited.at(currentNode) = true;
  bool allVisited = true;
  for (bool ed : visited) {
    if (!ed) {
      allVisited = false;
      break;
    }
  }
  if (allVisited) {
    answer++;
    return;
  }
  for (int nextNode : edges.at(currentNode)) {
    if (visited.at(nextNode)) {
      continue;
    }
    dfs(nextNode, visited);
  }
}

int main() {
  int M;
  cin >> N >> M;
  edges = vector<vector<int>>(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges.at(a).push_back(b);
    edges.at(b).push_back(a);
  }
  vector<bool> visited(N, false);
  dfs(0, visited);
  cout << answer << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define MOD 1000000007
vector<ll> g[10];
ll visited[10];
ll dfs(ll v, ll N) {
  ll i;
  bool flag = 1;
  for (i = 0; i < N; i++) {
    if (visited[i] == 0)
      flag = 0;
  }
  if (flag)
    return 1;
  ll cnt = 0;
  for (auto itr = g[v].begin(); itr != g[v].end(); ++itr) {
    if (visited[*itr] == 0) {
      visited[*itr] = 1;
      cnt += dfs(*itr, cnt);
      visited[*itr] = 0;
    }
  }
  return cnt;
}
int main() {
  ll N, M;
  cin >> N >> M;
  ll i, a, b;
  for (i = 0; i < N; i++) {
    visited[i] = 0;
  }
  for (i = 0; i < M; i++) {
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
  }
  visited[0] = 1;
  cout << dfs(0, N) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define MOD 1000000007
vector<ll> g[10];
ll visited[10];
ll dfs(ll v, ll N) {
  ll i;
  bool flag = 1;
  for (i = 0; i < N; i++) {
    if (visited[i] == 0)
      flag = 0;
  }
  if (flag)
    return 1;
  ll cnt = 0;
  for (auto itr = g[v].begin(); itr != g[v].end(); ++itr) {
    if (visited[*itr] == 0) {
      visited[*itr] = 1;
      cnt += dfs(*itr, N);
      visited[*itr] = 0;
    }
  }
  return cnt;
}
int main() {
  ll N, M;
  cin >> N >> M;
  ll i, a, b;
  for (i = 0; i < N; i++) {
    visited[i] = 0;
  }
  for (i = 0; i < M; i++) {
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
  }
  visited[0] = 1;
  cout << dfs(0, N) << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

int N, M;

int DFS(vector<vector<int>> &load, int now, vector<bool> visited) {

  bool end = true;

  // ベースケース
  for (int i = 1; i <= N; i++) {
    if (visited[i] == false) {
      end = false;
    }
  }
  if (end)
    return 1;

  int ret = 0;
  for (int i : load[now]) {

    // 道が無い、探索済
    if (visited[i])
      continue;

    visited[i] = true;
    ret += DFS(load, i, visited);
    visited[i] = false;
  }
  return ret;
}

int main() {

  cin >> N >> M;

  vector<vector<int>> load(M + 1, vector<int>());
  rep(z, M) {
    int a, b;
    cin >> a >> b;
    load[a].push_back(b);
    load[b].push_back(a);
  }

  vector<bool> visited(N + 1, false);
  visited[1] = true;
  int ans = DFS(load, 1, visited);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

int N, M;

int DFS(vector<vector<int>> &load, int now, vector<bool> visited) {

  bool end = true;

  // ベースケース
  for (int i = 1; i <= N; i++) {
    if (visited[i] == false) {
      end = false;
    }
  }
  if (end)
    return 1;

  int ret = 0;
  for (int i : load[now]) {

    // 道が無い、探索済
    if (visited[i])
      continue;

    visited[i] = true;
    ret += DFS(load, i, visited);
    visited[i] = false;
  }
  return ret;
}

int main() {

  cin >> N >> M;

  vector<vector<int>> load(N + 1, vector<int>());
  rep(z, M) {
    int a, b;
    cin >> a >> b;
    load[a].push_back(b);
    load[b].push_back(a);
  }

  vector<bool> visited(N + 1, false);
  visited[1] = true;
  int ans = DFS(load, 1, visited);
  cout << ans << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep01(i, n) for (int i = 0; i < (n) + 1; i++)
#define rep10(i, n) for (int i = 1; i < (n); i++)
#define rep11(i, n) for (int i = 1; i < (n) + 1; i++)
#define all(a) (a).begin(), (a).end()
typedef long long int ll;
typedef struct {
  int x;
  int y;
} Pos;
using namespace std;

//*********************
// n!
//*********************

// ABC054C
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(M, vector<int>());
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    A[a - 1].push_back(b - 1);
    A[b - 1].push_back(a - 1);
  }
  vector<int> pattern;
  int cnt = 0;
  rep(i, N) pattern.push_back(i);
  do {
    if (pattern[0] != 0)
      continue;
    bool success = true;
    rep(i, N - 1) {
      if (A[pattern[i]].end() == find(all(A[pattern[i]]), pattern[i + 1])) {
        success = false;
        break;
      }
    }
    if (success)
      cnt++;
  } while (next_permutation(all(pattern)));
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep01(i, n) for (int i = 0; i < (n) + 1; i++)
#define rep10(i, n) for (int i = 1; i < (n); i++)
#define rep11(i, n) for (int i = 1; i < (n) + 1; i++)
#define all(a) (a).begin(), (a).end()
typedef long long int ll;
typedef struct {
  int x;
  int y;
} Pos;
using namespace std;

//*********************
// n!
//*********************

// ABC054C
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(N, vector<int>());
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    A[a - 1].push_back(b - 1);
    A[b - 1].push_back(a - 1);
  }
  vector<int> pattern;
  int cnt = 0;
  rep(i, N) pattern.push_back(i);
  do {
    if (pattern[0] != 0)
      continue;
    bool success = true;
    rep(i, N - 1) {
      if (A[pattern[i]].end() == find(all(A[pattern[i]]), pattern[i + 1])) {
        success = false;
        break;
      }
    }
    if (success)
      cnt++;
  } while (next_permutation(all(pattern)));
  cout << cnt << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int n, m;
bool used[10];
vector<int> g[10];

int dfs(int s, int num) {
  if (num == n - 1) {
    // cout << s << endl;
    return 1;
  }

  int ret = 0;
  for (int i = 0; i < g[s].size(); i++) {
    if (!used[g[s][i]]) {
      used[g[s][i]] = true;
      ret += dfs(g[s][i], num + 1);
      used[g[s][i]] = false;
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  vector<int> v(n);
  iota(v.begin(), v.end(), 0);

  for (int i = 0; i < n; i++) {
    int ai, bi;
    cin >> ai >> bi;
    ai--;
    bi--;
    g[ai].push_back(bi);
    g[bi].push_back(ai);
  }

  ll ans = 0;
  memset(used, false, sizeof(used));
  used[0] = true;
  ans += dfs(0, 0);
  /*do {

  } while(next_permutation(v.begin(), v.end());
  */

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int n, m;
bool used[10];
vector<int> g[10];

int dfs(int s, int num) {
  if (num == n - 1) {
    // cout << s << endl;
    return 1;
  }

  int ret = 0;
  for (int i = 0; i < g[s].size(); i++) {
    if (!used[g[s][i]]) {
      used[g[s][i]] = true;
      ret += dfs(g[s][i], num + 1);
      used[g[s][i]] = false;
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  vector<int> v(n);
  iota(v.begin(), v.end(), 0);

  for (int i = 0; i < m; i++) {
    int ai, bi;
    cin >> ai >> bi;
    ai--;
    bi--;
    g[ai].push_back(bi);
    g[bi].push_back(ai);
  }

  ll ans = 0;
  memset(used, false, sizeof(used));
  used[0] = true;
  ans += dfs(0, 0);
  /*do {

  } while(next_permutation(v.begin(), v.end());
  */

  cout << ans << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
const int MAXN = 15;
const int MAXCASE = 25;
vector<int> m[MAXN];
int n, p;
int sum;
int vis[MAXN];
int judge[MAXN][MAXN];
void dfs(int cot, int num) {
  if (num == n) {
    sum++;
    return;
  }
  for (int i = 0; i < m[cot].size(); i++) {
    if (vis[m[cot][i]] == 0) {
      vis[m[cot][i]] = 1;
      dfs(m[cot][i], num + 1);
      vis[m[cot][i]] = 0;
    }
  }
}
int main() {
  int Test, numCase = 0;
  // cin >> Test;
  while (cin >> n >> p) {

    assert(2 <= n && n <= 8);
    assert(1 <= p && p <= n * (n - 1) / 2);

    memset(judge, 0, sizeof(judge));
    numCase++;
    for (int i = 1; i <= p; i++) {
      int v, u;
      cin >> v >> u;
      m[v].push_back(u);
      m[u].push_back(v);

      assert(1 <= v && v <= n);
      assert(1 <= u && u <= n);
      assert(u != v && judge[u][v] == judge[v][u] && judge[u][v] == 0);
      judge[u][v] = judge[v][u] = 1;
    }

    sum = 0;
    vis[1] = 1;
    dfs(1, 1);
    cout << sum << endl;
  }
  assert(numCase == MAXCASE);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
const int MAXN = 15;
const int MAXCASE = 25;
vector<int> m[MAXN];
int n, p;
int sum;
int vis[MAXN];
int judge[MAXN][MAXN];
void dfs(int cot, int num) {
  if (num == n) {
    sum++;
    return;
  }
  for (int i = 0; i < m[cot].size(); i++) {
    if (vis[m[cot][i]] == 0) {
      vis[m[cot][i]] = 1;
      dfs(m[cot][i], num + 1);
      vis[m[cot][i]] = 0;
    }
  }
}
int main() {
  int Test, numCase = 0;
  // cin >> Test;
  while (cin >> n >> p) {

    assert(2 <= n && n <= 8);
    assert(1 <= p && p <= n * (n - 1) / 2);

    memset(judge, 0, sizeof(judge));
    numCase++;
    for (int i = 1; i <= p; i++) {
      int v, u;
      cin >> v >> u;
      m[v].push_back(u);
      m[u].push_back(v);

      assert(1 <= v && v <= n);
      assert(1 <= u && u <= n);
      assert(u != v && judge[u][v] == judge[v][u] && judge[u][v] == 0);
      judge[u][v] = judge[v][u] = 1;
    }

    sum = 0;
    vis[1] = 1;
    dfs(1, 1);
    cout << sum << endl;
  }
  // assert(numCase == MAXCASE);
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define OUT(a) cout << (a) << "\n"
#define ll long long
#define uint unsigned int
#define ushort unsigned short
#define ulong unsigned long

ll MOD = 10e9 + 7;
int n, m;

int dfs(vector<vector<int>> rinsetsu, vector<int> dist, int now) {
  if (dist[now] == n)
    return 1;
  int size = rinsetsu[now].size();
  int res = 0;
  REP(i, size) {
    if (dist[rinsetsu[now][i]] != 0)
      continue;
    dist[rinsetsu[now][i]] = dist[now] + 1;
    res += dfs(rinsetsu, dist, rinsetsu[now][i]);
    dist[rinsetsu[now][i]] = 0;
  }
  return res;
}

int main() {
  cin >> n >> m;
  auto rinsetsu = vector<vector<int>>(m, vector<int>());
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    rinsetsu[a - 1].push_back(b - 1);
    rinsetsu[b - 1].push_back(a - 1);
  }
  vector<int> dist(n, 0);
  dist[0] = 1;
  cout << dfs(rinsetsu, dist, 0) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define OUT(a) cout << (a) << "\n"
#define ll long long
#define uint unsigned int
#define ushort unsigned short
#define ulong unsigned long

ll MOD = 10e9 + 7;
int n, m;

int dfs(vector<vector<int>> rinsetsu, vector<int> dist, int now) {
  if (dist[now] == n)
    return 1;
  int size = rinsetsu[now].size();
  int res = 0;
  REP(i, size) {
    if (dist[rinsetsu[now][i]] != 0)
      continue;
    dist[rinsetsu[now][i]] = dist[now] + 1;
    res += dfs(rinsetsu, dist, rinsetsu[now][i]);
    dist[rinsetsu[now][i]] = 0;
  }
  return res;
}

int main() {
  cin >> n >> m;
  auto rinsetsu = vector<vector<int>>(n, vector<int>());
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    rinsetsu[a - 1].push_back(b - 1);
    rinsetsu[b - 1].push_back(a - 1);
  }
  vector<int> dist(n, 0);
  dist[0] = 1;
  cout << dfs(rinsetsu, dist, 0) << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repp(i, j, n) for (int i = (j); i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define INF 1e9

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<ll, ll>> vpl;

int n, m;
vvi a;
int ans = 0;
vi flag;

void dfs(int v, int cnt) {
  for (auto i : a[v]) {
    if (flag[i] > 0)
      continue;
    flag[i] = 1;
    if (cnt + 1 == n - 1)
      ans++;
    else
      dfs(i, cnt + 1);
    flag[i] = 0;
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  a = vvi(m);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x].pb(y);
    a[y].pb(x);
  }
  flag = vi(n + 10);
  flag[0] = 1;
  dfs(0, 0);
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repp(i, j, n) for (int i = (j); i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define INF 1e9

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<ll, ll>> vpl;

int n, m;
vvi a;
int ans = 0;
vi flag;

void dfs(int v, int cnt) {
  for (auto i : a[v]) {
    if (flag[i] > 0)
      continue;
    flag[i] = 1;
    if (cnt + 1 == n - 1)
      ans++;
    else
      dfs(i, cnt + 1);
    flag[i] = 0;
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  a = vvi(n);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x].pb(y);
    a[y].pb(x);
  }
  flag = vi(n + 10);
  flag[0] = 1;
  dfs(0, 0);
  cout << ans << endl;

  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, f, n) for (int i = (f); (i) < (n); i++)
#define repe(i, f, n) for (int i = (f); (i) <= (n); i++)
using namespace std;

typedef long long ll;

int main() {
  int n;
  cin >> n;
  int m;
  cin >> m;
  vector<int> path[m];
  rep(i, 0, m) {
    int tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    path[i].push_back(tmp1);
    path[i].push_back(tmp2);
  }

  vector<int> perm(m);
  rep(i, 0, n) perm[i] = i + 1;

  int ans = 0;
  do {

    if (perm[0] != 1)
      break;

    int goalcount = 0;
    rep(i, 0, n - 1) {
      int from = perm[i];
      int to = perm[i + 1];
      rep(j, 0, m) {
        if ((from == path[j][0] && to == path[j][1]) ||
            (from == path[j][1] && to == path[j][0])) {
          goalcount++;
          break;
        }
      }
    }
    if (goalcount == n - 1)
      ans++;

  } while (next_permutation(perm.begin(), perm.end()));

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, f, n) for (int i = (f); (i) < (n); i++)
#define repe(i, f, n) for (int i = (f); (i) <= (n); i++)
using namespace std;

typedef long long ll;

int main() {
  int n;
  cin >> n;
  int m;
  cin >> m;
  vector<int> path[m];
  rep(i, 0, m) {
    int tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    path[i].push_back(tmp1);
    path[i].push_back(tmp2);
  }

  vector<int> perm(n);
  rep(i, 0, n) perm[i] = i + 1;

  int ans = 0;
  do {

    if (perm[0] != 1)
      break;

    int goalcount = 0;
    rep(i, 0, n - 1) {
      int from = perm[i];
      int to = perm[i + 1];
      rep(j, 0, m) {
        if ((from == path[j][0] && to == path[j][1]) ||
            (from == path[j][1] && to == path[j][0])) {
          goalcount++;
          break;
        }
      }
    }
    if (goalcount == n - 1)
      ans++;

  } while (next_permutation(perm.begin(), perm.end()));

  cout << ans << endl;

  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int dfs(int N, int p, vector<bool> v, vector<vector<bool>> ab);

int main() {
  int N, M, p;
  cin >> N >> M;
  vector<vector<bool>> ab(M, vector<bool>(M));
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    ab[a - 1][b - 1] = ab[b - 1][a - 1] = true;
  }
  vector<bool> v(N, false);
  v[0] = true;
  cout << dfs(N, 0, v, ab) << endl;
  return 0;
}

int dfs(int N, int p, vector<bool> v, vector<vector<bool>> ab) {
  bool all_v = true;
  for (int i = 0; i < N; i++) {
    if (!v[i])
      all_v = false;
  }
  if (all_v)
    return 1;

  int root = 0;
  for (int i = 0; i < N; i++) {
    if (!ab[p][i])
      continue;
    if (v[i])
      continue;
    v[i] = true;
    root += dfs(N, i, v, ab);
    v[i] = false;
  }
  return root;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int dfs(int N, int p, vector<bool> v, vector<vector<bool>> ab);

int main() {
  int N, M, p;
  cin >> N >> M;
  vector<vector<bool>> ab(N, vector<bool>(N));
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    ab[a - 1][b - 1] = ab[b - 1][a - 1] = true;
  }
  vector<bool> v(N, false);
  v[0] = true;
  cout << dfs(N, 0, v, ab) << endl;
  return 0;
}

int dfs(int N, int p, vector<bool> v, vector<vector<bool>> ab) {
  bool all_v = true;
  for (int i = 0; i < N; i++) {
    if (!v[i])
      all_v = false;
  }
  if (all_v)
    return 1;

  int root = 0;
  for (int i = 0; i < N; i++) {
    if (!ab[p][i])
      continue;
    if (v[i])
      continue;
    v[i] = true;
    root += dfs(N, i, v, ab);
    v[i] = false;
  }
  return root;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600
#define long long long // for codeforces

vector<int> vec[10];
vector<bool> visited;

int dfs(int d, int n) {
  if (n == 1)
    return 1;
  visited[d] = true;
  int cnt = 0;
  for (auto to : vec[d])
    if (visited[to] == false) {
      cnt += dfs(to, n - 1);
    }
  visited[d] = false;
  return cnt;
}

int main() {
  int n, m;
  cin >> n >> m;
  visited.resize(n);
  fill(all(visited), false);
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    vec[a].pb(b);
    vec[b].pb(a);
  }

  cout << dfs(0, n) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600
#define long long long // for codeforces

vector<int> vec[10];
vector<bool> visited;

int dfs(int d, int n) {
  if (n == 1)
    return 1;
  visited[d] = true;
  int cnt = 0;
  for (auto to : vec[d])
    if (visited[to] == false) {
      cnt += dfs(to, n - 1);
    }
  visited[d] = false;
  return cnt;
}

int main() {
  int n, m;
  cin >> n >> m;
  visited.resize(n);
  fill(all(visited), false);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    vec[a].pb(b);
    vec[b].pb(a);
  }

  cout << dfs(0, n) << endl;

  return 0;
}