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):
import networkx as nx
n,m=map(int,input().split())
s=[list(map(int,input().split())) for _ in range(m)]
g=nx.Graph()
g.add_edges_from(s)


l=[[] for _ in range(n)]
for i in range(m):
    a,b=s[i][0],s[i][1]
    l[a-1].append(b)
    l[b-1].append(a)

l1=sum(l, []) 
l1.insert(0,1)
l_depth=list(dict.fromkeys(l1))


k=list(nx.all_simple_paths(G, source=1, target=l_depth[-1]))

c=0
ma=0
for i in range(k):
    if len(k[i])>ma:
        ma=len(k[i])
        c-=c+1
    elif len(k[i])==ma:
        c+=1
print(c)

import networkx as nx
n,m=map(int,input().split())
s=[list(map(int,input().split())) for _ in range(m)]
g=nx.Graph()
g.add_edges_from(s)
 

l=[[] for _ in range(n)]
for i in range(m):
    a,b=s[i][0],s[i][1]
    l[a-1].append(b)
    l[b-1].append(a)

l1=sum(l, []) 
l1.insert(0,1)
l_depth=list(dict.fromkeys(l1))
 

k=list(nx.all_simple_paths(G, source=1, target=l_depth[-1]))
 
c=0
ma=0
for i in range(len(k)):
    if len(k[i])>ma:
        ma=len(k[i])
        c-=c+1
    elif len(k[i])==ma:
        c+=1
print(c)

import networkx as nx
n,m=map(int,input().split())
s=[list(map(int,input().split())) for _ in range(m)]
g=nx.Graph()
g.add_edges_from(s)
 

l=[[] for _ in range(n)]
for i in range(m):
    a,b=s[i][0],s[i][1]
    l[a-1].append(b)
    l[b-1].append(a)

l1=sum(l, []) 
l1.insert(0,1)
l_depth=list(dict.fromkeys(l1))
 

k=list(nx.all_simple_paths(g, source=1, target=l_depth[-1]))
 
c=0
ma=0
for i in range(len(k)):
    if len(k[i])>ma:
        ma=len(k[i])
        c-=c+1
    if len(k[i])==ma:
        c+=1
print(c)

import networkx as nx
n,m=map(int,input().split())
s=[list(map(int,input().split())) for _ in range(m)]
g=nx.Graph()
g.add_edges_from(s)
 

l=[[] for _ in range(n)]
for i in range(m):
    a,b=s[i][0],s[i][1]
    l[a-1].append(b)
    l[b-1].append(a)

l1=sum(l, []) 
l1.insert(0,1)
l_depth=list(dict.fromkeys(l1))
 

k=list(nx.all_simple_paths(g, source=1, target=l_depth[-1]))
 
c=0
ma=0
for i in range(len(k)):
    if len(k[i])>ma:
        ma=len(k[i])
        c-=c+1
    elif len(k[i])==ma:
        c+=1
print(c)

n,m=map(int,input().split())
s=[list(map(int,input().split())) for _ in range(m)]


l=[[] for _ in range(n)]
for i in range(m):
    a,b=s[i][0],s[i][1]
    l[a-1].append(b)
    l[b-1].append(a)

def dfs(now, prev, visited):
    visited.append(now)
    if len(visited) == n:
        return 1
 
    ret = 0 
    for edge in l[now-1]:
        if edge == prev:
            continue
        if edge in visited:
            continue
 
        ret += dfs(edge, prev, visited[:])
 
    return ret 

print(dfs(1, 0, []))

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted