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

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(m, vector<int>(m));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a][b] = 1;
    g[b][a] = 1;
  }
  vector<int> p(n - 1);
  rep(i, n - 1) p[i] = i + 1;
  int ans = 0;
  do {
    bool ok = true;
    vector<int> now(n);
    rep(i, n - 1) now[i + 1] = p[i];
    rep(i, n - 1) {
      if (g[now[i]][now[i + 1]] == 0)
        ok = false;
    }
    if (ok)
      ans++;
  } while (next_permutation(all(p)));
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n, vector<int>(n));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a][b] = 1;
    g[b][a] = 1;
  }
  vector<int> p(n - 1);
  rep(i, n - 1) p[i] = i + 1;
  int ans = 0;
  do {
    bool ok = true;
    vector<int> now(n);
    rep(i, n - 1) now[i + 1] = p[i];
    rep(i, n - 1) {
      if (g[now[i]][now[i + 1]] == 0)
        ok = false;
    }
    if (ok)
      ans++;
  } while (next_permutation(all(p)));
  cout << ans << 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)
#define rep2(i, s, n) for (int i = s; i < n; ++i)
#define _GLIBCXX_DEBUG
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> v(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  vector<int> nums(n - 1);
  rep(i, n) nums[i] = i + 1;
  int ans = 0;
  do {
    bool flag = true;
    int fir = 0;
    int sec = nums[0];
    bool firexist = false;
    for (int j : v[fir]) {
      if (j == sec)
        firexist = true;
    }
    if (!firexist)
      flag = false;
    rep(i, n - 2) {
      int now = nums[i];
      int next = nums[i + 1];
      bool exist = false;
      for (int j : v[now]) {
        if (j == next)
          exist = true;
      }
      if (!exist)
        flag = false;
    }
    if (flag)
      ans++;
  } while (next_permutation(nums.begin(), nums.end()));
  cout << ans << endl;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> v(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  vector<int> nums(n - 1);
  rep(i, n - 1) nums[i] = i + 1;
  int ans = 0;
  do {
    bool flag = true;
    int fir = 0;
    int sec = nums[0];
    bool firexist = false;
    for (int j : v[fir]) {
      if (j == sec)
        firexist = true;
    }
    if (!firexist)
      flag = false;
    rep(i, n - 2) {
      int now = nums[i];
      int next = nums[i + 1];
      bool exist = false;
      for (int j : v[now]) {
        if (j == next)
          exist = true;
      }
      if (!exist)
        flag = false;
    }
    if (flag)
      ans++;
  } while (next_permutation(nums.begin(), nums.end()));
  cout << ans << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
int N, M;
int ans = 0, cnt = 0;
vector<vector<int>> G;
vector<bool> visited;

void dfs(int v) {
  visited.at(v) = true;
  cnt++;
  if (cnt == N) {
    ans++;
    return;
  }
  for (int i = 0; i < N; i++) {
    if (G.at(v).at(i) == 1 && visited.at(i) == false) {
      int temp_cnt = cnt;
      vector<bool> temp_visited(visited);
      dfs(i);
      cnt = temp_cnt;
      visited = temp_visited;
    }
  }
}

int main() {
  cin >> N >> M;
  G = vector<vector<int>>(N, vector<int>(N, 0));
  visited = vector<bool>(N, false);

  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G.at(a).at(b) = 1;
    G.at(b).at(a) = 1;
  }
  dfs(0);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
int N, M;
int ans = 0, cnt = 0;
vector<vector<int>> G;
vector<bool> visited;

void dfs(int v) {
  visited.at(v) = true;
  cnt++;
  if (cnt == N) {
    ans++;
    return;
  }
  for (int i = 0; i < N; i++) {
    if (G.at(v).at(i) == 1 && visited.at(i) == false) {
      int temp_cnt = cnt;
      vector<bool> temp_visited(visited);
      dfs(i);
      cnt = temp_cnt;
      visited = temp_visited;
    }
  }
}

int main() {
  cin >> N >> M;
  G = vector<vector<int>>(N, vector<int>(N, 0));
  visited = vector<bool>(N, false);

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G.at(a).at(b) = 1;
    G.at(b).at(a) = 1;
  }
  dfs(0);
  cout << ans << endl;
}
Problem p03805 (C++)

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

using namespace std;

int n, m;
vector<vector<int>> graph;
vector<bool> check;
int visitcount;
int result;

void visit(int i) {
  if (check[i])
    return;
  check[i] = true;
  visitcount++;
  if (visitcount == n)
    result++;
  else
    for (auto &&ii : graph[i])
      visit(ii);
  visitcount--;
  check[i] = false;
}

int main() {
  cin >> n >> m;
  graph = vector<vector<int>>(n);
  check = vector<bool>(n, false);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  visit(0);
  cout << result << endl;
  return 0;
}


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

using namespace std;

int n, m;
vector<vector<int>> graph;
vector<bool> check;
int visitcount;
int result;

void visit(int i) {
  if (check[i])
    return;
  check[i] = true;
  visitcount++;
  if (visitcount == n)
    result++;
  else
    for (auto &&ii : graph[i])
      visit(ii);
  visitcount--;
  check[i] = false;
}

int main() {
  cin >> n >> m;
  graph = vector<vector<int>>(n);
  check = vector<bool>(n, false);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  visit(0);
  cout << result << endl;
  return 0;
}
Problem p03805 (C++)

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

const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

typedef long long ll;

#define MAX_N 10000

struct Edge {
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<int>>;
using Graph_e = vector<vector<Edge>>;

vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
typedef pair<int, int> P;

Graph G;
void dfs(int v, vector<bool> &seen, int &res) {
  bool end = true;
  for (int i = 0; i < seen.size(); i++)
    if (!seen[i] && i != v) // iにまだ行ってない&&次のvがiではない
      end = false;
  if (end) {
    ++res;
    return;
  }

  seen[v] = true;
  for (auto nv : G[v]) {
    if (seen[nv])
      continue;
    dfs(nv, seen, res);
  }
  seen[v] = false;
}
int main() {
  int N, M;
  cin >> N >> M;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<bool> seen(N, false);
  int res = 0;
  dfs(0, seen, res);
  cout << res << endl;
}


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

const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

typedef long long ll;

#define MAX_N 10000

struct Edge {
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<int>>;
using Graph_e = vector<vector<Edge>>;

vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
typedef pair<int, int> P;

Graph G;
void dfs(int v, vector<bool> &seen, int &res) {
  bool end = true;
  for (int i = 0; i < seen.size(); i++)
    if (!seen[i] && i != v) // iにまだ行ってない&&次のvがiではない
      end = false;
  if (end) {
    ++res;
    return;
  }

  seen[v] = true;
  for (auto nv : G[v]) {
    if (seen[nv])
      continue;
    dfs(nv, seen, res);
  }
  seen[v] = false;
}
int main() {
  int N, M;
  cin >> N >> M;
  G.assign(N, vector<int>());
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<bool> seen(N, false);
  int res = 0;
  dfs(0, seen, res);
  cout << res << endl;
}
Problem p03805 (C++)

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

using namespace std;
const int MAXN = 8;
vector<int> G[MAXN];
int ans = 0;
int n, m;

void dfs(int cur, vector<bool> visited) {
  bool done = true;
  for (int i = 0; i < n; ++i) {
    if (!visited[i])
      done = false;
  }
  if (done) {
    ans++;
    return;
  }
  for (size_t i = 0; i < G[cur].size(); ++i) {
    int next = G[cur][i];
    if (!visited[next]) {
      visited[next] = true;
      dfs(next, visited);
      visited[next] = false;
    }
  }
}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<bool> visited(n, false);
  visited[0] = true;
  dfs(0, visited);
  printf("%d\n", ans);
  return 0;
}


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

using namespace std;
const int MAXN = 8;
vector<int> G[MAXN];
int ans = 0;
int n, m;

void dfs(int cur, vector<bool> visited) {
  bool done = true;
  for (int i = 0; i < n; ++i) {
    if (!visited[i])
      done = false;
  }
  if (done) {
    ans++;
    return;
  }
  for (size_t i = 0; i < G[cur].size(); ++i) {
    int next = G[cur][i];
    if (!visited[next]) {
      visited[next] = true;
      dfs(next, visited);
      visited[next] = false;
    }
  }
}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<bool> visited(n, false);
  visited[0] = true;
  dfs(0, visited);
  printf("%d\n", ans);
  return 0;
}
Problem p03805 (C++)

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

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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

int main() {
  int n, m;
  cin >> n >> m;
  int edge[n][n];
  memset(edge, 0, sizeof(edge));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    edge[a - 1][b - 1] = 1;
    edge[b - 1][a - 1] = 1;
  }
  vector<int> a(n - 1);
  rep(i, n - 1) a[i] = i + 1;
  int ans = 0;
  int combi = 1;
  rep(i, 1, n) combi *= i;
  rep(i, combi) {
    bool flag = 1;
    rep(j, n - 1) {
      if (!edge[a[j]][a[j + 1]]) {
        flag = 0;
        break;
      }
    }
    if (flag)
      ++ans;
    next_permutation(a.begin(), a.end());
  }
  cout << ans << endl;
}


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

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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

int main() {
  int n, m;
  cin >> n >> m;
  int edge[n][n];
  memset(edge, 0, sizeof(edge));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    edge[a - 1][b - 1] = 1;
    edge[b - 1][a - 1] = 1;
  }
  vector<int> a(n);
  rep(i, n) a[i] = i;
  int ans = 0;
  int combi = 1;
  rep(i, 1, n) combi *= i;
  rep(i, combi) {
    bool flag = 1;
    rep(j, n - 1) {
      if (!edge[a[j]][a[j + 1]]) {
        flag = 0;
        break;
      }
    }
    if (flag)
      ++ans;
    next_permutation(a.begin(), a.end());
  }
  cout << ans << endl;
}
Problem p03805 (C++)

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

int main() {
  int N, M;
  cin >> N >> M;
  int a, b;
  vector<pair<int, int>> ab(N);
  for (int i = 0; i < M; ++i) {
    cin >> a >> b;
    ab[i] = make_pair(a, b);
  }
  vector<int> path(N);
  rep(i, N) path[i] = i + 1;
  int result = 0;
  do {
    if (path.end() == adjacent_find(all(path), [&](int l, int r) {
          return none_of(all(ab), [&](auto p) {
            return ((p.first == l) && (p.second == r)) ||
                   ((p.second == l) && (p.first == r));
          });
        })) {
      ++result;
    }
  } while (next_permutation(path.begin() + 1, path.end()));
  cout << result << endl;
  return 0;
}


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

