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):
N,M=map(int,input().split())
original_paths={}
paths={}
for n in range(1,N+1):
    original_paths[n]=[]
for m in range(1,M+1):
    s,e=map(int,input().split())
    original_paths[s].append(e)
    original_paths[e].append(s)
    
visited=[1]
current_point=1
answer=0
import copy
paths=copy.deepcopy(original_paths)

while len(paths[1])!=0:
    print(current_point,visited,paths)
    if len(paths[current_point])!=0:
        next_point=paths[current_point][0]
    
        if next_point in visited:

            if len(paths[current_point])!=1:
                paths[current_point].remove(next_point)

            else:
                if len(visited)==N:
                    answer+=1
                prior_point=visited[-2]
                paths[current_point]=original_paths[current_point].copy()
                paths[prior_point].remove(current_point)
                visited.remove(current_point)
                current_point=prior_point

        else:
            visited.append(next_point)
            current_point=next_point

    else:
        prior_point=visited[-2]
        paths[current_point]=original_paths[current_point].copy()
        paths[prior_point].remove(current_point)
        visited.remove(current_point)
        current_point=prior_point
        
print(answer)


N,M=map(int,input().split())
original_paths={}
paths={}
for n in range(1,N+1):
    original_paths[n]=[]
    paths[n]=[]
for m in range(1,M+1):
    s,e=map(int,input().split())
    original_paths[s].append(e)
    original_paths[e].append(s)
    paths[s].append(e)
    paths[e].append(s)
    
visited=[1]
current_point=1
answer=0

while len(paths[1])!=0:
    print(current_point,visited,paths)
    if len(paths[current_point])!=0:
        next_point=paths[current_point][0]
    
        if next_point in visited:

            if len(paths[current_point])!=1:
                paths[current_point].remove(next_point)

            else:
                if len(visited)==N:
                    answer+=1
                prior_point=visited[-2]
                paths[current_point]=original_paths[current_point].copy()
                paths[prior_point].remove(current_point)
                visited.remove(current_point)
                current_point=prior_point

        else:
            visited.append(next_point)
            current_point=next_point

    else:
        prior_point=visited[-2]
        paths[current_point]=original_paths[current_point].copy()
        paths[prior_point].remove(current_point)
        visited.remove(current_point)
        current_point=prior_point
        
print(answer)


N,M=map(int,input().split())
original_paths={}
paths={}
for n in range(1,N+1):
    original_paths[n]=[]
    paths[n]=[]
for m in range(1,M+1):
    s,e=map(int,input().split())
    original_paths[s].append(e)
    original_paths[e].append(s)
    paths[s].append(e)
    paths[e].append(s)
    
visited=[1]
current_point=1
answer=0

while len(paths[1])!=0:
    
    if len(paths[current_point])!=0:
        next_point=paths[current_point][0]
    
        if next_point in visited:

            if len(paths[current_point])!=1:
                paths[current_point].remove(next_point)

            else:
                if len(visited)==N:
                    answer+=1
                prior_point=visited[-2]
                paths[current_point]=original_paths[current_point].copy()
                paths[prior_point].remove(current_point)
                visited.remove(current_point)
                current_point=prior_point

        else:
            visited.append(next_point)
            current_point=next_point

    else:
        prior_point=visited[-2]
        paths[current_point]=original_paths[current_point].copy()
        paths[prior_point].remove(current_point)
        visited.remove(current_point)
        current_point=prior_point
        
print(answer)


Verdicts: Wrong Answer

Wrong Answer

Accepted