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

Buggy submission (status: Runtime Error):
import sys
from collections import deque

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(N, M, inputs):
    relations = {}
    visited_all = (2**N) - 1
    for i in range(1, N + 1):
        relations[i] = []

    for i in inputs:
        [parent, child] = string_to_int(i)
        relations[parent].append(child)
        relations[child].append(parent)

    routes = 0
    queue = deque([[1, 0]])
    while len(queue) > 0:
        [node, visited] = queue.popleft()

        if visited & (1 << (node - 1)) != 0:
            continue

        visited = visited + (1 << (node - 1))

        if visited == visited_all:
            routes += 1
            continue

        for next in relations[node]:
            queue.append([next, visited])
    return routes


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, M] = string_to_int(input())
    ret = solve(inputs(M))
    print(ret)


Fixed submission:
import sys
from collections import deque

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(N, M, inputs):
    relations = {}
    visited_all = (2**N) - 1
    for i in range(1, N + 1):
        relations[i] = []

    for i in inputs:
        [parent, child] = string_to_int(i)
        relations[parent].append(child)
        relations[child].append(parent)

    routes = 0
    queue = deque([[1, 0]])
    while len(queue) > 0:
        [node, visited] = queue.popleft()

        if visited & (1 << (node - 1)) != 0:
            continue

        visited = visited + (1 << (node - 1))

        if visited == visited_all:
            routes += 1
            continue

        for next in relations[node]:
            queue.append([next, visited])
    return routes


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, M] = string_to_int(input())
    ret = solve(N, M, inputs(M))
    print(ret)
Problem p03805 (Python)

Buggy submission (status: Runtime Error):
import sys

sys.setrecursionlimit(10**6)

N, M = map(int, input().split())
graph = [set([]) for _ in range(M)]
for _ in range(M):
    a, b = map(int, input().split())
    graph[a - 1].add(b - 1)
    graph[b - 1].add(a - 1)


def dfs(graph, visited, v):
    if all(visited):
        return 1

    cnt = 0
    for u in graph[v]:
        if visited[u]:
            continue
        visited[u] = True
        cnt += dfs(graph, visited, u)
        visited[u] = False

    return cnt


visited = [False] * len(graph)
visited[0] = True
ans = dfs(graph, visited, 0)
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(10**6)

N, M = map(int, input().split())
graph = [set([]) for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    graph[a - 1].add(b - 1)
    graph[b - 1].add(a - 1)


def dfs(graph, visited, v):
    if all(visited):
        return 1

    cnt = 0
    for u in graph[v]:
        if visited[u]:
            continue
        visited[u] = True
        cnt += dfs(graph, visited, u)
        visited[u] = False

    return cnt


visited = [False] * len(graph)
visited[0] = True
ans = dfs(graph, visited, 0)
print(ans)
Problem p03805 (Python)

Buggy submission (status: Runtime Error):
n, m = [int(x) for x in input().split(" ")]

edges = set([tuple(input().split(" ")) for _ in range(n)])
edges.update([(x[1], x[0]) for x in list(edges)])


def dfs(node, history):
    ans = 0
    if len(history) == n - 1:
        return 1

    nextnodes = [
        x[1] for x in edges if x[1] not in history and x[0] == node and x[1] != node
    ]
    history.add(node)
    for next in nextnodes:
        ans += dfs(next, history)
    history.remove(node)
    return ans


print(dfs("1", set()))


Fixed submission:
n, m = [int(x) for x in input().split(" ")]

edges = set([tuple(input().split(" ")) for _ in range(m)])
edges.update([(x[1], x[0]) for x in list(edges)])


def dfs(node, history):
    ans = 0
    if len(history) == n - 1:
        return 1

    nextnodes = [
        x[1] for x in edges if x[1] not in history and x[0] == node and x[1] != node
    ]
    history.add(node)
    for next in nextnodes:
        ans += dfs(next, history)
    history.remove(node)
    return ans


print(dfs("1", set()))
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

bool G[8][8];

int dfs(int v, int N, bool visited[8]) {
  bool all = true;
  rep(i, N) if (visited[i] == false) all = false;
  if (all)
    return 1;

  int ret = 0;
  rep(i, N) {
    if (G[v][i] == false)
      continue;
    if (visited[i] == true)
      continue;

    visited[i] = true;
    ret += dfs(i, N, visited);
    visited[i] = false;
  }

  return ret;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  int a[M], b[M];
  rep(i, N) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    G[a[i]][b[i]] = true;
    G[b[i]][a[i]] = true;
  }

  bool visited[8];
  rep(i, 8) visited[i] = false;

  visited[0] = true;
  cout << dfs(0, N, visited) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

bool G[8][8];

int dfs(int v, int N, bool visited[8]) {
  bool all = true;
  rep(i, N) if (visited[i] == false) all = false;
  if (all)
    return 1;

  int ret = 0;
  rep(i, N) {
    if (G[v][i] == false)
      continue;
    if (visited[i] == true)
      continue;

    visited[i] = true;
    ret += dfs(i, N, visited);
    visited[i] = false;
  }

  return ret;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  int a[M], b[M];
  rep(i, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    G[a[i]][b[i]] = true;
    G[b[i]][a[i]] = true;
  }

  bool visited[8];
  rep(i, 8) visited[i] = false;

  visited[0] = true;
  cout << dfs(0, N, visited) << endl;

  return 0;
}
Problem p03805 (Python)

Buggy submission (status: Runtime Error):
from itertools import permutations

N, M = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(N):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    G[a].append(b)
    G[b].append(a)

res = 0
for P in permutations(range(N), N):
    if P[0] != 0:
        continue
    for i in range(N - 1):
        if P[i + 1] not in G[P[i]]:
            break
    else:
        res += 1

print(res)


Fixed submission:
from itertools import permutations

N, M = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    G[a].append(b)
    G[b].append(a)

res = 0
for P in permutations(range(N), N):
    if P[0] != 0:
        continue
    for i in range(N - 1):
        if P[i + 1] not in G[P[i]]:
            break
    else:
        res += 1

print(res)
Problem p03805 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  ll dp[(1 << n)][n];
  ll ndp[(1 << n)][n];

  rep(i, (1 << n)) rep(j, n) {
    dp[i][j] = 0;
    ndp[i][j] = 0;
  }
  dp[1][0] = 1;
  ndp[1][0] = 1;
  vector<int> g[n];
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  rep(h, n - 1) {
    rep(i, (1 << n)) rep(j, n) {
      if (i & 1) {
        for (auto l : g[j]) {
          if (i & (1 << l))
            continue;
          // cout << i << " " << j << " " << l <<endl;
          ndp[i | (1 << l)][l] += dp[i][j];
        }
      }
    }
    // cout << ndp[3][1] << endl;
    //     swap(dp, ndp);
    rep(i, (1 << n)) rep(j, n) dp[i][j] = ndp[i][j];
  }

  ll ans = 0;
  rep(j, n) ans += dp[(1 << n) - 1][j];
  cout << ans << endl;

  return 0;
}


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