int main() {
  int N, M;
  cin >> N >> M;
  int a, b;
  vector<pair<int, int>> ab(M);
  rep(i, M) {
    cin >> a >> b;
    ab[i] = make_pair(a, b);
  }
  vector<int> path(N);
  rep(i, N) path[i] = i + 1;
  int result = 0;
  do {
    if (path.end() == adjacent_find(all(path), [&](int l, int r) {
          return none_of(all(ab), [&](auto p) {
            return ((p.first == l) && (p.second == r)) ||
                   ((p.second == l) && (p.first == r));
          });
        })) {
      ++result;
    }
  } while (next_permutation(path.begin() + 1, path.end()));
  cout << result << endl;
  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 < (int)(n); i++)

map<int, set<int>> routes;

int ans = 0;
int N;

void search(int position, vector<bool> reached, int depth) {
  if (reached.at(position)) {
    return;
  }
  reached.at(position) = true;

  if (depth == N) {
    ans++;
    return;
  }

  for (int next_position : routes[position]) {
    int next_depth = depth + 1;
    vector<bool> reached2;
    copy(reached.begin(), reached.end(), back_inserter(reached2));
    search(next_position, reached2, next_depth);
  }
}

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

  vector<int> a(N);
  vector<int> b(N);
  rep(i, N) cin >> a.at(i) >> b.at(i);
  rep(i, N) {
    a.at(i)--;
    b.at(i)--;
  }

  rep(i, M) {
    routes[a.at(i)].insert(b.at(i));
    routes[b.at(i)].insert(a.at(i));
  }
  vector<bool> reached(N, false);
  search(0, reached, 1);

  cout << ans << endl;

  return 0;
}


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

map<int, set<int>> routes;

int ans = 0;
int N;

void search(int position, vector<bool> reached, int depth) {
  if (reached.at(position)) {
    return;
  }
  reached.at(position) = true;

  if (depth == N) {
    ans++;
    return;
  }

  for (int next_position : routes[position]) {
    int next_depth = depth + 1;
    vector<bool> reached2;
    copy(reached.begin(), reached.end(), back_inserter(reached2));
    search(next_position, reached2, next_depth);
  }
}

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

  vector<int> a(M);
  vector<int> b(M);
  rep(i, M) cin >> a.at(i) >> b.at(i);
  rep(i, M) {
    a.at(i)--;
    b.at(i)--;
  }

  rep(i, M) {
    routes[a.at(i)].insert(b.at(i));
    routes[b.at(i)].insert(a.at(i));
  }
  vector<bool> reached(N, false);
  search(0, reached, 1);

  cout << ans << endl;

  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main(void) {
  /*
  ABC 054 C
  https://atcoder.jp/contests/abc054/tasks/abc054_c
  */
  int N_vertices, N_edges;
  cin >> N_vertices >> N_edges;

  vector<vector<int>> Graph(N_vertices, vector<int>(N_vertices, 0));
  vector<int> permutation(N_vertices);
  for (int i = 0; i < N_vertices; i++) {
    permutation[i] = i;
  }

  int i, j;
  for (int i = 0; i < N_edges; i++) {
    cin >> i >> j;
    Graph[i - 1][j - 1] = 1;
    Graph[j - 1][i - 1] = 1;
  }

  int comb = 0;
  bool flag = true;
  int pos, next;
  do {
    flag = true;
    pos = 0;
    for (int i = 0; i < N_vertices - 1; i++) {
      next = permutation[i + 1];
      if (Graph[pos][next] == 0) {
        flag = false;
        break;
      }
      pos = next;
    }
    if (flag)
      comb++;
  } while (next_permutation(permutation.begin() + 1, permutation.end()));

  cout << comb << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main(void) {
  /*
  ABC 054 C
  https://atcoder.jp/contests/abc054/tasks/abc054_c
  */
  int N_vertices, N_edges;
  cin >> N_vertices >> N_edges;

  vector<vector<int>> Graph(N_vertices, vector<int>(N_vertices, 0));
  vector<int> permutation(N_vertices);
  for (int i = 0; i < N_vertices; i++) {
    permutation[i] = i;
  }

  int i, j;
  for (int k = 0; k < N_edges; k++) {
    cin >> i >> j;
    Graph[i - 1][j - 1] = 1;
    Graph[j - 1][i - 1] = 1;
  }

  int comb = 0;
  bool flag = true;
  int pos, next;
  do {
    flag = true;
    pos = 0;
    for (int i = 0; i < N_vertices - 1; i++) {
      next = permutation[i + 1];
      if (Graph[pos][next] == 0) {
        flag = false;
        break;
      }
      pos = next;
    }
    if (flag)
      comb++;
  } while (next_permutation(permutation.begin() + 1, permutation.end()));

  cout << comb << endl;

  return 0;
}
Problem p03805 (C++)

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

int ans = 0;
int N;

void dfs(vector<vector<bool>> &graph, vector<bool> &visited, int V) {
  for (int i = 0; i < N; i++) {
    if (visited.at(i))
      continue;
    if (graph.at(V).at(i) == false)
      continue;
    visited.at(i) = true;
    dfs(graph, visited, i);
    visited.at(i) = false;
  }

  bool flag = true;
  for (int i = 0; i < N; i++)
    if (visited.at(i) == false)
      flag = false;
  if (flag)
    ans++;
}

int main() {
  int M;
  cin >> N >> M;
  vector<vector<bool>> graph(N, vector<bool>(N, false));
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph.at(a).at(b) = true;
    graph.at(b).at(a) = true;
  }

  vector<bool> visited(N, false);
  visited.at(0) = true;
  dfs(graph, visited, 0);
  cout << ans << endl;

  return 0;
}


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

