Problem: You are given an undirected unweighted graph with N vertices and M edges that contains neither self-loops nor double edges.   Here, a _self-loop_ is an edge where a_i = b_i (1≤i≤M), and _double edges_ are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≤i<j≤M).   How many different paths start from vertex 1 and visit all the vertices exactly once?   Here, the endpoints of a path are considered visited.  For example, let us assume that the following undirected graph shown in Figure 1 is given.    Figure 1: an example of an undirected graph  The following path shown in Figure 2 satisfies the condition.    Figure 2: an example of a path that satisfies the condition  However, the following path shown in Figure 3 does not satisfy the condition, because it does not visit all the vertices.    Figure 3: an example of a path that does not satisfy the condition  Neither the following path shown in Figure 4, because it does not start from vertex 1.    Figure 4: another example of a path that does not satisfy the condition

Submission attempts (in order):
from log import logger

N, M = list(map(int, input().split()))

key_value = {}
for i in range(M):
    a, b = input().split()
    try:
        key_value[a].append(b)
    except KeyError:
        key_value[a] = [b]
    try:
        key_value[b].append(a)
    except KeyError:
        key_value[b] = [a]

logger.info("key_value: {}\n".format(key_value))



node = "1"

open_list = [node]

closed_list = []

cnt = 0
while True:
    
    logger.info(
        "[before] open_list: {}, closed_list: {}".format(open_list, closed_list)
    )
    if len(open_list) == 0:
        break
    else:
        
        node = open_list[0]
        open_list = open_list[1:]
        closed_list.append(node)
        
        if sorted(closed_list) == list(map(str, range(1, N + 1))):
            cnt += 1
        else:
            
            try:
                children = key_value[node]
                for i in range(len(children)):
                    if children[i] not in open_list and children[i] not in closed_list:
                        open_list.append(children[i])
            except KeyError:
                pass
    logger.info(
        "[after] open_list: {}, closed_list: {}\n".format(open_list, closed_list)
    )
print(cnt)


from log import logger

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

logger.info('key_value: {}\n'.format(key_value))

closed_list = [False] * N
closed_list[0] = True
cnt = 0


def dfs(target):
    global cnt
    logger.info('key_value[target]: {}'.format(key_value[target]))
    if all(closed_list):
        cnt += 1
        return
    for node in key_value[target]:
        logger.info('node: {}'.format(node))
        
        if closed_list[node] is False:
            closed_list[node] = True
            dfs(node)
            closed_list[node] = False
        logger.info('closed_list: {}\n'.format(closed_list))
    return


dfs(0)
print(cnt)


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


closed_list = [False] * N
closed_list[0] = True
cnt = 0
level = 1


def dfs(target):
    global cnt, level
    if all(closed_list):
        cnt += 1
        return
    
    for node in open_list[target]:
        
        if closed_list[node] is False:
            
            closed_list[node] = True
            
            level += 1
            dfs(node)
            level = 1
            
            closed_list[node] = False
    return


dfs(0)
print(cnt)


Verdicts: Runtime Error

Runtime Error

Accepted