int main() {
  int n, m;
  cin >> n >> m;
  ll dp[(1 << n)][n];
  ll ndp[(1 << n)][n];

  rep(i, (1 << n)) rep(j, n) {
    dp[i][j] = 0;
    ndp[i][j] = 0;
  }
  dp[1][0] = 1;
  ndp[1][0] = 1;
  vector<int> g[n];
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  rep(h, n - 1) {
    rep(i, (1 << n)) rep(j, n) {
      if (i & 1) {
        for (auto l : g[j]) {
          if (i & (1 << l))
            continue;
          // cout << i << " " << j << " " << l <<endl;
          ndp[i | (1 << l)][l] += dp[i][j];
        }
      }
    }
    // cout << ndp[3][1] << endl;
    //     swap(dp, ndp);
    rep(i, (1 << n)) rep(j, n) dp[i][j] = ndp[i][j];
  }

  ll ans = 0;
  rep(j, n) ans += dp[(1 << n) - 1][j];
  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 all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using p = pair<int, int>;

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

  vector<vector<int>> ab(M, vector<int>(2));
  rep(i, M) { cin >> ab.at(i).at(0) >> ab.at(i).at(1); }

  vector<int> v;
  rep(i, N) { v.push_back(i + 1); }

  bool total_flag;
  bool flag;
  int count = 0;
  do {
    if (v.at(0) != 1)
      break;
    total_flag = true;
    for (int i = 0; i < N - 1; i++) {
      flag = false;
      rep(j, N) {
        if (ab.at(j).at(0) == v.at(i) && ab.at(j).at(1) == v.at(i + 1)) {
          // cout << v.at(i) << ' ' << v.at(i+1) << endl;
          flag = true;
          continue;
        } else if (ab.at(j).at(1) == v.at(i) && ab.at(j).at(0) == v.at(i + 1)) {
          // cout << v.at(i) << ' ' << v.at(i+1) << endl;
          flag = true;
          continue;
        }
      }
      if (flag == false) {
        total_flag = false;
      }
    }
    if (total_flag == true) {
      count++;
    }
  } while (next_permutation(v.begin(), v.end()));
  cout << count << endl;
}

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;
using ll = long long;
using p = pair<int, int>;

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

  vector<vector<int>> ab(M, vector<int>(2));
  rep(i, M) { cin >> ab.at(i).at(0) >> ab.at(i).at(1); }

  vector<int> v;
  rep(i, N) { v.push_back(i + 1); }

  bool total_flag;
  bool flag;
  int count = 0;
  do {
    if (v.at(0) != 1)
      break;
    total_flag = true;
    for (int i = 0; i < N - 1; i++) {
      flag = false;
      rep(j, M) {
        if (ab.at(j).at(0) == v.at(i) && ab.at(j).at(1) == v.at(i + 1)) {
          // cout << v.at(i) << ' ' << v.at(i+1) << endl;
          flag = true;
          continue;
        } else if (ab.at(j).at(1) == v.at(i) && ab.at(j).at(0) == v.at(i + 1)) {
          // cout << v.at(i) << ' ' << v.at(i+1) << endl;
          flag = true;
          continue;
        }
      }
      if (flag == false) {
        total_flag = false;
      }
    }
    if (total_flag == true) {
      count++;
    }
  } while (next_permutation(v.begin(), v.end()));
  cout << count << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

int solve(const std::vector<std::vector<int>> &connection,
          std::vector<bool> &seenTable, int current) {
  if (seenTable[current])
    return 0;

  seenTable[current] = true;

  bool all = true;
  for (auto s : seenTable) {
    if (!s) {
      all = false;
      break;
    }
  }

  if (all) {
    seenTable[current] = false;
    return 1;
  }

  int result = 0;
  for (auto &c : connection[current]) {
    result += solve(connection, seenTable, c);
  }

  seenTable[current] = false;
  return result;
}

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<bool> seenTable(N, false);
  std::vector<std::vector<int>> connection(M);

  for (int i = 0; i < M; ++i) {
    int am, bm;
    std::cin >> am >> bm;
    connection[am - 1].emplace_back(bm - 1);
    connection[bm - 1].emplace_back(am - 1);
  }

  std::cout << solve(connection, seenTable, 0) << std::endl;
}