int ans = 0;
int N;

void dfs(vector<vector<bool>> &graph, vector<bool> &visited, int V) {
  for (int i = 0; i < N; i++) {
    if (visited.at(i))
      continue;
    if (graph.at(V).at(i) == false)
      continue;
    visited.at(i) = true;
    dfs(graph, visited, i);
    visited.at(i) = false;
  }

  bool flag = true;
  for (int i = 0; i < N; i++)
    if (visited.at(i) == false)
      flag = false;
  if (flag)
    ans++;
}

int main() {
  int M;
  cin >> N >> M;
  vector<vector<bool>> graph(N, vector<bool>(N, false));
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph.at(a).at(b) = true;
    graph.at(b).at(a) = true;
  }

  vector<bool> visited(N, false);
  visited.at(0) = true;
  dfs(graph, visited, 0);
  cout << ans << endl;

  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, m, p1, p2, ans;
  ans = 0;
  cin >> n >> m;
  vector<set<int>> path;
  vector<int> p;
  path.resize(m + 1);
  for (int i = 0; i < m; ++i) {
    cin >> p1 >> p2;
    path[p1].insert(p2);
    path[p2].insert(p1);
  }
  for (int i = 1; i <= n; ++i) {
    p.push_back(i);
  }
  do {
    if (p[0] != 1) {
      break;
    }
    for (int i = 0; i < n; ++i) {
      if (i == n - 1) {
        ans++;
      } else if (path[p[i]].find(p[i + 1]) == path[p[i]].end()) {
        break;
      }
    }
  } while (next_permutation(p.begin(), p.end()));
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, m, p1, p2, ans;
  ans = 0;
  cin >> n >> m;
  vector<set<int>> path;
  vector<int> p;
  path.resize(n + 1);
  for (int i = 0; i < m; ++i) {
    cin >> p1 >> p2;
    path[p1].insert(p2);
    path[p2].insert(p1);
  }
  for (int i = 1; i <= n; ++i) {
    p.push_back(i);
  }
  do {
    if (p[0] != 1) {
      break;
    }
    for (int i = 0; i < n; ++i) {
      if (i == n - 1) {
        ans++;
      } else if (path[p[i]].find(p[i + 1]) == path[p[i]].end()) {
        break;
      }
    }
  } while (next_permutation(p.begin(), p.end()));
  cout << ans << endl;
}
Problem p03805 (C++)

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

int main() {
  int N, M, result = 0;
  cin >> N >> M;
  vector<pair<int, int>> R(M);
  for (int i = 0; i < M; i++) {
    cin >> R.at(i).first >> R.at(i).second;
  }
  vector<int> n(N - 1);
  for (int i = 0; i < N - 1; i++) {
    n.at(i) = i + 2;
  }
  do {
    bool y = false;
    for (int i = 0; R.at(i).first == 1; i++) {
      if (n.at(0) == R.at(i).second) {
        y = true;
      }
    }
    if (y) {
      bool x = true;
      for (int i = 0; i < N - 2; i++) {
        for (int j = 0; j < M; j++) {
          if ((R.at(j).first == n.at(i) && R.at(j).second == n.at(i + 1)) ||
              (R.at(j).second == n.at(i) && R.at(j).first == n.at(i + 1))) {
            break;
          }
          if (j == M - 1) {
            x = false;
          }
        }
      }
      if (x) {
        result++;
      }
    }
  } while (next_permutation(n.begin(), n.end()));
  cout << result << endl;
}

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

int main() {
  int N, M, result = 0;
  cin >> N >> M;
  vector<pair<int, int>> R(M);
  for (int i = 0; i < M; i++) {
    cin >> R.at(i).first >> R.at(i).second;
  }
  vector<int> n(N - 1);
  for (int i = 0; i < N - 1; i++) {
    n.at(i) = i + 2;
  }
  do {
    bool y = false;
    for (int i = 0; i < M; i++) {
      if (R.at(i).first == 1 && n.at(0) == R.at(i).second) {
        y = true;
      }
    }
    if (y) {
      bool x = true;
      for (int i = 0; i < N - 2; i++) {
        for (int j = 0; j < M; j++) {
          if ((R.at(j).first == n.at(i) && R.at(j).second == n.at(i + 1)) ||
              (R.at(j).second == n.at(i) && R.at(j).first == n.at(i + 1))) {
            break;
          }
          if (j == M - 1) {
            x = false;
          }
        }
      }
      if (x) {
        result++;
      }
    }
  } while (next_permutation(n.begin(), n.end()));
  cout << result << endl;
}
Problem p03805 (C++)

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

