Problem p03805 (python)

Fixed submission:
import itertools


def main():
    n, m = [int(i) for i in input().split()]
    path_matrix = [[False] * n for _ in range(n)]
    for _ in range(m):
        a, b = [int(i) for i in input().split()]
        a -= 1
        b -= 1
        path_matrix[a][b] = True
        path_matrix[b][a] = True

    ans = 0
    for path in itertools.permutations(range(n), n):
        if path[0] != 0:
            continue
        for i in range(n):
            if i == n - 1:
                ans += 1
                break
            if not path_matrix[path[i]][path[i+1]]:
                break

    print(ans)


if __name__ == "__main__":
    main()

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

N, M = [int(i) for i in input().split()]
adjacent = [[0] * N for _ in range(N)]
for _ in range(N):
    a, b = [int(i) for i in input().split()]
    adjacent[a - 1][b - 1] = 1
    adjacent[b - 1][a - 1] = 1

ans = 0
for p in permutations(range(N), r=N):
    if p[0] != 0:
        continue
    for i in range(1, N):
        f, t = p[i-1:i+1]
        if adjacent[f][t] != 1:
            break
    else:
        ans += 1
print(ans)

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

N, M = [int(i) for i in input().split()]
adjacent = [[0] * N for _ in range(N)]
for _ in range(N):
    a, b = [int(i) for i in input().split()]
    adjacent[a - 1][b - 1] = 1
    adjacent[b - 1][a - 1] = 1

ans = 0
for p in permutations(range(1, N), r=N-1):
    start, end = 0, p[0]
    if adjacent[start][end] != 1:
        continue
    for i in range(1, N-1):
        start, end = end, p[i]
        if adjacent[start][end] != 1:
            break
    else:
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 9)

def dfs(v, n, graph, visited):
    all_visited = 1

    for i in range(n):
        if visited[i] == 0:
            all_visited = 0
            break

    if all_visited:
        return 1

    n_paths = 0

    for i in range(0, n):
        if not graph[v][i]:
            continue
        if visited[i]:
            continue

        visited[i] = 1
        n_paths += dfs(i, n, graph, visited)
        visited[i] = 0

    return n_paths


def main():
    n, m = [int(i) for i in input().split()]
    if m == 0:
      print(0)
      return None
    graph = [[0] * m for _ in range(m)]
    for _ in range(m):
        a, b = [int(i) for i in input().split()]
        graph[a-1][b-1] = 1
        graph[b-1][a-1] = 1

    visited = [0] * n

    visited[0] = 1
    n_paths = dfs(0, n, graph, visited)
    print(n_paths)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 9)

def dfs(v, n, graph, visited):
    all_visited = 1

    for i in range(n):
        if visited[i] == 0:
            all_visited = 0
            break

    if all_visited:
        return 1

    n_paths = 0

    for i in range(0, n):
        if not graph[v][i]:
            continue
        if visited[i]:
            continue

        visited[i] = 1
        n_paths += dfs(i, n, graph, visited)
        visited[i] = 0

    return n_paths


def main():
    n, m = [int(i) for i in input().split()]
    graph = [[0] * m for _ in range(m)]
    for _ in range(m):
        a, b = [int(i) for i in input().split()]
        graph[a-1][b-1] = 1
        graph[b-1][a-1] = 1

    visited = [0] * n

    visited[0] = 1
    n_paths = dfs(0, n, graph, visited)
    print(n_paths)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def dfs(v, n, graph, visited):
    all_visited = 1

    for i in range(n):
        if visited[i] == 0:
            all_visited = 0
            break

    if all_visited:
        return 1

    n_paths = 0

    for i in range(0, n):
        if not graph[v][i]:
            continue
        if visited[i]:
            continue

        visited[i] = 1
        n_paths += dfs(i, n, graph, visited)
        visited[i] = 0

    return n_paths


def main():
    n, m = [int(i) for i in input().split()]
    graph = [[0] * m for _ in range(m)]
    for _ in range(m):
        a, b = [int(i) for i in input().split()]
        graph[a-1][b-1] = 1
        graph[b-1][a-1] = 1

    visited = [0] * n

    visited[0] = 1
    n_paths = dfs(0, n, graph, visited)
    print(n_paths)


if __name__ == "__main__":
    main()