Fixed submission:
#include <iostream>
#include <vector>

int solve(const std::vector<std::vector<int>> &connection,
          std::vector<bool> &seenTable, int current) {
  if (seenTable[current])
    return 0;

  seenTable[current] = true;

  bool all = true;
  for (auto s : seenTable) {
    if (!s) {
      all = false;
      break;
    }
  }

  if (all) {
    seenTable[current] = false;
    return 1;
  }

  int result = 0;
  for (auto &c : connection[current]) {
    result += solve(connection, seenTable, c);
  }

  seenTable[current] = false;
  return result;
}

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<bool> seenTable(N, false);
  std::vector<std::vector<int>> connection(N);

  for (int i = 0; i < M; ++i) {
    int am, bm;
    std::cin >> am >> bm;
    connection[am - 1].emplace_back(bm - 1);
    connection[bm - 1].emplace_back(am - 1);
  }

  std::cout << solve(connection, seenTable, 0) << std::endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

#define ll long long
const ll MOD = 1e9 + 7;
const ll INF = 1 << 29;

ll DFS(std::vector<std::vector<ll>> &connection, std::vector<bool> &lookedTable,
       ll current) {
  if (lookedTable[current])
    return 0;

  lookedTable[current] = true;

  bool visitedAll = true;
  for (auto i = 0; i < lookedTable.size(); ++i)
    visitedAll &= lookedTable[i];

  if (visitedAll) {
    lookedTable[current] = false;
    return 1;
  }

  ll result = 0;
  for (auto &c : connection[current]) {
    result += DFS(connection, lookedTable, c);
  }

  lookedTable[current] = false;
  return result;
}

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

  std::vector<std::vector<ll>> connection(M);
  std::vector<bool> lookedTable(N);

  for (auto i = 0; i < M; ++i) {
    ll from, to;
    std::cin >> from >> to;
    from -= 1;
    to -= 1;
    connection[from].emplace_back(to);
    connection[to].emplace_back(from);
  }

  std::cout << DFS(connection, lookedTable, 0) << std::endl;
}


Fixed submission:
#include <iostream>
#include <vector>

#define ll long long
const ll MOD = 1e9 + 7;
const ll INF = 1 << 29;