#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using Graph = vector<vector<int>>;

int n, m;
int ans = 0;
int c = 0;
vector<vector<int>> g;
vector<bool> seen;

void dfs(int i) {
  seen[i] = true;
  c++;
  if (c == n)
    ans++;
  for (auto next : g[i]) {
    if (seen[next])
      continue;
    dfs(next);
  }
  seen[i] = false;
  c--;
}

int main() {
  cin >> n >> m;
  g.resize(m + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  seen.resize(n + 1, false);
  dfs(1);
  cout << ans << endl;
}


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

#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using Graph = vector<vector<int>>;

int n, m;
int ans = 0;
int c = 0;
vector<vector<int>> g;
vector<bool> seen;

void dfs(int i) {
  seen[i] = true;
  c++;
  if (c == n)
    ans++;
  for (auto next : g[i]) {
    if (seen[next])
      continue;
    dfs(next);
  }
  seen[i] = false;
  c--;
}

int main() {
  cin >> n >> m;
  g.resize(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  seen.resize(n + 1, false);
  dfs(1);
  cout << ans << endl;
}
Problem p03805 (C++)

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

#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define all(x) (x).begin(), (x).end()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define pb push_back
#define sz(x) (int)(x).size()
#define PQ(T) priority_queue<T, v(T), greater<T>>

// #define v(T) vector<T>
// #define vv(T) v(v(T))

using namespace std;
typedef long long ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
// typedef vector<int> vi;
// typedef vector<vi> vvi;
// typedef vector<ll> vl;
// typedef vector<P> vp;

const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;

const int mod = 1000000007;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> to[10];
  int a, b;
  rep(i, n) {
    cin >> a >> b;
    --a;
    --b;
    to[a].pb(b);
    to[b].pb(a);
  }

  vector<int> v(n - 1);
  rep(i, n - 1) v[i] = i + 1;
  int ans = 0;
  do {
    int pre = 0;
    bool ok = true;
    for (auto i : v) {
      auto itr = find(all(to[pre]), i);
      if (itr == to[pre].end())
        ok = false;
      pre = i;
    }
    if (ok)
      ++ans;
  } while (next_permutation(all(v)));
  cout << ans << endl;
  return 0;
}


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

#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define all(x) (x).begin(), (x).end()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define pb push_back
#define sz(x) (int)(x).size()
#define PQ(T) priority_queue<T, v(T), greater<T>>

// #define v(T) vector<T>
// #define vv(T) v(v(T))

using namespace std;
typedef long long ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
// typedef vector<int> vi;
// typedef vector<vi> vvi;
// typedef vector<ll> vl;
// typedef vector<P> vp;

const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;

const int mod = 1000000007;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> to[10];
  int a, b;
  rep(i, m) {
    cin >> a >> b;
    --a;
    --b;
    to[a].pb(b);
    to[b].pb(a);
  }

  vector<int> v(n - 1);
  rep(i, n - 1) v[i] = i + 1;
  int ans = 0;
  do {
    int pre = 0;
    bool ok = true;
    for (auto i : v) {
      auto itr = find(all(to[pre]), i);
      if (itr == to[pre].end())
        ok = false;
      pre = i;
    }
    if (ok)
      ++ans;
  } while (next_permutation(all(v)));
  cout << ans << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;

int N, M;
vector<vector<int>> graph;
vector<bool> seen;
int cnt = 0;
void dfs(int v) {
  if (seen[v])
    return;
  seen[v] = 1;
  for (int nv : graph[v]) {
    if (seen[nv])
      continue;
    dfs(nv);
  }
  bool flag = 1;
  for (int i = 0; i < N; ++i) {
    flag &= seen[i];
  }
  if (flag) {
    ++cnt;
  }
  seen[v] = 0;
}
int main() {
  cin >> N >> M;
  graph.resize(N);
  seen.resize(N, 0);
  for (int i = 0; i < N; ++i) {
    int from, to;
    cin >> from >> to;
    --from;
    --to;
    graph[from].push_back(to);
    graph[to].push_back(from);
  }
  dfs(0);
  cout << cnt << endl;
}


Fixed submission:
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;

int N, M;
vector<vector<int>> graph;
vector<bool> seen;
int cnt = 0;
void dfs(int v) {
  if (seen[v])
    return;
  seen[v] = 1;
  for (int nv : graph[v]) {
    if (seen[nv])
      continue;
    dfs(nv);
  }
  bool flag = 1;
  for (int i = 0; i < N; ++i) {
    flag &= seen[i];
  }
  if (flag) {
    ++cnt;
  }
  seen[v] = 0;
}
int main() {
  cin >> N >> M;
  graph.resize(N);
  seen.resize(N, 0);
  for (int i = 0; i < M; ++i) {
    int from, to;
    cin >> from >> to;
    --from;
    --to;
    graph[from].push_back(to);
    graph[to].push_back(from);
  }
  dfs(0);
  cout << cnt << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, p, q) for (ll i = p; i < q; i++)
#define ll long long

using namespace std;

vector<vector<ll>> graph;
ll n;
ll c;

void dfs(ll v, vector<ll> used) {
  if (used.size() == n) {
    c++;
    return;
  }
  bool deadEnd = true;
  rep(i, 0, n) {
    if (find(used.begin(), used.end(), i) == used.end()) {
      deadEnd = false;
      if (graph[v][i] > 0) {
        vector<ll> newUsed = used;
        newUsed.push_back(i);
        dfs(i, newUsed);
      }
    }
  }
  return;
}

// ABC 54
int main(void) {
  ll m;
  cin >> n >> m;
  graph = vector<vector<ll>>(n, vector<ll>(n, -1));
  rep(i, 0, n) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a][b] = 1;
    graph[b][a] = 1;
  }

  ll ans = 0;
  ll v = 0;
  c = 0;
  dfs(v, vector<ll>(1, 0));

  cout << c << endl;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, p, q) for (ll i = p; i < q; i++)
