Problem p03805 (python)

Fixed submission:
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest
# Problem C


# See:
# https://atcoder.jp/img/abc054/editorial.pdf
def dfs(v):
    all_visited = True

    if False in visited:
        all_visited = False

    if all_visited:
        return 1

    count = 0

    for i in range(n):
        if graph[v][i] == 0:
            continue

        if visited[i]:
            continue

        visited[i] = True
        count += dfs(i)
        visited[i] = False
    return count


if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    graph = [[0 for __ in range(n)] for _ in range(n)]

    for i in range(m):
        a, b = list(map(lambda x: int(x) - 1, input().split()))
        graph[a][b] = 1
        graph[b][a] = 1

    visited = [False] * n
    visited[0] = True

    print(dfs(0))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest
# Problem C


# See:
# https://atcoder.jp/img/abc054/editorial.pdf
def dfs(v):
    all_visited = True

    if False in visited:
        all_visited = False

    if all_visited:
        return 1

    count = 0

    for i in range(n):
        if graph[v][i] == 0:
            continue

        if visited[i]:
            continue

        visited[i] = True
        count += dfs(i)
        visited[i] = False
    return count


if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    graph = [[0 for __ in range(m)] for _ in range(m)]

    for i in range(m):
        a, b = list(map(lambda x: int(x) - 1, input().split()))
        graph[a][b] = 1
        graph[b][a] = 1

    visited = [False] * n
    visited[0] = True

    print(dfs(0))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest
# Problem C


def dfs(pos, mask):
    if pos == n:
        if len(set(p)) == n:
            pass_count = 1

            for i in range(n - 1):
                if graph[p[i]][p[i + 1]] == 1:
                    pass_count += 1

            if pass_count == n:
                global count
                count += 1
        return

    # See:
    # https://www.youtube.com/watch?v=UFMvexj1hY4
    for i in range(n):
        if mask and (1 << i):  # i in mask.
            p[pos] = i
            dfs(pos + 1, mask ^ (1 << i))  # remove i from mask.


if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    ab = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(m)]
    graph = [[0 for __ in range(m)] for _ in range(m)]
    p = [0] * n
    count = 0

    for a, b in ab:
        graph[a][b] = 1

        if a != 0:
            graph[b][a] = 1

    dfs(0, (1 << n) - 1)  # permutation [0, n - 1]

    print(count)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest
# Problem C


def dfs(i: int):
    if visited[i]:
        return

    visited[i] = True

    for k in range(n):
        if graph[i][k] == 1:
            dfs(k)


if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    ab = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(m)]
    graph = [[0 for __ in range(m)] for _ in range(m)]

    for a, b in ab:
        graph[a][b] = 1
        graph[b][a] = 1

    count = 0

    for a, b in ab:
        visited = [False for _ in range(n)]

        if a == 0:
            dfs(a)

            if False not in visited:
                count += 1

    print(count)