ll DFS(std::vector<std::vector<ll>> &connection, std::vector<bool> &lookedTable,
       ll current) {
  if (lookedTable[current])
    return 0;

  lookedTable[current] = true;

  bool visitedAll = true;
  for (auto i = 0; i < lookedTable.size(); ++i)
    visitedAll &= lookedTable[i];

  if (visitedAll) {
    lookedTable[current] = false;
    return 1;
  }

  ll result = 0;
  for (auto &c : connection[current]) {
    result += DFS(connection, lookedTable, c);
  }

  lookedTable[current] = false;
  return result;
}

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

  std::vector<std::vector<ll>> connection(N);
  std::vector<bool> lookedTable(N);

  for (auto i = 0; i < M; ++i) {
    ll from, to;
    std::cin >> from >> to;
    from -= 1;
    to -= 1;
    connection[from].emplace_back(to);
    connection[to].emplace_back(from);
  }

  std::cout << DFS(connection, lookedTable, 0) << std::endl;
}
Problem p03805 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD 1000000000 + 7
#define INF 1000000000
typedef long long ll;
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {

  int n = 0, m = 0, a, b;
  int ans = 0, r = 0;
  vector<int> v;
  bool list[100][100] = {false};
  cin >> n >> m;
  rep(i, m) {
    cin >> a >> b;
    list[a - 1][b - 1] = true;
    list[b - 1][a - 1] = true;
  }
  rep(i, n) { v.push_back(i); }

  do {

    for (int i = 0; i < m - 1; i++) {

      if (list[v[i]][v[i + 1]] == true || list[v[i + 1]][v[i]] == true) {
        r++;
      }
    }

    if (r == n - 1 && v[0] == 0)
      ans++;

    r = 0;

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

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD 1000000000 + 7
#define INF 1000000000
typedef long long ll;
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {

  int n = 0, m = 0, a, b;
  int ans = 0, r = 0;
  vector<int> v;
  bool list[100][100] = {false};
  cin >> n >> m;
  rep(i, m) {
    cin >> a >> b;
    list[a - 1][b - 1] = true;
    list[b - 1][a - 1] = true;
  }
  rep(i, n) { v.push_back(i); }

  do {

    for (int i = 0; i < n - 1; i++) {

      if (list[v[i]][v[i + 1]] == true || list[v[i + 1]][v[i]] == true) {
        r++;
      }
    }

    if (r == n - 1 && v[0] == 0)
      ans++;

    r = 0;

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

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

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define REP(i, n, N) for (ll i = (n); i < (N); i++)
#define RREP(i, n, N) for (ll i = (N - 1); i >= (n); i--)
#define LREP(lst, itr) for (auto itr = lst.begin(); itr != lst.end(); ++itr)
#define CK(n, a, b) ((a) <= (n) && (n) < (b))
#define ALL(v) (v).begin(), (v).end()
#define MCP(a, b) memcpy(b, a, sizeof(b))
#define P(s) cout << (s) << endl
#define P2(a, b) cout << (a) << " " << (b) << endl
#define P3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define V2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
int main() {
  int N, M;
  cin >> N >> M;
  if (!M) {
    P(0);
    return 0;
  }
  vector<int> edges[M];
  REP(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  queue<pair<int, vector<int>>> que;
  vector<int> nodes(N, 0);
  nodes[0] = 1;
  que.push(make_pair(0, nodes));

  int ans = 0;
  while (!que.empty()) {
    pair<int, vector<int>> node = que.front();

    // P(node.first);
    // REP(i,0,N){
    //     cout << node.second[i];
    //     if(i!=N-1) cout << " ";
    // }
    // cout << endl;

    que.pop();
    if (accumulate(ALL(node.second), 0) == N) {
      ans++;
      continue;
    }
    for (auto e : edges[node.first]) {
      if (node.second[e] == 1) {
        continue;
      } else {
        node.second[e] = 1;
        que.push(make_pair(e, node.second));
        node.second[e] = 0;
      }
    }
  }

  P(ans);
}

Fixed submission:
#include "bits/stdc++.h"
#define REP(i, n, N) for (ll i = (n); i < (N); i++)
#define RREP(i, n, N) for (ll i = (N - 1); i >= (n); i--)
#define LREP(lst, itr) for (auto itr = lst.begin(); itr != lst.end(); ++itr)
#define CK(n, a, b) ((a) <= (n) && (n) < (b))
#define ALL(v) (v).begin(), (v).end()
#define MCP(a, b) memcpy(b, a, sizeof(b))
#define P(s) cout << (s) << endl
#define P2(a, b) cout << (a) << " " << (b) << endl
#define P3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define V2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
int main() {
  int N, M;
  cin >> N >> M;
  if (!M) {
    P(0);
    return 0;
  }
  vector<int> edges[N];
  REP(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  queue<pair<int, vector<int>>> que;
  vector<int> nodes(N, 0);
  nodes[0] = 1;
  que.push(make_pair(0, nodes));

  int ans = 0;
  while (!que.empty()) {
    pair<int, vector<int>> node = que.front();

    // P(node.first);
    // REP(i,0,N){
    //     cout << node.second[i];
    //     if(i!=N-1) cout << " ";
    // }
    // cout << endl;

    que.pop();
    if (accumulate(ALL(node.second), 0) == N) {
      ans++;
      continue;
    }
    for (auto e : edges[node.first]) {
      if (node.second[e] == 1) {
        continue;
      } else {
        node.second[e] = 1;
        que.push(make_pair(e, node.second));
        node.second[e] = 0;
      }
    }
  }

  P(ans);
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, ans = 0;
  cin >> N >> M;
  vector<int> P(N);
  vector<set<int>> AB(N);
  for (int i = 0; i <= N; i++)
    P[i] = i;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    AB[a - 1].insert(b - 1), AB[b - 1].insert(a - 1);
  }
  do {
    for (int i = 0; i < N - 1; i++)
      if (AB[P[i]].count(P[i + 1]) == false)
        goto skip;
    ans++;
  skip:;
  } while (next_permutation(P.begin() + 1, P.end()));
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, ans = 0;
  cin >> N >> M;
  vector<int> P(N);
  vector<set<int>> AB(N);
  for (int i = 0; i < N; i++)
    P[i] = i;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    AB[a - 1].insert(b - 1), AB[b - 1].insert(a - 1);
  }
  do {
    for (int i = 0; i < N - 1; i++)
      if (AB[P[i]].count(P[i + 1]) == false)
        goto skip;
    ans++;
  skip:;
  } while (next_permutation(P.begin() + 1, P.end()));
  cout << ans << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))   // 最大値を求める
#define MIN(x) *min_element(ALL(x))   // 最小値を求める
#define INF 1e9
typedef long long ll;
typedef long double ld;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> edges(n + 1);
  int a, b;
  REP(i, m) {
    cin >> a >> b;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  vector<int> nodes(n - 1);
  REP(i, n) nodes[i] = i + 2;
  int cnt = 0;
  if (SIZE(nodes) == 1) {
    cnt = 1;
  } else {
    do {
      bool can = true;
      int from = 1;
      REP(i, n - 1) {
        int to = nodes[i];
        if (find(ALL(edges[from]), to) != edges[from].end()) {
          from = to;
        } else {
          can = false;
          break;
        }
      }
      if (can)
        cnt++;
    } while (next_permutation(ALL(nodes)));
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))   // 最大値を求める
#define MIN(x) *min_element(ALL(x))   // 最小値を求める
#define INF 1e9
typedef long long ll;
typedef long double ld;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> edges(n + 1);
  int a, b;
  REP(i, m) {
    cin >> a >> b;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  vector<int> nodes(n - 1);
  REP(i, n - 1) nodes[i] = i + 2;
  int cnt = 0;
  if (SIZE(nodes) == 1) {
    cnt = 1;
  } else {
    do {
      bool can = true;
      int from = 1;
      REP(i, n - 1) {
        int to = nodes[i];
        if (find(ALL(edges[from]), to) != edges[from].end()) {
          from = to;
        } else {
          can = false;
          break;
        }
      }
      if (can)
        cnt++;
    } while (next_permutation(ALL(nodes)));
  }
  cout << cnt << 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<int> nodes, edges[8];
  for (int i = 2; i <= N; i++) {
    nodes.push_back(i);
  }
  for (int i = 0; i < M; i++) {
    int le, ri;
    cin >> le >> ri;
    edges[le].push_back(ri);
    edges[ri].push_back(le);
  }

  int ans = 0, donecou, nodenum;
  do {
    nodenum = 1;
    donecou = 0;
    for (int oi = 0; oi < nodes.size(); oi++) {
      bool flag = false;
      for (int i = 0; i < edges[nodenum].size(); i++) {
        if (edges[nodenum][i] == nodes[oi])
          flag = true;
      }
      if (!flag)
        break;
      nodenum = nodes[oi];
      donecou++;
    }
    if (donecou == nodes.size())
      ans++;
  } while (next_permutation(nodes.begin(), nodes.end()));

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> nodes, edges[10];
  for (int i = 2; i <= N; i++) {
    nodes.push_back(i);
  }
  for (int i = 0; i < M; i++) {
    int le, ri;
    cin >> le >> ri;
    edges[le].push_back(ri);
    edges[ri].push_back(le);
  }

  int ans = 0, donecou, nodenum;
  do {
    nodenum = 1;
    donecou = 0;
    for (int oi = 0; oi < nodes.size(); oi++) {
      bool flag = false;
      for (int i = 0; i < edges[nodenum].size(); i++) {
        if (edges[nodenum][i] == nodes[oi])
          flag = true;
      }
      if (!flag)
        break;
      nodenum = nodes[oi];
      donecou++;
    }
    if (donecou == nodes.size())
      ans++;
  } while (next_permutation(nodes.begin(), nodes.end()));

  cout << ans << endl;

  return 0;
}
Problem p03805 (C++)

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

#define ll long long
#define ull unsigned long long
#define rep(i, N) for (int i = 0; i < N; i++)

// 変数群
int N, M;
vector<int> vec;
int ans = 0;
vector<pair<int, int>> ab;

bool joint(int a, int b) {
  if (a > b) {
    int tmp = a;
    a = b;
    b = tmp;
  }

  rep(i, M) {
    if (ab[i].first == a && ab[i].second == b) {
      return true;
    }
  }
  return false;
}

int judge() {

  bool flag = true;
  rep(i, N - 1) {
    if (!joint(vec[i], vec[i + 1]))
      flag = false;
  }

  if (flag) {
    // cout << "true" << endl;
    return 1;
  } else {
    // cout << "false" << endl;
    return 0;
  }
}

// 解法
void solve(int num) {

  // 値を繰り返し、前に出ていた値なら次へ
  rep(i, N) {

    bool flag = true;
    rep(j, num) {
      if (vec[j] == i)
        flag = false;
      ;
    }
    if (!flag)
      continue;

    vec[num] = i;

    if (num == N - 1) {
      rep(i, N) {
        // cout << vec[i] << "->";
      }
      ans += judge();
    } else {
      solve(num + 1);
    }
  }
}

int main() {
  // 入力
  cin >> N >> M;
  vec.resize(N);
  ab.resize(N);
  rep(i, M) {
    cin >> ab[i].first >> ab[i].second;
    ab[i].first--;
    ab[i].second--;
  }
  vec[0] = 0;
  solve(1);
  cout << ans << endl;
  return 0;
}

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

#define ll long long
#define ull unsigned long long
#define rep(i, N) for (int i = 0; i < N; i++)

// 変数群
int N, M;
vector<int> vec;
int ans = 0;
vector<pair<int, int>> ab;

bool joint(int a, int b) {
  if (a > b) {
    int tmp = a;
    a = b;
    b = tmp;
  }

  rep(i, M) {
    if (ab[i].first == a && ab[i].second == b) {
      return true;
    }
  }
  return false;
}

int judge() {

  bool flag = true;
  rep(i, N - 1) {
    if (!joint(vec[i], vec[i + 1]))
      flag = false;
  }

  if (flag) {
    // cout << "true" << endl;
    return 1;
  } else {
    // cout << "false" << endl;
    return 0;
  }
}

// 解法
void solve(int num) {

  // 値を繰り返し、前に出ていた値なら次へ
  rep(i, N) {

    bool flag = true;
    rep(j, num) {
      if (vec[j] == i)
        flag = false;
      ;
    }
    if (!flag)
      continue;

    vec[num] = i;

    if (num == N - 1) {
      rep(i, N) {
        // cout << vec[i] << "->";
      }
      ans += judge();
    } else {
      solve(num + 1);
    }
  }
}

int main() {
  // 入力
  cin >> N >> M;
  vec.resize(N);
  ab.resize(M);
  rep(i, M) {
    cin >> ab[i].first >> ab[i].second;
    ab[i].first--;
    ab[i].second--;
  }
  vec[0] = 0;
  solve(1);
  cout << ans << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)

int main() {
  int N, M;
  cin >> N >> M;
  vector<set<int>> G(N);
  vector<int> v(N);
  FOR(i, 0, N) v[i] = i;
  int a, b;
  FOR(i, 0, N) {
    cin >> a >> b;
    a--;
    b--;
    G[a].insert(b);
    G[b].insert(a);
  }
  int res = 0;
  do {
    bool f = true;
    FOR(i, 0, N - 1) {
      if (G[v[i]].find(v[i + 1]) == G[v[i]].end())
        f = false;
    }
    if (f)
      res++;
  } while (next_permutation(v.begin() + 1, v.end()));
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)

int main() {
  int N, M;
  cin >> N >> M;
  vector<set<int>> G(N);
  vector<int> v(N);
  FOR(i, 0, N) v[i] = i;
  int a, b;
  FOR(i, 0, M) {
    cin >> a >> b;
    a--;
    b--;
    G[a].insert(b);
    G[b].insert(a);
  }
  int res = 0;
  do {
    bool f = true;
    FOR(i, 0, N - 1) {
      if (G[v[i]].find(v[i + 1]) == G[v[i]].end())
        f = false;
    }
    if (f)
      res++;
  } while (next_permutation(v.begin() + 1, v.end()));
  cout << res << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep3(i, a, b) for (int i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define queint queue<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pq priority_queue<int>
#define pqg priority_queue<int, vector<int>, greater<int>()>
#define pb push_back
#define vec vector<int>
#define vecvec vector<vector<int>>
#define vecll vector<ll>
#define vecvecll vector<vector<ll>>
#define bs binary_search
#define All(c) (c).begin(), (c).end()
#define mp make_pair
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
int main() {
  int n = in(), m = in();
  vec a(n - 1);
  rep(i, n) a[i] = i + 1;
  vector<vector<bool>> bl(n, vector<bool>(n));
  rep(i, m) {
    int x = in() - 1, y = in() - 1;
    bl[y][x] = bl[x][y] = true;
  }
  int ans = 0;
  do {
    bool flag = true;
    if (!bl[0][a[0]])
      flag = false;
    rep(i, n - 2) if (!bl[a[i]][a[i + 1]]) flag = false;
    if (flag)
      ans++;
  } while (next_permutation(All(a)));
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep3(i, a, b) for (int i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define queint queue<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pq priority_queue<int>
#define pqg priority_queue<int, vector<int>, greater<int>()>
#define pb push_back
#define vec vector<int>
#define vecvec vector<vector<int>>
#define vecll vector<ll>
#define vecvecll vector<vector<ll>>
#define bs binary_search
#define All(c) (c).begin(), (c).end()
#define mp make_pair
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
int main() {
  int n = in(), m = in();
  vec a(n - 1);
  rep(i, n - 1) a[i] = i + 1;
  vector<vector<bool>> bl(n, vector<bool>(n));
  rep(i, m) {
    int x = in() - 1, y = in() - 1;
    bl[y][x] = bl[x][y] = true;
  }
  int ans = 0;
  do {
    bool flag = true;
    if (!bl[0][a[0]])
      flag = false;
    rep(i, n - 2) if (!bl[a[i]][a[i + 1]]) flag = false;
    if (flag)
      ans++;
  } while (next_permutation(All(a)));
  cout << ans << endl;
}
Problem p03805 (C++)

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

using namespace std;

void dfs(int now, int bit, int complete, int &cnt, vector<vector<int>> &g) {
  bit |= (1 << now);
  if (bit == complete) {
    ++cnt;
    return;
  }
  for (const auto &x : g[now]) {
    if ((bit >> x) & 1)
      continue;
    dfs(x, bit, complete, cnt, g);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(m);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int cnt = 0;
  dfs(0, 0, (1 << n) - 1, cnt, g);
  cout << cnt << endl;
  return 0;
}

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

using namespace std;

void dfs(int now, int bit, int complete, int &cnt, vector<vector<int>> &g) {
  bit |= (1 << now);
  if (bit == complete) {
    ++cnt;
    return;
  }
  for (const auto &x : g[now]) {
    if ((bit >> x) & 1)
      continue;
    dfs(x, bit, complete, cnt, g);
  }
}

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

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

bool all_seen(vector<bool> seen) {
  auto f = [](bool v_seen) { return v_seen == true; };
  return all_of(seen.begin(), seen.end(), f);
}

void dfs(int &cnt, vector<vector<int>> &graph, vector<bool> seen, int v) {
  // 頂点vを訪問済みとする
  seen.at(v) = true;

  // ベースケース
  // 全頂点を巡回できる経路の場合、インクリメントする
  if (all_seen(seen)) {
    cnt++;
  }

  // 再帰ステップ
  // 頂点vから行ける各頂点 next_v について DFS で探索する
  for (auto next_v : graph.at(v)) {
    // next_v が探索済みの場合はスキップ
    if (seen.at(next_v) == true) {
      continue;
    }
    dfs(cnt, graph, seen, next_v);
  }
}

int main() {
  // 頂点数と辺数
  int n, m;
  cin >> n >> m;

  // グラフ入力受取
  vector<vector<int>> graph(n);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--; // 0-indexed
    graph.at(a).push_back(b);
    // 無向グラフのため、逆方向の辺も true とする
    graph.at(b).push_back(a);
  }

#ifndef ONLINE_JUDGE
  for (int i = 0; i < n; i++) {
    cout << "# loop start" << endl;
    for (auto a : graph.at(i)) {
      cout << i << " " << a << endl;
    }
  }
#endif

  // 全頂点を「未訪問」に初期化する
  vector<bool> seen(n, false);

  // 頂点0 (0-indexed) を始点として探索を開始
  int cnt = 0;
  dfs(cnt, graph, seen, 0);

  cout << cnt << endl;
  return 0;
}


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

bool all_seen(vector<bool> seen) {
  auto f = [](bool v_seen) { return v_seen == true; };
  return all_of(seen.begin(), seen.end(), f);
}

void dfs(int &cnt, vector<vector<int>> &graph, vector<bool> seen, int v) {
  // 頂点vを訪問済みとする
  seen.at(v) = true;

  // ベースケース
  // 全頂点を巡回できる経路の場合、インクリメントする
  if (all_seen(seen)) {
    cnt++;
  }

  // 再帰ステップ
  // 頂点vから行ける各頂点 next_v について DFS で探索する
  for (auto next_v : graph.at(v)) {
    // next_v が探索済みの場合はスキップ
    if (seen.at(next_v) == true) {
      continue;
    }
    dfs(cnt, graph, seen, next_v);
  }
}

int main() {
  // 頂点数と辺数
  int n, m;
  cin >> n >> m;

  // グラフ入力受取
  vector<vector<int>> graph(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--; // 0-indexed
    graph.at(a).push_back(b);
    // 無向グラフのため、逆方向の辺も true とする
    graph.at(b).push_back(a);
  }

#ifndef ONLINE_JUDGE
  for (int i = 0; i < n; i++) {
    cout << "# loop start" << endl;
    for (auto a : graph.at(i)) {
      cout << i << " " << a << endl;
    }
  }
#endif

  // 全頂点を「未訪問」に初期化する
  vector<bool> seen(n, false);

  // 頂点0 (0-indexed) を始点として探索を開始
  int cnt = 0;
  dfs(cnt, graph, seen, 0);

  cout << cnt << 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 vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define mod 1000000007
using ll = long long;
using namespace std;
int all = 0;
int check(vvi &root, int current, int checked) {
  if (checked == all)
    return 1;
  int total = 0;
  for (int num : root[current]) {
    if (!(checked & (1 << num))) {
      checked |= (1 << num);
      total += check(root, num, checked);
      checked &= ~(1 << num);
    }
  }
  return total;
}
int main() {
  int n, m;
  cin >> n >> m;
  vvi root(n);
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    root[a].push_back(b);
    root[b].push_back(a);
  }
  rep(i, n) {
    all++;
    all <<= 1;
  }
  all >>= 1;
  int checked = 1;
  cout << check(root, 0, checked) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define mod 1000000007
using ll = long long;
using namespace std;
int all = 0;
int check(vvi &root, int current, int checked) {
  if (checked == all)
    return 1;
  int total = 0;
  for (int num : root[current]) {
    if (!(checked & (1 << num))) {
      checked |= (1 << num);
      total += check(root, num, checked);
      checked &= ~(1 << num);
    }
  }
  return total;
}
int main() {
  int n, m;
  cin >> n >> m;
  vvi root(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    root[a].push_back(b);
    root[b].push_back(a);
  }
  rep(i, n) {
    all++;
    all <<= 1;
  }
  all >>= 1;
  int checked = 1;
  cout << check(root, 0, checked) << endl;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for (ll i = (a); i > (b); --i)
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define dump(x) cout << #x << " = " << (x) << endl;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<int> A;
  REP(i, N) { A.push_back(i); }
  vector<vector<bool>> graph(M, vector<bool>(M, false));
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    graph[a][b] = true;
    graph[b][a] = true;
  }

  int ans = 0;
  do {
    if (A[0] != 0)
      break;
    bool ok = true;
    REP(i, N - 1) {
      if (!graph[A[i]][A[i + 1]]) {
        ok = false;
        break;
      }
    }
    if (ok) {
      ans++;
    }
  } while (next_permutation(all(A)));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for (ll i = (a); i > (b); --i)
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define dump(x) cout << #x << " = " << (x) << endl;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<int> A;
  REP(i, N) { A.push_back(i); }
  vector<vector<bool>> graph(N, vector<bool>(N, false));
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    graph[a][b] = true;
    graph[b][a] = true;
  }

  int ans = 0;
  do {
    if (A[0] != 0)
      break;
    bool ok = true;
    REP(i, N - 1) {
      if (!graph[A[i]][A[i + 1]]) {
        ok = false;
        break;
      }
    }
    if (ok) {
      ans++;
    }
  } while (next_permutation(all(A)));
  cout << ans << endl;
  return 0;
}
Problem p03805 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

using namespace std;
int G[9][9];

int main() {
  int N, M;
  int ans = 0;
  cin >> N >> M;
  int A[8], B[8];

  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    G[A[i]][B[i]] = 1;
    G[B[i]][A[i]] = 1;
  }

  vector<int> v(N);

  for (int i = 0; i < N; i++) {
    v[i] = i + 1;
  }

  do {
    if (v[0] == 1) {
      int flag = 1;
      for (int i = 0; i < N - 1; i++) {
        if (G[v[i]][v[i + 1]] == 0)
          flag = 0;
      }
      if (flag == 1)
        ans++;
    }

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

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

using namespace std;
int G[9][9];

int main() {
  int N, M;
  int ans = 0;
  cin >> N >> M;
  int A[50], B[50];

  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    G[A[i]][B[i]] = 1;
    G[B[i]][A[i]] = 1;
  }

  vector<int> v(N);

  for (int i = 0; i < N; i++) {
    v[i] = i + 1;
  }

  do {
    if (v[0] == 1) {
      int flag = 1;
      for (int i = 0; i < N - 1; i++) {
        if (G[v[i]][v[i + 1]] == 0)
          flag = 0;
      }
      if (flag == 1)
        ans++;
    }

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

  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)++)

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

vector<vector<bool>> G;

bool solve(vector<int> v, int index) {
  if (index == v.size() - 1)
    return true;
  else if (G[v[index]][v[index + 1]] == true)
    return solve(v, index + 1);
  else
    return false;
}

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

  G.resize(N);
  for (int i = 0; i < N; i++)
    G[i].resize(N);

  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      G[i][j] = false;

  for (int i = 0; i < M; i++) {
    ll v1, v2;
    cin >> v1 >> v2;
    G[v1 - 1][v2 - 1] = true;
    G[v2 - 1][v1 - 1] = true;
  }

  vector<int> v(N);
  for (int i = 0; i < M; i++)
    v[i] = i;

  ll count = 0;
  do {
    if (solve(v, 0)) {
      //	  for(int i=0; i<v.size(); i++)
      // cout << v[i] <<" " ;
      cout << endl;
      count++;
    }
  } while (next_permutation(v.begin() + 1, v.end()));

  cout << count << endl;

  return 0;
}

// cout << setprecision(13);
// next_permutation();
//__gcd();
// reverse();
// set ,tuple ,pair;
// bitset
// vector.find
// vector.count


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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

vector<vector<bool>> G;

bool solve(vector<int> v, int index) {
  if (index == v.size() - 1)
    return true;
  else if (G[v[index]][v[index + 1]] == true)
    return solve(v, index + 1);
  else
    return false;
}

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

  G.resize(N);
  for (int i = 0; i < N; i++)
    G[i].resize(N);

  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      G[i][j] = false;

  for (int i = 0; i < M; i++) {
    ll v1, v2;
    cin >> v1 >> v2;
    G[v1 - 1][v2 - 1] = true;
    G[v2 - 1][v1 - 1] = true;
  }

  vector<int> v(N);
  for (int i = 0; i < N; i++)
    v[i] = i;

  ll count = 0;
  do {
    if (solve(v, 0)) {
      //	  for(int i=0; i<v.size(); i++)
      // cout << v[i] <<" " ;
      cout << endl;
      count++;
    }
  } while (next_permutation(v.begin() + 1, v.end()));

  cout << count << endl;

  return 0;
}

// cout << setprecision(13);
// next_permutation();
//__gcd();
// reverse();
// set ,tuple ,pair;
// bitset
// vector.find
// vector.count
Problem p03805 (C++)

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

static const int maxN = 8, maxM = 32;

bool checkPath(const vector<int> &node, const int edgeList[maxN][maxN]) {
  // vectorの隣のパスが存在するかを調べる
  bool flag = true;
  for (int i = 0; i < node.size() - 1; i++) {
    if (node[0] != 0) {
      flag = false;
      break;
    } else if (edgeList[node[i]][node[i + 1]] == 0) {
      flag = false;
      break;
    }
  }
  return flag;
}

int main(void) {
  int N, M;
  int a[maxM], b[maxM];
  int edgeList[maxN][maxN];
  vector<int> node;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
  }
  // 0~N-1に添字を一個ずらす
  for (int i = 0; i < M; i++) {
    a[i] = a[i] - 1, b[i] = b[i] - 1;
  }
  for (int i = 0; i < N; i++) {
    node.push_back(i); // 0~N-1をストックする。
  }
  // EdgeListでエッジの情報を管理
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      edgeList[i][j] = 0;
    }
  }
  for (int i = 0; i < N; i++) {
    edgeList[a[i]][b[i]]++;
    edgeList[b[i]][a[i]]++;
    // cout << node[i] << endl;
  }
  /*for(int i = 0; i < N; i++){
    for(int j = 0; j < N; j++){
      if(edgeList[i][j] != 0) cout << i << " " << j << " " << edgeList[i][j] <<
  endl;
    }
  }*/

  // N個の並び替えを行なって，パスが存在するかをチェックしてみる。
  int cntPath = 0;
  do {
    if (checkPath(node, edgeList))
      cntPath++;
  } while (next_permutation(node.begin(), node.end()));
  cout << cntPath << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

static const int maxN = 8, maxM = 32;

bool checkPath(const vector<int> &node, const int edgeList[maxN][maxN]) {
  // vectorの隣のパスが存在するかを調べる
  bool flag = true;
  for (int i = 0; i < node.size() - 1; i++) {
    if (node[0] != 0) {
      flag = false;
      break;
    } else if (edgeList[node[i]][node[i + 1]] == 0) {
      flag = false;
      break;
    }
  }
  return flag;
}

int main(void) {
  int N, M;
  int a[maxM], b[maxM];
  int edgeList[maxN][maxN];
  vector<int> node;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
  }
  // 0~N-1に添字を一個ずらす
  for (int i = 0; i < M; i++) {
    a[i] = a[i] - 1, b[i] = b[i] - 1;
  }
  for (int i = 0; i < N; i++) {
    node.push_back(i); // 0~N-1をストックする。
  }
  // EdgeListでエッジの情報を管理
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      edgeList[i][j] = 0;
    }
  }
  for (int i = 0; i < M; i++) {
    edgeList[a[i]][b[i]]++;
    edgeList[b[i]][a[i]]++;
    // cout << node[i] << endl;
  }
  /*for(int i = 0; i < N; i++){
    for(int j = 0; j < N; j++){
      if(edgeList[i][j] != 0) cout << i << " " << j << " " << edgeList[i][j] <<
  endl;
    }
  }*/

  // N個の並び替えを行なって，パスが存在するかをチェックしてみる。
  int cntPath = 0;
  do {
    if (checkPath(node, edgeList))
      cntPath++;
  } while (next_permutation(node.begin(), node.end()));
  cout << cntPath << endl;
  return 0;
}