#define ll long long

using namespace std;

vector<vector<ll>> graph;
ll n;
ll c;

void dfs(ll v, vector<ll> used) {
  if (used.size() == n) {
    c++;
    return;
  }
  bool deadEnd = true;
  rep(i, 0, n) {
    if (find(used.begin(), used.end(), i) == used.end()) {
      deadEnd = false;
      if (graph[v][i] > 0) {
        vector<ll> newUsed = used;
        newUsed.push_back(i);
        dfs(i, newUsed);
      }
    }
  }
  return;
}

// ABC 54
int main(void) {
  ll m;
  cin >> n >> m;
  graph = vector<vector<ll>>(n, vector<ll>(n, -1));
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a][b] = 1;
    graph[b][a] = 1;
  }

  ll ans = 0;
  ll v = 0;
  c = 0;
  dfs(v, vector<ll>(1, 0));

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, p, q) for (ll i = p; i < q; i++)
#define ll long long

using namespace std;

vector<vector<ll>> graph;
ll n;
ll dfs(ll v, ll count, vector<ll> used) {
  if (used.size() == n) {
    return ++count;
  }
  bool deadEnd = true;
  rep(i, 0, n) {
    if (find(used.begin(), used.end(), i) == used.end()) {
      deadEnd = false;
      if (graph[v][i] > 0) {
        vector<ll> newUsed = used;
        newUsed.push_back(i);
        count = dfs(i, count, newUsed);
      }
    }
  }
  return deadEnd ? 0 : count;
}

// ABC 54
int main(void) {
  ll m;
  cin >> n >> m;
  graph = vector<vector<ll>>(n, vector<ll>(n, -1));
  rep(i, 0, n) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a][b] = 1;
    graph[b][a] = 1;
  }

  ll ans = 0;
  ans += dfs(0, 0, vector<ll>(1, 0));

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, p, q) for (ll i = p; i < q; i++)
#define ll long long

using namespace std;

vector<vector<ll>> graph;
ll n;
ll dfs(ll v, ll count, vector<ll> used) {
  if (used.size() == n) {
    return ++count;
  }
  bool deadEnd = true;
  rep(i, 0, n) {
    if (find(used.begin(), used.end(), i) == used.end()) {
      deadEnd = false;
      if (graph[v][i] > 0) {
        vector<ll> newUsed = used;
        newUsed.push_back(i);
        count = dfs(i, count, newUsed);
      }
    }
  }
  return deadEnd ? 0 : count;
}

// ABC 54
int main(void) {
  ll m;
  cin >> n >> m;
  graph = vector<vector<ll>>(n, vector<ll>(n, -1));
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a][b] = 1;
    graph[b][a] = 1;
  }

  ll ans = 0;
  ans += dfs(0, 0, vector<ll>(1, 0));

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
const long long mod = 1000000007;
typedef long long int ll;
typedef pair<int, int> P;
const vector<int> di = {-1, 0, 1, 0};
const vector<int> dj = {0, 1, 0, -1};
int ans = 0;

void step(int num, int sum, int n, int m, vector<int> a, vector<int> b,
          vector<int> c) {

  c.at(num - 1) = 1;
  sum++;
  if (sum == n) {
    ans++;
    rep(i, n) c.at(i) = 0;
  }
  rep(i, m) {
    if (num == a.at(i) && c.at(b.at(i) - 1) == 0) {
      step(b.at(i), sum, n, m, a, b, c);
      continue;
    }
    if (num == b.at(i) && c.at(a.at(i) - 1) == 0) {
      step(a.at(i), sum, n, m, a, b, c);
    }
  }
}

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b(m);
  vector<int> c(n, 0);
  rep(i, m) cin >> a.at(i) >> b.at(i);

  int num = 1;
  int sum = 0;
  step(num, sum, n, m, a, b, c);

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
const long long mod = 1000000007;
typedef long long int ll;
typedef pair<int, int> P;
const vector<int> di = {-1, 0, 1, 0};
const vector<int> dj = {0, 1, 0, -1};
int ans = 0;

void step(int num, int sum, int n, int m, vector<int> a, vector<int> b,
          vector<int> c) {

  c.at(num - 1) = 1;
  sum++;
  if (sum == n) {
    ans++;
  }
  rep(i, m) {
    if (num == a.at(i) && c.at(b.at(i) - 1) == 0) {
      step(b.at(i), sum, n, m, a, b, c);
      continue;
    }
    if (num == b.at(i) && c.at(a.at(i) - 1) == 0) {
      step(a.at(i), sum, n, m, a, b, c);
    }
  }
}

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b(m);
  vector<int> c(n, 0);
  rep(i, m) cin >> a.at(i) >> b.at(i);

  int num = 1;
  int sum = 0;
  step(num, sum, n, m, a, b, c);

  cout << ans << endl;

  return 0;
}
Problem p03805 (C++)

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

int main() {
  int N, M;
  cin >> N >> M;
  // 　グラフを隣接行列で管理
  vector<vector<int>> graph(N, vector<int>(N));
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    graph.at(x).at(y) = graph.at(y).at(x) = 1;
  }

  // 順列(0, 1, 2, ……)
  vector<int> ord(N);
  for (int i = 0; i < N; i++) {
    ord.at(i) = i;
  }

  // 順列を全部試す
  int ans = 0;
  do {
    if (ord.at(0) != 0)
      break; // 1から始まっていない順列は調べない

    bool ok = true;
    for (int i = 0; i + 1 < N; i++) {
      int from = ord.at(i);
      int to = ord.at(i + 1);
      if (!graph.at(from).at(to)) {
        ok = false;
      }
    }
    if (ok) {
      ans++;
    }
  } while (next_permutation(ord.begin(), ord.end()));

  cout << ans << endl;
}

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

int main() {
  int N, M;
  cin >> N >> M;
  // 　グラフを隣接行列で管理
  vector<vector<int>> graph(N, vector<int>(N));
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    graph.at(x).at(y) = graph.at(y).at(x) = 1;
  }

  // 順列(0, 1, 2, ……)
  vector<int> ord(N);
  for (int i = 0; i < N; i++) {
    ord.at(i) = i;
  }

  // 順列を全部試す
  int ans = 0;
  do {
    if (ord.at(0) != 0)
      break; // 1から始まっていない順列は調べない

    bool ok = true;
    for (int i = 0; i + 1 < N; i++) {
      int from = ord.at(i);
      int to = ord.at(i + 1);
      if (!graph.at(from).at(to)) {
        ok = false;
      }
    }
    if (ok) {
      ans++;
    }
  } while (next_permutation(ord.begin(), ord.end()));

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

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

int kensaku(int s, int g, vector<pair<int, int>> H) {
  for (int i = 0; i < H.size(); i++) {
    if (s == H.at(i).first && g == H.at(i).second)
      return 1;
    if (g == H.at(i).first && s == H.at(i).second)
      return 1;
  }
  return 0;
}

int main() {
  int N, M, a, b, ans = 0, ac = 0;
  cin >> N >> M;
  vector<pair<int, int>> H;
  vector<int> v(M);
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    H.push_back(make_pair(a, b));
  }
  for (int i = 1; i <= N; i++)
    v.at(i - 1) = i;

  sort(v.begin(), v.end());
  do {
    ac = 0;
    // 1行で今の並び方を出力
    for (int i = 0; i < N - 1; i++) {
      if (kensaku(v.at(i), v.at(i + 1), H) == 0) {
        ac = 1;
        break;
      }
    }
    if (ac == 0)
      ans++;
  } while (next_permutation(v.begin() + 1, v.end()));

  cout << ans << endl;
}

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

int kensaku(int s, int g, vector<pair<int, int>> H) {
  for (int i = 0; i < H.size(); i++) {
    if (s == H.at(i).first && g == H.at(i).second)
      return 1;
    if (g == H.at(i).first && s == H.at(i).second)
      return 1;
  }
  return 0;
}

int main() {
  int N, M, a, b, ans = 0, ac = 0;
  cin >> N >> M;
  vector<pair<int, int>> H;
  vector<int> v(N);
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    H.push_back(make_pair(a, b));
  }
  for (int i = 1; i <= N; i++)
    v.at(i - 1) = i;

  sort(v.begin(), v.end());
  do {
    ac = 0;
    // 1行で今の並び方を出力
    for (int i = 0; i < N - 1; i++) {
      if (kensaku(v.at(i), v.at(i + 1), H) == 0) {
        ac = 1;
        break;
      }
    }
    if (ac == 0)
      ans++;
  } while (next_permutation(v.begin() + 1, v.end()));

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> num(N - 1);
  for (int i = 0; i < N; i++)
    num[i] = i + 2;
  bool load[10][10];
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      load[i][j] = false;
    }
  }
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    load[a][b] = load[b][a] = true;
  }
  int ans = 0;
  do {
    bool ok = true;
    for (int i = 0; i < N - 1; i++) {
      int p = 1;
      int n = num[i];
      if (i > 0)
        p = num[i - 1];
      if (!load[p][n])
        ok = false;
    }
    if (ok)
      ans++;
  } while (next_permutation(num.begin(), num.end()));
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> num(N - 1);
  for (int i = 0; i < N - 1; i++)
    num[i] = i + 2;
  bool load[10][10];
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      load[i][j] = false;
    }
  }
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    load[a][b] = load[b][a] = true;
  }
  int ans = 0;
  do {
    bool ok = true;
    for (int i = 0; i < N - 1; i++) {
      int p = 1;
      int n = num[i];
      if (i > 0)
        p = num[i - 1];
      if (!load[p][n])
        ok = false;
    }
    if (ok)
      ans++;
  } while (next_permutation(num.begin(), num.end()));
  cout << ans << endl;
}
Problem p03806 (C++)

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

using namespace std;

pair<pair<int, int>, int> a[45];

int dp[11][405][405];
int n;

int solve(int pos, int x, int y) {
  if (x < 0 || y < 0)
    return 1e9;
  if (pos == n) {
    if (x == 0 && y == 0)
      return 0;
    return 1e9;
  }
  if (dp[pos][x][y] != -1)
    return dp[pos][x][y];
  int ans =
      min(solve(pos + 1, x - a[pos].first.first, y - a[pos].first.second) +
              a[pos].second,
          solve(pos + 1, x, y));
  return dp[pos][x][y] = ans;
}

int main() {
  int ma, mb;
  scanf("%d %d %d", &n, &ma, &mb);
  int foo = 0;
  int bar = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &a[i].first.first, &a[i].first.second, &a[i].second);
    foo += a[i].first.first;
    bar += a[i].first.second;
  }
  int res = max(foo, bar);
  int k = 1;
  int ans = 1e9;
  while (ma * k <= res && mb * k <= res) {
    memset(dp, -1, sizeof dp);
    int v = solve(0, ma * k, mb * k);
    ans = min(ans, v);
    k++;
  }
  printf("%d\n", (ans == 1e9) ? -1 : ans);
  return 0;
}

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

using namespace std;

pair<pair<int, int>, int> a[45];

int dp[45][405][405];
int n;

int solve(int pos, int x, int y) {
  if (x < 0 || y < 0)
    return 1e9;
  if (pos == n) {
    if (x == 0 && y == 0)
      return 0;
    return 1e9;
  }
  if (dp[pos][x][y] != -1)
    return dp[pos][x][y];
  int ans =
      min(solve(pos + 1, x - a[pos].first.first, y - a[pos].first.second) +
              a[pos].second,
          solve(pos + 1, x, y));
  return dp[pos][x][y] = ans;
}

int main() {
  int ma, mb;
  scanf("%d %d %d", &n, &ma, &mb);
  int foo = 0;
  int bar = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &a[i].first.first, &a[i].first.second, &a[i].second);
    foo += a[i].first.first;
    bar += a[i].first.second;
  }
  int res = max(foo, bar);
  int k = 1;
  int ans = 1e9;
  while (ma * k <= res && mb * k <= res) {
    memset(dp, -1, sizeof dp);
    int v = solve(0, ma * k, mb * k);
    ans = min(ans, v);
    k++;
  }
  printf("%d\n", (ans == 1e9) ? -1 : ans);
  return 0;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int a[40], b[40], c[40];
int dp[11][401][401];

int main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }

  for (int i = 0; i <= n; i++) {
    for (int ca = 0; ca <= 400; ca++) {
      for (int cb = 0; cb <= 400; cb++) {
        dp[i][ca][cb] = 1e6;
      }
    }
  }

  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int ca = 0; ca <= 400; ca++) {
      for (int cb = 0; cb <= 400; cb++) {
        if (dp[i][ca][cb] == 1e6)
          continue;
        dp[i + 1][ca][cb] = min(dp[i][ca][cb], dp[i + 1][ca][cb]);
        dp[i + 1][ca + a[i]][cb + b[i]] =
            min(dp[i + 1][ca + a[i]][cb + b[i]], dp[i][ca][cb] + c[i]);
      }
    }
  }

  int ans = 1e6;
  for (int ca = 1; ca <= 400; ca++) {
    for (int cb = 1; cb <= 400; cb++) {
      if (ca * mb == cb * ma) {
        ans = min(ans, dp[n][ca][cb]);
      }
    }
  }
  if (ans == 1e6) {
    ans = -1;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int a[50], b[50], c[50];
int dp[51][501][501];

int main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }

  for (int i = 0; i <= n; i++) {
    for (int ca = 0; ca <= 400; ca++) {
      for (int cb = 0; cb <= 400; cb++) {
        dp[i][ca][cb] = 1e6;
      }
    }
  }

  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int ca = 0; ca <= 400; ca++) {
      for (int cb = 0; cb <= 400; cb++) {
        if (dp[i][ca][cb] == 1e6)
          continue;
        dp[i + 1][ca][cb] = min(dp[i][ca][cb], dp[i + 1][ca][cb]);
        dp[i + 1][ca + a[i]][cb + b[i]] =
            min(dp[i + 1][ca + a[i]][cb + b[i]], dp[i][ca][cb] + c[i]);
      }
    }
  }

  int ans = 1e6;
  for (int ca = 1; ca <= 400; ca++) {
    for (int cb = 1; cb <= 400; cb++) {
      if (ca * mb == cb * ma) {
        ans = min(ans, dp[n][ca][cb]);
      }
    }
  }
  if (ans == 1e6) {
    ans = -1;
  }

  cout << ans << endl;

  return 0;
}