s_id stringlengths 10 10 | p_id stringlengths 6 6 | u_id stringlengths 10 10 | date stringlengths 10 10 | language stringclasses 1
value | original_language stringclasses 11
values | filename_ext stringclasses 1
value | status stringclasses 1
value | cpu_time stringlengths 1 5 | memory stringlengths 1 7 | code_size stringlengths 1 6 | code stringlengths 1 539k |
|---|---|---|---|---|---|---|---|---|---|---|---|
s329791591 | p03805 | u119655368 | 1583890997 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 524 | n, m = map(int, input().split())
l = [list(map(int, input().split())) for i in range(m)]
r = [[] for i in range(n + 1)]
ans = 0
for i in range(m):
r[l[i][0]].append(l[i][1])
r[l[i][1]].append(l[i][0])
p = []
for i in range(n):
p.append(i + 1)
p = list(itertools.permutations(p))
for i in range(len(p)):
check = True
t = list(p[i])
if t[0] != 1:
check = False
for j in range(len(t)-1):
if not t[j + 1] in r[t[j]]:
check = False
if check:
ans += 1
print(ans) |
s271765711 | p03805 | u686036872 | 1583737313 | Python | PyPy3 (2.4.0) | py | Runtime Error | 176 | 38256 | 498 | import sys
sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限
N, M = map(int, input().split())
A = [[0]* N for _ in range(N)]
for i in range(M):
a, b = map(int, input().split())
A[a-1][b] = 1
A[b-1][a] = 1
visited=[0]*N
ans = 0
def DFS(x):
if not 0 in visited:
return 1
for i in range(N):
if not 1 in A[x][i]:
return
if visited[x] == 1:
return
visited[x] = 1
ans += DFS(i)
return ans
print(DFS(0)) |
s938258035 | p03805 | u686036872 | 1583737134 | Python | PyPy3 (2.4.0) | py | Runtime Error | 184 | 40432 | 481 | import sys
sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限
N, M = map(int, input().split())
A = [0]*(N+1)
for i in range(M):
a, b = map(int, input().split())
A[a-1][b] = 1
A[b-1][a] = 1
visited=[0]*N
ans = 0
def DFS(x):
if not 0 in visited:
return 1
for i in range(N):
if not 1 in A[x][i]:
return
if visited[x] == 1:
return
visited[x] = 1
ans += DFS(i)
return ans
print(DFS(0)) |
s244671464 | p03805 | u686036872 | 1583735593 | Python | PyPy3 (2.4.0) | py | Runtime Error | 167 | 38384 | 431 | import sys
sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限
N, M = map(int, input().split())
A = [0]*(N+1)
for i in range(M):
a, b = map(int, input().split())
A[a].append([b])
A[b].append([a])
visited=[1]+[0]*N
ans = 0
def DFS(x):
if visited[x] == 1:
return
if sum(visited) == N:
ans += 1
visited[x] = 1
for i in range(N):
DFS(A[x][i])
return ans
print(DFS(1)) |
s025268513 | p03805 | u686036872 | 1583735227 | Python | PyPy3 (2.4.0) | py | Runtime Error | 170 | 38640 | 413 | import sys
sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限
N, M = map(int, input().split())
A = [0]*(N+1)
for i in range(M):
a, b = map(int, input().split())
A[a].append([b])
A[b].append([a])
visited=[1]+[0]*N
ans = 0
def DFS(x):
if visited[x] == 1:
return
if sum(visited) == N:
ans += 1
visited[x] = 1
for i in range(N):
DFS(A[x][i])
print(ans) |
s850390693 | p03805 | u112317104 | 1583347841 | Python | Python (3.4.3) | py | Runtime Error | 21 | 3316 | 601 | from collections import deque
def dfs(v, P, visited, N):
if len(list(filter(lambda x: x == True, visited))) == N:
return 1
c = 0
for i in P[v]:
if visited[i]: continue
visited[i] = True
c += dfs(i, P, visited, N)
visited[i] = False
return c
def resolve():
N, M = map(int, input().split())
P = [[] for _ in range(M)]
for i in range(N):
pp, ss = map(int, input().split())
P[pp-1].append(ss-1)
P[ss-1].append(pp-1)
visited = [False] * N
visited[0] = True
print(dfs(0, P, visited, N))
resolve() |
s482971956 | p03805 | u112317104 | 1583347721 | Python | Python (3.4.3) | py | Runtime Error | 21 | 3316 | 601 | from collections import deque
def dfs(v, P, visited, N):
if len(list(filter(lambda x: x == True, visited))) == N:
return 1
c = 0
for i in P[v]:
if visited[i]: continue
visited[i] = True
c += dfs(i, P, visited, N)
visited[i] = False
return c
def resolve():
N, M = map(int, input().split())
P = [[] for _ in range(N)]
for i in range(N):
pp, ss = map(int, input().split())
P[pp-1].append(ss-1)
P[ss-1].append(pp-1)
visited = [False] * N
visited[0] = True
print(dfs(0, P, visited, N))
resolve() |
s344159890 | p03805 | u995062424 | 1583283817 | Python | Python (3.4.3) | py | Runtime Error | 26 | 3064 | 406 | N, M = map(int, input().split())
dict = {i+1:[] for i in range(M)}
for _ in range(M):
a, b = map(int, input().split())
dict[a].append(b)
dict[b].append(a)
def DFS(visited, sight, cnt):
if(len(visited) == N):
return cnt+1
for num in dict[sight]:
if num in visited:
continue
cnt = DFS(visited+[num], num, cnt)
return cnt
print(DFS([1], 1, 0)) |
s471541270 | p03805 | u593934357 | 1583274316 | Python | PyPy3 (2.4.0) | py | Runtime Error | 300 | 66776 | 1061 | import sys
from math import *
from collections import deque, Counter, defaultdict
from fractions import gcd
from itertools import permutations
input = lambda: sys.stdin.readline().rstrip()
def eprint(s):
sys.stderr.write('DEBUG: {}'.format(s))
return
def main():
n,m = map(int, input().split())
graph = [[0 for _ in range(m)] for _ in range(m)]
for i in range(m):
a,b = map(int, input().split())
graph[a-1][b-1] = 1
graph[b-1][a-1] = 1
# n!通りの並べ方があり,各並べ方について成立するかどうかを調べる.
# 成立しないと判明した時点で探索をやめる
a = [i+1 for i in range(n)]
p = permutations(a)
def check(x):
# x: (1,3,4,6,7,2)
for i in range(n-1):
if not graph[x[i]-1][x[i+1]-1]:
return False
return True
cnt = 0
for x in p:
if x[0] != 1:
continue
if check(x):
cnt += 1
print(cnt)
return
if __name__ == '__main__':
main() |
s320526347 | p03805 | u593934357 | 1583274249 | Python | Python (3.4.3) | py | Runtime Error | 46 | 5072 | 1061 | import sys
from math import *
from collections import deque, Counter, defaultdict
from fractions import gcd
from itertools import permutations
input = lambda: sys.stdin.readline().rstrip()
def eprint(s):
sys.stderr.write('DEBUG: {}'.format(s))
return
def main():
n,m = map(int, input().split())
graph = [[0 for _ in range(m)] for _ in range(m)]
for i in range(m):
a,b = map(int, input().split())
graph[a-1][b-1] = 1
graph[b-1][a-1] = 1
# n!通りの並べ方があり,各並べ方について成立するかどうかを調べる.
# 成立しないと判明した時点で探索をやめる
a = [i+1 for i in range(n)]
p = permutations(a)
def check(x):
# x: (1,3,4,6,7,2)
for i in range(n-1):
if not graph[x[i]-1][x[i+1]-1]:
return False
return True
cnt = 0
for x in p:
if x[0] != 1:
continue
if check(x):
cnt += 1
print(cnt)
return
if __name__ == '__main__':
main() |
s614464833 | p03805 | u940139461 | 1583185120 | Python | PyPy3 (2.4.0) | py | Runtime Error | 348 | 62936 | 845 | n, m = map(int, input().split())
graph = [[] for _ in range(m + 1)]
for _ in range(m):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
def my_permutations(nums, t, path):
if len(nums) == 0:
path.append(t)
return
for i in range(len(nums)):
my_permutations(nums[:i] + nums[i + 1:], t + [nums[i]], path)
def dfs(graph, permu, n):
visited = [False] * (n + 1)
next_nodes = set([1])
for p in permu:
if visited[p]:
return False
if p not in next_nodes:
return False
visited[p] = True
next_nodes = set(graph[p])
return True
path = []
my_permutations([i for i in range(1, n + 1)], [], path)
ans = 0
for permu in path:
if permu[0] != 1:
continue
if dfs(graph, permu, n):
ans += 1
print(ans) |
s889864554 | p03805 | u139112865 | 1583052666 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 367 | #054_C
from itertools import permutations
n,m=map(int,input().split())
edges=[[False for _ in range(n)] for _ in range(n)]
for _ in range(m):
a,b=map(int,input().split())
a,b=a-1,b-1
edges[a][b]=edges[b][sa]=True
ans=0
for l in permutations(range(1,n),n-1):
l=[0]+list(l)
if all(edges[l[i]][l[i+1]] for i in range(n-1)):
ans+=1
print(ans) |
s503713212 | p03805 | u863442865 | 1583005630 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3060 | 884 | res = 0
def main():
import sys
#input = sys.stdin.readline
sys.setrecursionlimit(10000000)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations
#from itertools import accumulate, product, permutations
from math import floor, ceil
#mod = 1000000007
N,M = map(int, input().split())
g = [[] for _ in range(N)]
for _ in range(M):
a,b = map(int, input().split())
g[a-1].append(b-1)
g[b-1].append(a-1)
visited = [0]*N
def dfs(node):
global res
visited[node]=1
if all(visited):
res += 1
return
for n_node in g[node]:
if not visited[n_node]:
dfs(n_node)
visited[n_node] = 0
dfs(0)
print(res)
if __name__ == '__main__':
main() |
s729861026 | p03805 | u729535891 | 1582997719 | Python | Python (3.4.3) | py | Runtime Error | 2230 | 1724704 | 496 | from itertools import permutations
n, m = map(int, input().split())
LST = [[0]*2 for _ in range(m)]
for i in range(m):
a, b = map(int, input().split())
LST[i][0] = a - 1
LST[i][1] = b - 1
roots = list(permutations(range(m)))
ans = 0
for root in roots:
tmp = 0
if root[0] != 0:
continue
for i in range(n-1):
path = sorted([root[i], root[i+1]])
if path in LST:
tmp += 1
if tmp == n - 1:
ans += 1
print(ans if m != 0 else 0) |
s848180544 | p03805 | u729535891 | 1582996417 | Python | Python (3.4.3) | py | Runtime Error | 2215 | 1725472 | 478 | from itertools import permutations
n, m = map(int, input().split())
LST = [[0]*2 for _ in range(m)]
for i in range(m):
a, b = map(int, input().split())
LST[i][0] = a - 1
LST[i][1] = b - 1
roots = list(permutations(range(m)))
ans = 0
for root in roots:
tmp = 0
if root[0] != 0:
continue
for i in range(n-1):
path = sorted([root[i], root[i+1]])
if path in LST:
tmp += 1
if tmp == n - 1:
ans += 1
print(ans) |
s263186626 | p03805 | u995062424 | 1582987088 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 529 | N, M = map(int, input().split())
graph ={i+1:[] for i in range(N)}
for _ in range(N):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
def DFS(graph, start, end):
stack = [start]
visited = []
cnt = 0
while stack:
label = stack.pop(0)
if label == end:
visited.append(label)
cnt += 1
if label not in visited:
visited.append(label)
stack = graph.get(label, [])+stack
return cnt
print(DFS(graph, 1, N)) |
s471017884 | p03805 | u746419473 | 1582843600 | Python | Python (3.4.3) | py | Runtime Error | 33 | 3064 | 453 | import itertools
n, m = map(int, input().split())
am = [[False]*m for _ in range(n)]
for _ in range(m):
a, b = map(int, input().split())
am[a-1][b-1] = True
am[b-1][a-1] = True
ans = 0
for p in itertools.permutations(list(range(n)), n):
if p[0] != 0:
continue
isok = True
for i in range(len(p)-1):
if not am[p[i]][p[i+1]]:
isok = False
break
if isok:
ans += 1
print(ans)
|
s422066076 | p03805 | u577138895 | 1582775970 | Python | PyPy3 (2.4.0) | py | Runtime Error | 187 | 40048 | 348 | N, M = map(int, input().split())
V = [[] for _ in range(M)]
for _ in range(M):
a, b = map(int, input().split())
V[a - 1].append(b - 1)
V[b - 1].append(a - 1)
ctr = 0
def dfs(a, lst):
lst.append(a)
global ctr
if len(lst) == N:
ctr += 1
return 0
for v in V[a]:
if v in lst: continue
return dfs(v, lst)
dfs(0, [])
print(ctr) |
s148110614 | p03805 | u944643608 | 1582754475 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 525 | #深さ優先探索
N, M = map(int,input().split())
graph = [[] for j in range(N)]
for i in range(M):
a, b = map(int,input().split())
graph[a-1].append(b-1)
graph[b-1].append(a-1)
judge = [False]*N
def dfs(v,judge):#vは今いる場所
if all(judge):
return 1
res = 0
for nv in graph(v):#vと隣り合っている場所
if judge[nv] == True:#すでに探索済み
continue #if文の初めに戻る
judge[nv] = True
res += dfs(nv,judge)
judge[nv] = False
return res
print(dfs(0,judge)) |
s429755951 | p03805 | u693378622 | 1582539164 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 415 | N,M = gets.split.map(&:to_i)
g = Array.new(N){Array.new(N)}
M.times do |i|
a,b = gets.split.map(&:to_i).sort.reverse
a -= 1
b -= 1
g[a][b] = g[b][a] = 1
end
pattern = [*1...N].permutation.to_a.map{ |k| [0] + k }
ans = 0
pattern.each do |p|
f = true
(1...N).each do |i|
p [p[i-1], p[i]]
if g[p[i-1]][p[i]].nil?
f = false
break
end
end
ans += 1 if f
end
puts ans |
s763951923 | p03805 | u112317104 | 1582345503 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 485 | N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
visited = [False] * N
visited[0] = True
d = {}
for a, b in A:
d.setdefault(b-1, []).append(a-1)
d.setdefault(a-1, []).append(b-1)
l = sorted(d.items())
def dfs(v, seen):
if False not in seen: return 1
c = 0
for i in d[v]:
if (seen[i]): continue
seen[i] = True
c += dfs(i, seen)
seen[i] = False
return c
print(dfs(0, visited))
|
s882828688 | p03805 | u112317104 | 1582264681 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 595 | N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
visited = [False] * N
visited[0] = True
d = {}
for a, b in A:
d.setdefault(a, []).append(b)
d.setdefault(b, []).append(a)
l = sorted(d.items())
# print(d)
def dfs(v, c):
if visited[v-1]: return 0
visited[v-1] = True
if False not in visited:
c += 1
# print(visited, False not in visited, c)
# print('vertex', v)
for next_v in d[v]:
c += dfs(next_v, c)
return c
c = 0
ans = 0
for i in l[0][1]:
ans += dfs(i, c)
print(ans)
|
s218919649 | p03805 | u301624971 | 1582086294 | Python | Python (3.4.3) | py | Runtime Error | 40 | 3064 | 847 | def df_search(searchNode,node,sss,i):
global cnt
ss=sss+1
sn=searchNode
sn.append(i)
if(hantei==sorted(sn)):
if(ans==[]):
cnt+=1
ans.append(searchNode)
else:
for an in ans[:]:
if(an != searchNode):
cnt+=1
ans.append(searchNode)
for i in node:
if(i not in sn):
df_search(sn[:],df_dic[i],ss,i)
ans=[]
lis=[]
df_dic=dict()
NODES,EDGE=map(int,input().split())
df_flag = [0 for i in range(NODES)] # 探索状況[0:未探索, 1:探索済み] 初期値:0
for i in range(EDGE):
lis.append(list(map(int,input().split())))
df_dic[i+1]=[]
for i in lis:
df_dic[i[0]].append(i[1])
df_dic[i[1]].append(i[0])
cnt=0
hantei=[i for i in range(1,EDGE+1)]
df_search([],df_dic[1],0,1)
print(cnt) |
s757295658 | p03805 | u103902792 | 1581826199 | Python | Python (3.4.3) | py | Runtime Error | 20 | 3064 | 323 | n,m = map(int,input().split())
graph = {i+1:[] for i in range(n)}
for _ in range(m):
a,b = map(int,input(),split())
graph[a].append(b)
graph[b].append(a)
count = 0
def func(path,p):
if len(path) == n:
count += 1
return
for i in graph[p]:
if i in path:
continue
func(path+[i],i)
print(count) |
s918936181 | p03805 | u090266528 | 1581198463 | Python | PyPy3 (2.4.0) | py | Runtime Error | 196 | 40816 | 502 | N, M = list(map(int, input().split()))
ab = {i:[] for i in range(N)}
for _ in range(M):
a, b = list(map(int, input().split()))
ab[a-1].append(b-1)
ab[b-1].append(a-1)
cand = [[0]+[one] for one in ab[0]]
while len(cand)>0:
tmp_cand = []
for c in cand:
last = c[-1]
n = []
for nc in ab[last]:
if nc not in c:
n.append(c+[nc])
tmp_cand.extend(n)
cand = tmp_cand
if len(cand[0])==N:
break
print(len(cand)) |
s614458282 | p03805 | u686036872 | 1580604913 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 436 | N, M = map(int, input().split())
matrix=[[0]*N for i in range(N)]
for i in range(M):
a, b = map(int, input().split())
matrix[a-1][b-1]=1
matrix[b-1][a-1]=1
import itertools
count=0
for i in itertools.permulations(range(N)):
if i[0]==0:
for j in range(N-1):
result=1
result*=matrix[i[j]][i[j+1]]
if result==1:
count+=1
else:
break
print(count)
|
s966033419 | p03805 | u686036872 | 1580604852 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 430 | N, M = map(int, input().split())
matrix=[[0]*N for i in range(N)]
for i in range(M):
a, b = map(int, input().split())
matrix[a-1][b-1]=1
matrix[b-1][a-1]=1
import itertools
count=0
for i in itertools.permulations(range(N)):
if i[0]==0:
for j in range(N-1):
result=1
result*=matrix[i[j]][i[j+1]]
if result=1:
count+=1
else:
break
print(count) |
s593870205 | p03805 | u686036872 | 1580604763 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 458 | N, M = map(int, input().split())
matrix=[[0]*N for i in range(N)]
for i in range(M):
a, b = map(int, input().split())
matrix[a-1][b-1]=1
matrix[b-1][a-1]=1
import itertools
count=0
result=1
for i in itertools.permulations(range(N)):
if i==0:
for j in range(N-1):
result*=matrix[i[j]][i[j+1]]
if result=1:
count+=1
else:
result=1
else:
break
print(count) |
s678594627 | p03805 | u686036872 | 1580604579 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 463 | N, M = map(int, input().split())
matrix=[[0]*N for i in range(N)]
for i in range(M):
a, b = map(int, input().split())
matrix[a-1][b-1]=1
matrix[b-1][a-1]=1
import itertools
count=0
result=1
for i in itertools.permulations(range(1, N+1)):
if i==1:
for j in range(N-1):
result+=matrix[i[j]][i[j+1]]
if result=1:
count+=1
else:
result=1
else:
break
print(count) |
s105446178 | p03805 | u686036872 | 1580604521 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 436 | N, M = map(int, input().split())
matrix=[[0]*N for i in range(N)]
for i in range(M):
a, b = map(int, input().split())
matrix[a-1][b-1]=1
matrix[b-1][a-1]=1
import itertools
count=0
result=1
for i in itertools.permulations(range(1, N+1)):
if i==1:
for j in range():
result+=matrix[i[j]][i[j+1]]
if result=1:
count+=1
else:
result=1
print(count) |
s537490067 | p03805 | u740284863 | 1580593629 | Python | Python (3.4.3) | py | Runtime Error | 24 | 3064 | 481 | def dfs(v,been):
if len(been) == n:
ans.append(1)
#print(been)
return
for i in graph[v]:
if i in been:
pass
else:
been.add(i)
dfs(i,been)
been.remove(i)
return
n,m = map(int,input().split())
graph = [[] for _ in range(m+1)]
for _ in range(m):
a,b = map(int,input().split())
graph[a].append(b)
graph[b].append(a)
#print(graph)
ans = []
dfs(1,set([1]))
print(sum(ans)) |
s413331363 | p03805 | u624689667 | 1580335161 | Python | PyPy3 (2.4.0) | py | Runtime Error | 181 | 40048 | 464 | 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)
|
s136277475 | p03805 | u624689667 | 1580334973 | Python | PyPy3 (2.4.0) | py | Runtime Error | 195 | 39280 | 523 | 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)
|
s594411526 | p03805 | u853819426 | 1580326391 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 422 | def search(node, searched, goal):
if searched == goal:
return 1
v = 0
for nei in edges[node]:
if searched & (1 << nei):
continue
v += search(nei, searched | (1<<nei), goal)
return v
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for i in range(n):
a, b = map(lambda x: int(x) - 1, input().split())
edges[a].append(b)
edges[b].append(a)
print(search(0, 1, (1<<n) - 1))
|
s241158997 | p03805 | u191394596 | 1580303872 | Python | Python (3.4.3) | py | Runtime Error | 30 | 3572 | 510 | from itertools import permutations, tee
N, M = map(int, input().split())
edges = set()
for _ in range(N):
a, b = map(int, input().split())
edges.add((a, b))
def each_cons(iterable):
a, b = tee(iterable)
next(b, None)
return zip(a, b)
cases = list(permutations(range(2, N + 1), N - 1))
count = 0
def is_available(edge):
return edge in edges or edge[::-1] in edges
for numbers in cases:
if all(map(is_available, each_cons((1,) + numbers))):
count += 1
print(count) |
s155510632 | p03805 | u169138653 | 1580252971 | Python | Python (3.4.3) | py | Runtime Error | 45 | 8052 | 494 | from itertools import permutations
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
p=[i for i in range(1,n+1)]
np=list(permutations(p))
edge=[[] for i in range(m)]
for i in range(m):
edge[l[i][0]-1].append(l[i][1]-1)
edge[l[i][1]-1].append(l[i][0]-1)
ans=0
for i in range(len(np)):
if np[i][0]!=1:
continue
else:
ok=True
for j in range(n-1):
if np[i][j]-1 not in edge[np[i][j+1]-1]:
ok=False
if ok:
ans+=1
print(ans) |
s611443281 | p03805 | u692515710 | 1580151443 | Python | Python (3.4.3) | py | Runtime Error | 28 | 4080 | 794 | import sys
import copy
from queue import Queue
sys.setrecursionlimit(1000000000)
def dfs(start, paths, count):
if count <= 1:
return 1
indixies = findNodeIndex(start, paths)
if len(indixies) == 0:
return 0
count = 0
for i in indixies:
nextNode = paths[i][1] if paths[i][0] == start else paths[i][0]
newPaths = copy.deepcopy(paths)
newPaths.pop(i)
count += dfs(nextNode, newPaths, count - 1)
return count
def findNodeIndex(node, paths):
return [i for (i, row) in filter(lambda x: x[1][0] == node or x[1][1] == node, [(i, row) for i, row in enumerate(paths)])]
N, M = map(int, input().split())
PATHS = [list(map(int, input().split())) for _ in range(N)]
#start = findNodeIndex(1, PATHS)
print(dfs(1, PATHS, M)) |
s656932805 | p03805 | u798665594 | 1579679288 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 835 | #include <bits/stdc++.h>
using namespace std;
#define ll unsighned long int
int n,m;
int ans = 0;
void solve(vector<vector<int> > v,vector<int> dist,int pos = 1,int k = 1){
//cout << ans << endl;
if(k == n){
ans++;
return ;
}
for(int i = 0; i < v[pos - 1].size(); ++i){
if(dist[v[pos - 1][i] - 1] != 1){
dist[v[pos - 1][i] - 1] = 1;
solve(v,dist,v[pos - 1][i],k + 1);
dist[v[pos - 1][i] - 1] = -1;
}
}
}
int main(){
cin >> n >> m;
//cout << n << " " << m << endl;
vector<vector<int> > v(n,0);
vector<int> dist(n,-1);
dist[0] = 1;
for(int i = 0; i < m; ++i){
int a,b;
cin >> a >> b;
v[a - 1].push_back(b);
v[b - 1].push_back(a);
}
solve(v,dist);
cout << ans << endl;
} |
s617375590 | p03805 | u798665594 | 1579678749 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 833 | #include <bits/stdc++.h>
using namespace std;
#define ll unsighned long int
int n,m;
int ans = 0;
void solve(vector<vector<int> > v,vector<int> dist,int pos = 1,int k = 1){
//cout << ans << endl;
if(k == n){
ans++;
return ;
}
for(int i = 0; i < v[pos - 1].size(); ++i){
if(dist[v[pos - 1][i] - 1] != 1){
dist[v[pos - 1][i] - 1] = 1;
solve(v,dist,v[pos - 1][i],k + 1);
dist[v[pos - 1][i] - 1] = -1;
}
}
}
int main(){
cin >> n >> m;
//cout << n << " " << m << endl;
vector<vector<int> > v(n);
vector<int> dist(n,-1);
dist[0] = 1;
for(int i = 0; i < m; ++i){
int a,b;
cin >> a >> b;
v[a - 1].push_back(b);
v[b - 1].push_back(a);
}
solve(v,dist);
cout << ans << endl;
} |
s259099007 | p03805 | u798665594 | 1579678593 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 824 | #include <bits/stdc++.h>
using namespace std;
#define ll long int
int n,m;
int ans = 0;
void solve(vector<vector<int> > v,vector<int> dist,int pos = 1,int k = 1){
//cout << ans << endl;
if(k == n){
ans++;
return ;
}
for(int i = 0; i < v[pos - 1].size(); ++i){
if(dist[v[pos - 1][i] - 1] != 1){
dist[v[pos - 1][i] - 1] = 1;
solve(v,dist,v[pos - 1][i],k + 1);
dist[v[pos - 1][i] - 1] = -1;
}
}
}
int main(){
cin >> n >> m;
//cout << n << " " << m << endl;
vector<vector<int> > v(n);
vector<int> dist(n,-1);
dist[0] = 1;
for(int i = 0; i < m; ++i){
int a,b;
cin >> a >> b;
v[a - 1].push_back(b);
v[b - 1].push_back(a);
}
solve(v,dist);
cout << ans << endl;
} |
s430791065 | p03805 | u530786533 | 1579238236 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 550 | n, m = map(int, input().split())
edge = [[0] * n for _ in range(n)]
for i in range(n):
a, b = map(int, input().split())
edge[a-1][b-1] = 1
edge[b-1][a-1] = 1
# depth-first search
def dfs(now, visited):
if (False not in visited):
return(1)
ans = 0
for i in range(1, n+1):
if (edge[now-1][i-1] == 1 and visited[i-1] == False):
visited[i-1] = True
# recursive
ans += dfs(i, visited)
visited[i-1] = False
return(ans)
visited = [False] * n
visited[0] = True
print(dfs(1, visited))
|
s833968015 | p03805 | u530786533 | 1579238090 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 503 | n, m = map(int, input().split())
edge = [[0] * n for _ in range(n)]
for i in range(n):
a, b = map(int, input().split())
edge[a-1][b-1] = 1
edge[b-1][a-1] = 1
def dfs(now, visited):
if (False not in visited):
return(1)
ans = 0
for i in range(n):
if (edge[now-1][i-1] == 1 and visited[i-1] == False):
visited[i-1] = True
ans += dfs(i, visited)
visited[i-1] = False
return(ans)
visited = [False] * n
visited[0] = True
print(dfs(1, visited))
|
s919008900 | p03805 | u404290207 | 1579155258 | Python | PyPy3 (2.4.0) | py | Runtime Error | 206 | 42352 | 713 | import itertools
n,m=map(int,input().split())
if n ==2:
print(1)
exit()
arr=[[_] for _ in range(n)]
for i in range(n):
a,b=map(int,input().split())
arr[a-1].append(b-1)
arr[b-1].append(a-1)
for i in range(n):
arr[i] = arr[i][1:]
#print("arr",arr)
route=list(itertools.permutations(range(1,n)))
#print("route",route)
ans=0
for i in route:
chk0=False
chk=True
if i[0] in arr[0]:
#print(i)
for j in range(1,n-1):
if i[j] in arr[i[j-1]]:
chk0=True
#print(i[j],arr[j])
pass
else:
chk=False
if chk==True and chk0==True:
#print(i)
ans+=1
print(ans) |
s040627089 | p03805 | u404290207 | 1579154919 | Python | PyPy3 (2.4.0) | py | Runtime Error | 184 | 40816 | 671 | import itertools
n,m=map(int,input().split())
if n ==2:
print(1)
exit()
arr=[[_] for _ in range(n)]
for i in range(n):
a,b=map(int,input().split())
arr[a-1].append(b-1)
arr[b-1].append(a-1)
#print("arr",arr)
route=list(itertools.permutations(range(1,n)))
#print("route",route)
ans=0
for i in route:
chk0=False
chk=True
if i[0] in arr[0]:
#print(i)
for j in range(1,n-1):
if i[j] in arr[i[j-1]]:
chk0=True
#print(i[j],arr[j])
pass
else:
chk=False
if chk==True and chk0==True:
#print(i)
ans+=1
print(ans) |
s706323058 | p03805 | u735008991 | 1578618703 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 464 | def dfs(v):
for i in range(len(seen)):
if !seen[i] and i != v:
brea;
else:
res += 1
seen[i] = True
for nv in G[v]:
if seen[nv]:
continue
def(nv)
seen[i] = False
N, M = map(int, input().split())
G = dict([[i, [] for i in range(N)]])
for i in range(M):
a, b = map(int, input().split())
G[a-1].append(b-1)
G[b-1].append(a-1)
seen = [False]*N
res = 0
dfs(0)
print(res)
|
s490573728 | p03805 | u439396449 | 1578277603 | Python | Python (3.4.3) | py | Runtime Error | 25 | 3064 | 569 | import sys
sys.setrecursionlimit(10**6)
N, M = map(int, input().split())
graph = [set([]) for _ in range(M)]
for _ in range(M):
a, b = map(int, input().split())
graph[a - 1].add(b - 1)
graph[b - 1].add(a - 1)
def dfs(graph, visited, v):
if all(visited):
return 1
cnt = 0
for u in graph[v]:
if visited[u]:
continue
visited[u] = True
cnt += dfs(graph, visited, u)
visited[u] = False
return cnt
visited = [False] * len(graph)
visited[0] = True
ans = dfs(graph, visited, 0)
print(ans)
|
s341318308 | p03805 | u439396449 | 1578277058 | Python | Python (3.4.3) | py | Runtime Error | 25 | 3064 | 527 | N, M = map(int, input().split())
graph = [set([]) for _ in range(M)]
for _ in range(M):
a, b = map(int, input().split())
graph[a - 1].add(b - 1)
graph[b - 1].add(a - 1)
def dfs(graph, visited, v):
if all(visited):
return 1
cnt = 0
for u in graph[v]:
if visited[u]:
continue
visited[u] = True
cnt += dfs(graph, visited, u)
visited[u] = False
return cnt
visited = [False] * len(graph)
visited[0] = True
ans = dfs(graph, visited, 0)
print(ans)
|
s402515345 | p03805 | u286623856 | 1578269219 | Python | Python (3.4.3) | py | Runtime Error | 40 | 3188 | 1448 | import sys
import math
input = sys.stdin.readline
used = []
connect = []
N = 0
M = 0
def main():
global used
global connect
global N
global M
sc = Scan()
N, M = sc.intarr()
a = [0] * M
b = [0] * M
for i in range(M):
aa, bb = sc.intarr()
a[i] = aa - 1
b[i] = bb - 1
connect = [[0] * M for i in range(M)]
used = [0] * N
for i in range(M):
connect[a[i]][b[i]] = 1
connect[b[i]][a[i]] = 1
print(dfs(0, 1))
def dfs(now, depth):
global used
global connect
if used[now] == 1:
return 0
if depth == N:
return 1
ans = 0
used[now] = 1
for i in range(N):
if connect[now][i] == 1:
ans += dfs(i, depth+1)
used[now] = 0
return ans
class Scan():
def intarr(self):
num_array = list(map(int, input().split()))
return num_array
def intarr_ver(self, n):
return [int(input()) for _ in range(n)]
def strarr(self):
line = input()
array = line.split(' ')
array[-1] = array[-1].strip('\n')
return array
def display(array):
for a in range(len(array)):
if len(array) - a != 1:
print(array[a], end=' ')
else:
print(array[a])
def gcd(a, b): # 最大公約数
while b:
a, b = b, a % b
return a
def lcm(a, b): # 最小公倍数
return a * b // gcd(a, b)
main()
|
s709505216 | p03805 | u745561510 | 1578247675 | Python | PyPy3 (2.4.0) | py | Runtime Error | 201 | 42972 | 597 | import sys
sys.setrecursionlimit(10 ** 6)
N, M = map(int, input().split())
graph = [[] for i in range(M)]
for i in range(M):
v, e = map(int, input().split())
graph[v - 1].append(e - 1)
graph[e - 1].append(v - 1)
def dfs(v, seen, res):
end = True
for i in range(len(seen)):
if seen[i] == False and i != v:
end = False
if end:
res[0] += 1
return
seen[v] = True
for i in graph[v]:
if seen[i]:
continue
dfs(i, seen, res)
seen[v] = False
res = [0]
seen = [False] * N
dfs(0, seen, res)
print(res[0]) |
s202293242 | p03805 | u745561510 | 1578247549 | Python | PyPy3 (2.4.0) | py | Runtime Error | 233 | 44252 | 554 | N, M = map(int, input().split())
graph = [[] for i in range(M)]
for i in range(M):
v, e = map(int, input().split())
graph[v - 1].append(e - 1)
graph[e - 1].append(v - 1)
def dfs(v, seen, res):
end = True
for i in range(len(seen)):
if seen[i] == False and i != v:
end = False
if end:
res[0] += 1
return
seen[v] = True
for i in graph[v]:
if seen[i]:
continue
dfs(i, seen, res)
seen[v] = False
res = [0]
seen = [False] * N
dfs(0, seen, res)
print(res[0]) |
s666645392 | p03805 | u235376569 | 1578000359 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3060 | 195 | N,M=[int(x) for x in input().rstrip().split()]
dp=[0 for i in range(N)]
for i in range(N):
a,b=[int(x) for x in input().rstrip().split()]
dp[a-1]+=1
dp[b-1]+=1
dp.sort()
print(dp[0]) |
s498937695 | p03805 | u875600867 | 1577992584 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 1096 | import sys
# 再帰上限を引き上げる
sys.setrecursionlimit(10**6)
####
# DFS(深さ優先探索
count = 0
def dfs(graph, v, seen):
#print(seen)
# Falseがない、つまり頂点が1個しかない場合は、1を返す
if not False in seen:
global count
count +=1
return
# 頂点Vから行ける頂点を全探索
for i in graph[v]:
# 行ける場所はTrueである。
if seen[i]:
continue
# いったんTrueにして
seen[i] = True
# 探索したら
dfs(graph, i, seen)
# 戻す
seen[i] = False
return
####
M, N = map(int, input().split())
# グラフを作る
graph = [[] for i in range(N)]
#print(graph)
for i in range(M):
a, b = map(int, input().split())
# index番号にあわせたいので、1引く
a -= 1
b -= 1
graph[a].append(b)
graph[b].append(a)
seen = [False for i in range(N)]
seen[0] = True
#print(seen)
dfs(graph, 0, seen)
# 全探索できた数を表示
print(count) |
s794819189 | p03805 | u875600867 | 1577992480 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 1096 | import sys
# 再帰上限を引き上げる
sys.setrecursionlimit(10**6)
####
# DFS(深さ優先探索
count = 0
def dfs(graph, v, seen):
#print(seen)
# Falseがない、つまり頂点が1個しかない場合は、1を返す
if not False in seen:
global count
count +=1
return
# 頂点Vから行ける頂点を全探索
for i in graph[v]:
# 行ける場所はTrueである。
if seen[i]:
continue
# いったんTrueにして
seen[i] = True
# 探索したら
dfs(graph, i, seen)
# 戻す
seen[i] = False
return
####
M, N = map(int, input().split())
# グラフを作る
graph = [[] for i in range(N)]
#print(graph)
for i in range(M):
a, b = map(int, input().split())
# index番号にあわせたいので、1引く
a -= 1
b -= 1
graph[a].append(b)
graph[b].append(a)
seen = [False for i in range(M)]
seen[0] = True
#print(seen)
dfs(graph, 0, seen)
# 全探索できた数を表示
print(count) |
s995768923 | p03805 | u962127640 | 1577931139 | Python | Python (3.4.3) | py | Runtime Error | 24 | 3064 | 470 | N, M = list(map(int, input().split()))
g = [[] for _ in range(N)]
for i in range(N):
a, b = list(map(int, input().split()))
g[a-1].append(b-1)
g[b-1].append(a-1)
visited = [False for _ in range(N)]
def dfs(v, visited):
if all(visited):
return 1
total = 0
for cv in g[v]:
if not visited[cv]:
visited[cv] = True
total += dfs(v, visited)
visited[cv] = False
return total
visited[0] = True
ret = dfs(0, visited)
print(ret)
|
s027972400 | p03805 | u223646582 | 1577647154 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 500 | import sys
sys.setrecursionlimit(1000000)
N, M = map(int, input().split())
G = {k: [] for k in range(N)}
for _ in range(N):
a, b = map(int, input().split())
# 無向グラフ
G[a-1].append(b-1)
G[b-1].append(a-1)
visited = [False]*N
visited[0] = True
def dfs(v):
if all(visited):
return 1
r = 0
for nv in G[v]:
if visited[nv] is False:
visited[nv] = True
r += dfs(nv)
visited[nv] = False
return r
print(dfs(0))
|
s628863229 | p03805 | u223646582 | 1577647094 | Python | Python (3.4.3) | py | Runtime Error | 19 | 3316 | 502 | import sys
sys.setrecursionlimit(1000000)
N, M = map(int, input().split())
G = {k: [] for k in range(N+1)}
for _ in range(N):
a, b = map(int, input().split())
# 無向グラフ
G[a-1].append(b-1)
G[b-1].append(a-1)
visited = [False]*N
visited[0] = True
def dfs(v):
if all(visited):
return 1
r = 0
for nv in G[v]:
if visited[nv] is False:
visited[nv] = True
r += dfs(nv)
visited[nv] = False
return r
print(dfs(0))
|
s964418957 | p03805 | u223646582 | 1577647067 | Python | PyPy3 (2.4.0) | py | Runtime Error | 178 | 38384 | 502 | import sys
sys.setrecursionlimit(1000000)
N, M = map(int, input().split())
G = {k: [] for k in range(N+1)}
for _ in range(N):
a, b = map(int, input().split())
# 無向グラフ
G[a-1].append(b-1)
G[b-1].append(a-1)
visited = [False]*N
visited[0] = True
def dfs(v):
if all(visited):
return 1
r = 0
for nv in G[v]:
if visited[nv] is False:
visited[nv] = True
r += dfs(nv)
visited[nv] = False
return r
print(dfs(0))
|
s983664071 | p03805 | u223646582 | 1577647021 | Python | PyPy3 (2.4.0) | py | Runtime Error | 172 | 38640 | 468 | N, M = map(int, input().split())
G = {k: [] for k in range(N+1)}
for _ in range(N):
a, b = map(int, input().split())
# 無向グラフ
G[a-1].append(b-1)
G[b-1].append(a-1)
visited = [False]*N
visited[0] = True
ans = 0
def dfs(v):
if all(visited):
return 1
r = 0
for nv in G[v]:
if visited[nv] is False:
visited[nv] = True
r += dfs(nv)
visited[nv] = False
return r
print(dfs(0))
|
s084050858 | p03805 | u223646582 | 1577646565 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 470 | N, M = map(int, input().split())
G = {k: [] for k in range(N+1)}
for _ in range(N):
a, b = map(int, input().split())
# 無向グラフ
G[a-1].append(b-1)
G[b-1].append(a-1)
visited = [False]*N
visited[0] = True
def dfs(v):
if all(visited):
ans += 1
return
for nv in G[i]:
if visited[nv] is False:
visited[nv] = True
dfs(nv)
visited[nv] = False
return
ans = 0
dfs(0)
print(ans)
|
s116918970 | p03805 | u708255304 | 1577496866 | Python | Python (3.4.3) | py | Runtime Error | 25 | 3064 | 475 | import itertools
N, M = map(int, input().split())
graph = [[False]*N for _ in range(M)]
for _ in range(M):
a, b = map(lambda x: int(x)-1, input().split())
graph[a][b] = True
graph[b][a] = True
ans = 0
for x in itertools.permutations(range(1, N)):
# このxの順番で訪問できるか確かめる
flag = True
now = 0
for e in x:
if not graph[now][e]:
flag = False
now = e
if flag:
ans += 1
print(ans)
|
s009329329 | p03805 | u624689667 | 1577134864 | Python | PyPy3 (2.4.0) | py | Runtime Error | 203 | 42716 | 897 | 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()
|
s285920431 | p03805 | u624689667 | 1577134749 | Python | PyPy3 (2.4.0) | py | Runtime Error | 206 | 42844 | 849 | 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()
|
s147363631 | p03805 | u624689667 | 1577134672 | Python | PyPy3 (2.4.0) | py | Runtime Error | 204 | 42716 | 805 | 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()
|
s046567716 | p03805 | u181215519 | 1577063731 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 690 | N, M = map( int, input().split() )
ab = [ [ int(x) for x in input().split() ] for y in range(M) ]
e = [ [ 0 for x in range(N) ] for y in range(N) ]
flag = [ False for x in range(N) ]
dp = [ 0 for x in range(N) ] #number of path from 1 to i
for i in range(M) :
e[ ab[i][0]-1 ][ ab[i][1]-1 ] = 1
e[ ab[i][1]-1 ][ ab[i][0]-1 ] = 1
print( e )
ct = 0
for i in range(N) :
for j in range(N) :
if i == 0 and e[i][j]==1:
dp[j] = 1
flag[j]=True
elif e[j][i]==1 :
dp[i] += dp[j]
flag[j]=True
if !False in flag :
ct += 1
print( dp[N-1] )
#does not count the path which goes through ALL nodes |
s342876081 | p03805 | u357949405 | 1576908774 | Python | Python (3.4.3) | py | Runtime Error | 20 | 3188 | 847 | import itertools
def main():
N, M = map(int, input().split())
VE = tuple(tuple(map(lambda x: int(x)-1, input().split())) for _ in range(M))
# print(VE)
adj_mat = [[0 for _ in range(N)] for _ in range(M)]
for ve in VE:
adj_mat[ve[0]][ve[1]] = 1
adj_mat[ve[1]][ve[0]] = 1
# for row in adj_mat:
# print(row)
ans = 0
for perm in itertools.permutations([i for i in range(1, N)]):
# print(perm)
flag = True
now_V = 0
for next_V in perm:
# print('{} to {}'.format(now_V, next_V))
# print('{}'.format(adj_mat[now_V][next_V]))
if not adj_mat[now_V][next_V]:
flag = False
break
now_V = next_V
if flag:
ans += 1
print(ans)
if __name__ == '__main__':
main()
|
s592043865 | p03805 | u694810977 | 1576874188 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 558 | n, m = map(int, input().split())
q = []
for i in range(m):
a, b = map(int, input().split())
q.append({a, b})
cnt = 0
array = list(range(1, m+1))
while cnt != 10000:
a = q.pop()
for j in q:
if len(j & a) != 0: # jとaに共通の値があるならば
j |= a
count = 0
break
else: # jとaに共通の値がなければ
q = [a] + q
if count > len(q):
break
count += 1
for i in q:
tmp = list(i)
if tmp == array:
cnt += 1
print(cnt)
|
s367121804 | p03805 | u694810977 | 1576874157 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 558 | n, m = map(int, input().split())
q = []
for i in range(m):
a, b = map(int, input().split())
q.append({a, b})
cnt = 0
array = list(range(1, n+1))
while cnt != 10000:
a = q.pop()
for j in q:
if len(j & a) != 0: # jとaに共通の値があるならば
j |= a
count = 0
break
else: # jとaに共通の値がなければ
q = [a] + q
if count > len(q):
break
count += 1
for i in q:
tmp = list(i)
if tmp == array:
cnt += 1
print(cnt)
|
s923578659 | p03805 | u694810977 | 1576874084 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 530 | n, m = map(int, input().split())
q = []
for i in range(m):
a, b = map(int, input().split())
q.append({a, b})
cnt = 0
while cnt != 10000:
a = q.pop()
for j in q:
if len(j & a) != 0: # jとaに共通の値があるならば
j |= a
count = 0
break
else: # jとaに共通の値がなければ
q = [a] + q
if count > len(q):
break
count += 1
for i in q:
tmp = list(i)
if tmp == array:
cnt += 1
print(cnt)
|
s682923046 | p03805 | u694810977 | 1576874020 | Python | Python (3.4.3) | py | Runtime Error | 26 | 8052 | 617 | import itertools
n, m = map(int, input().split())
array = list(range(1, n + 1))
p = list(itertools.permutations(array))
q = []
for i in range(m):
a, b = map(int, input().split())
q.append({a, b})
cnt = 0
while cnt != 10000:
a = q.pop()
for j in q:
if len(j & a) != 0: # jとaに共通の値があるならば
j |= a
count = 0
break
else: # jとaに共通の値がなければ
q = [a] + q
if count > len(q):
break
count += 1
for i in q:
tmp = list(i)
if tmp == array:
cnt += 1
print(cnt)
|
s649118569 | p03805 | u706740637 | 1576745620 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 720 | import sys
import itertools
import math
input = sys.stdin.readline
#sys.setrecursionlimit(1e9)
N,M = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(M)]
graph=[[0]*8 for _ in range(8)]
def dfs(v,visited):
if sum(visited) == N:
return 1
ret =0
for i in range(N):
if graph[v][i] == 1 and visited[i] == 0:
visited[i]=1
ret += dfs(i,visited)
visited[i]=0
return ret
def main():
"""code here"""
for i in range(N):
a=ab[i][0]
b=ab[i][1]
graph[a-1][b-1]=1
graph[b-1][a-1]=1
visited=[0]*N
visited[0]=1
print(dfs(0,visited))
if __name__ == '__main__':
main() |
s028134862 | p03805 | u706740637 | 1576745371 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 720 | import sys
import itertools
import math
input = sys.stdin.readline
#sys.setrecursionlimit(1e9)
N,M = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(M)]
graph=[[0]*N for _ in range(N)]
def dfs(v,visited):
if sum(visited) == N:
return 1
ret =0
for i in range(N):
if graph[v][i] == 1 and visited[i] == 0:
visited[i]=1
ret += dfs(i,visited)
visited[i]=0
return ret
def main():
"""code here"""
for i in range(N):
a=ab[i][0]
b=ab[i][1]
graph[a-1][b-1]=1
graph[b-1][a-1]=1
visited=[0]*N
visited[0]=1
print(dfs(0,visited))
if __name__ == '__main__':
main() |
s586032548 | p03805 | u905582793 | 1576632842 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 359 | from itertools import permutations
n,m=map(int,input().split())
side=[[] for _ in range(n+1)]
for i in range(m):
a,b=map(int,input().split())
side[a].append(b)
side[b].append(a)
route=[i for i in range(2,n+1)]
ans=0
for x in permutations(route):
x=(1)+x
for i in range(n-1):
if x[i+1] not in side[x[i]]:
break
else:
ans+=1
print(ans) |
s088753415 | p03805 | u560988566 | 1576438174 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 730 | n,m = map(int, input().split())
ls = [tuple(map(int, input().split())) for _ in range(m)]
neighborls = []
for i in range(1,n+1):
neighbors = []
for j in range(1, m+1):
if (i,j) in ls or (j,i) in ls:
neighbors.append(j)
neighborls.append(neighbors)
ans = 0
def main():
global ans
visitedls = [1]
ans = dfs(1, visitedls, ans)
print(ans)
def dfs(a, visitedls, ans):
global ans
if len(visitedls) == n:
ans += 1
for i in range(1,n+1):
if i not in visitedls and i in neighborls[a-1]:
visitedls.append(i)
ans = dfs(i, visitedls, ans)
visitedls.remove(i)
return ans
if __name__ == "__main__":
main() |
s959764831 | p03805 | u560988566 | 1576438006 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 719 | n,m = map(int, input().split())
ls = [tuple(map(int, input().split())) for _ in range(m)]
neighborls = []
for i in range(1,n+1):
neighbors = []
for j in range(1, m+1):
if (i,j) in ls or (j,i) in ls:
neighbors.append(j)
neighborls.append(neighbors)
ans = 0
def main():
visitedls = [1]
ans = dfs(1, visitedls, ans)
print(ans)
def dfs(a, visitedls, ans):
if len(visitedls) == n:
ans += 1
return ans
for i in range(1,n+1):
if i not in visitedls and i in neighborls[a-1]:
visitedls.append(i)
ans = dfs(i, visitedls, ans)
visitedls.remove(i)
return ans
if __name__ == "__main__":
main() |
s494616119 | p03805 | u019637926 | 1575573093 | Python | PyPy3 (2.4.0) | py | Runtime Error | 182 | 38384 | 522 | N, M = map(int, input().split())
Bs = [[] for _ in range(N + 1)]
for _ in range(M):
a, b = map(int, input().split())
Bs[a].append(b)
Bs[b].append(a)
import heapq
st = []
heapq.heappush(st, (1, {1: True}))
c = 0
while len(st) > 0:
(node, log) = heapq.heappop(st)
for b in Bs[node]:
if b in log:
continue
elif len(log) == N - 1:
c += 1
else:
newlog = log.copy()
newlog[b] = True
heapq.heappush(st, (b, newlog))
print(c) |
s999598357 | p03805 | u633548583 | 1575145091 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 342 | import itertools
n,m=map(int,input().split())
p=[[False]*n for i in range(n)]
for i in range(m):
a,b=map(int,input().split())
a-=1
b-=1
p[a][b]=True
p[b][a]=True
ans=0
for i in itertools.permutations(range(1,n),n-1):
list=[0]+[i]
if all(p[list[j]][list[j+1]]==True for j in range(n)):
ans+=1
print(ans) |
s147690204 | p03805 | u633548583 | 1575144718 | Python | Python (3.4.3) | py | Runtime Error | 26 | 3064 | 413 | import itertools
n,m=map(int,input().split())
p=[[False]*n for i in range(n)]
ans=0
for i in range(n):
a,b=map(int,input().split())
a-=1
b-=1
p[a][b]=True
p[b][a]=True
for i in itertools.permutations(range(n),n):
if i[0]==0:
for j in range(n):
if j==n-1:
ans+=1
break
if not p[i[j]][i[j+1]]:
break
print(ans) |
s883530745 | p03805 | u633548583 | 1575144580 | Python | Python (3.4.3) | py | Runtime Error | 26 | 3064 | 413 | import itertools
n,m=map(int,input().split())
p=[[False]*n for i in range(n)]
ans=0
for i in range(n):
a,b=map(int,input().split())
a-=1
b-=1
p[a][b]=True
p[b][a]=True
for i in itertools.permutations(range(n),n):
if i[0]==0:
for j in range(n):
if j==n-1:
ans+=1
break
if not p[i[j]][i[j+1]]:
break
print(ans) |
s029238116 | p03805 | u379959788 | 1574996524 | Python | Python (3.4.3) | py | Runtime Error | 22 | 3064 | 760 | # ABC054C
# 方針:階乗の全探索
import itertools
N, M = map(int, input().split())
# Graph の作成
graph = [[] * N for _ in range(N)]
for i in range(M):
u, v = map(int, input().split())
u -= 1
v -= 1
graph[u].append(v)
graph[v].append(u)
# パターンの全列挙
lst = [_ for _ in range(0, N)]
search_list = itertools.permutations(lst)
ans = 0
for lst in search_list:
flag = True
if lst[0] != 0: # 1度でも先頭が0以外ならそれ以降は全てダメ
break
for i in range(M-1):
if lst[i+1] in graph[lst[i]]: # 今の数字が次の数字と繋がっているかどうか
pass
else:
flag = False
break
if flag:
ans += 1
print(ans) |
s297488662 | p03805 | u698902360 | 1574806070 | Python | PyPy3 (2.4.0) | py | Runtime Error | 187 | 45424 | 452 | import itertools
N, M = map(int, input().split())
graph = [[0] * N for i in range(N)]
for i in range(N):
a, b = map(int, input().split())
graph[a-1][b-1] = 1
graph[b-1][a-1] = 1
p = list(itertools.permutations(range(N)))
Sum = 0
for i in range(len(p)):
if p[i][0] != 0:
break
for j in range(N - 1):
if graph[p[i][j]][p[i][j + 1]] != 1:
break
if j == N - 2:
Sum += 1
print(Sum)
|
s822907973 | p03805 | u814986259 | 1574726776 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 428 | from itertools import permutation
N,M=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(M)]
g=[[False]*N for i in range(N)]
for a,b in ab:
g[a-1][b-1]=True
g[b-1][a-1]=True
nodes=[i for i in range(N)]
ans=0
for route in permutation(nodes):
for i in range(N):
if i == N-1:
ans+=1
break
if g[route[i]][route[i+1]]:
continue
else:
break
print(ans)
|
s500444273 | p03805 | u936985471 | 1574522354 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 577 | n,m=map(int,input().split())
nexts=[None for i in range(n)]
for i in range(n):
a,b=map(int,input().split())
a,b=a-1,b-1
if nexts[a]==None:
nexts[a]=[b]
elif b not in nexts[a]:
nexts[a].append(b)
if nexts[b]==None:
nexts[b]=[a]
elif a not in nexts[b]:
nexts[b].append(a)
ans=0
stack=[]
stack.append([0,[]])
while stack:
node=stack.pop()
v=node[0]
seen=node[1][:]
seen.append(v)
if len(seen)==n:
ans+=1
continue
childs=nexts[v]
if childs:
for child in (set(childs)-set(seen)):
stack.append([child,seen])
print(ans)
|
s719277604 | p03805 | u936985471 | 1574522297 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3188 | 541 | n,m=map(int,input().split())
nexts=[None for i in range(n)]
for i in range(n):
a,b=map(int,input().split())
a,b=a-1,b-1
if nexts[a]==None:
nexts[a]=[b]
else:
nexts[a].append(b)
if nexts[b]==None:
nexts[b]=[a]
else:
nexts[b].append(a)
ans=0
stack=[]
stack.append([0,[]])
while stack:
node=stack.pop()
v=node[0]
seen=node[1][:]
seen.append(v)
if len(seen)==n:
ans+=1
continue
childs=nexts[v]
if childs:
for child in (set(childs)-set(seen)):
stack.append([child,seen])
print(ans)
|
s717783700 | p03805 | u936985471 | 1574522170 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 538 | n,m=map(int,input().split())
nexts=[None for i in range(n)]
for i in range(n):
a,b=map(int,input().split())
a,b=a-1,b-1
if nexts[a]==None:
nexts[a]=[b]
else:
nexts[a].append(b)
if nexts[b]==None:
nexts[b]=[a]
else:
nexts[b].append(a)
ans=0
stack=[]
stack.append([0,set()])
while stack:
node=stack.pop()
v=node[0]
seen=set(node[1])
seen.add(v)
if len(seen)==n:
ans+=1
continue
childs=nexts[v]
if childs:
for child in (set(childs)-seen):
stack.append([child,seen])
print(ans)
|
s191965530 | p03805 | u936985471 | 1574521127 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 538 | n,m=map(int,input().split())
nexts=[None for i in range(n)]
for i in range(n):
a,b=map(int,input().split())
a,b=a-1,b-1
if nexts[a]==None:
nexts[a]=[b]
else:
nexts[a].append(b)
if nexts[b]==None:
nexts[b]=[a]
else:
nexts[b].append(a)
ans=0
stack=[]
stack.append([0,[]])
while stack:
node=stack.pop()
v=node[0]
seen=node[1]
seen.append(v)
if len(seen)==n:
ans+=1
continue
childs=nexts[v]
if childs:
for child in (set(childs)-set(seen)):
stack.append([child,seen])
print(ans)
|
s351491827 | p03805 | u936985471 | 1574521072 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 533 | n,m=map(int,input().split())
nexts=[None for i in range(n)]
for i in range(n):
a,b=map(int,input().split())
a,b=a-1,b-1
if nexts[a]==None:
nexts[a]=[b]
else:
nexts[a].append(b)
if nexts[b]==None:
nexts[b]=[a]
else:
nexts[b].append(a)
ans=0
stack=[]
stack.append([0,[]])
while stack:
node=stack.pop()
v=node[0]
seen=node[1]
seen.append(v)
if len(seen)==n:
ans+=1
continue
childs=nexts[v]
if childs:
for child in (set(childs)-seen):
stack.append([child,seen])
print(ans)
|
s045993711 | p03805 | u936985471 | 1574520030 | Python | Python (3.4.3) | py | Runtime Error | 19 | 3188 | 461 | n,m=map(int,input().split())
nexts=[None for i in range(n)]
for i in range(n):
a,b=map(int,input().split())
a,b=a-1,b-1
if nexts[a]==None:
nexts[a]=[b]
else:
nexts[a].append(b)
ans=0
stack=[]
stack.append([0,set()])
while stack:
node=stack.pop()
v=node[0]
seen=node[1]
seen.add(v)
if len(seen)==n:
ans+=1
continue
childs=nexts[v]
if childs:
for child in (set(childs)-seen):
stack.append(child,seen)
print(ans) |
s610820984 | p03805 | u322229918 | 1574471018 | Python | Python (3.4.3) | py | Runtime Error | 312 | 21800 | 495 | from itertools import permutations
import numpy as np
N, M = map(int, input().split(" "))#N頂点M辺
mat = [[0] * n for _ in range(n)]
for _ in range(M):
v1, v2 = map(int, input().split(" "))
mat[v1 - 1][v2 - 1] = mat[v2 - 1][v1 - 1] = 1
count = 0
for line in permutations(np.arange(N - 1) + 2):
vtxs = [1] + list(line)
for i in range(N - 1):
v1, v2 = vtxs[i], vtxs[i + 1]
if mat[v1 - 1][v2 - 1] == 0:
break
else:
count += 1
print(count) |
s987955988 | p03805 | u047178225 | 1574003200 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3060 | 615 | nmax = 8
graph = [[0]*nmax for i in range(nmax)]
visited = [0] * nmax
def dfs(v, N, visitied):
all_visited = True
for i in range(N):
if visitied[i] == 0:
all_visited = False
if all_visited:
return 1
ret = 0
for i in range(N):
if graph[v][i] == 0:
continue
if visited[i] == 0:
continue
visited[i] = 1
ret += dfs(i, N, visited)
visited[i] = False
return ret
N, M = [int(i) for i input().split()]
for j in range(M):
a, b = [int(j) for j in input().split()]
graph[a-1][b-1] = graph[b-1][a-1] = 1
visited[0] = 1
print(dfs(0, N, visited))
|
s265274673 | p03805 | u557494880 | 1573891923 | Python | Python (3.4.3) | py | Runtime Error | 19 | 3064 | 402 | N,M = map(int,input().split())
G = [[] for i in range(N+1)]
for i in range(M):
a,b = map(int,input().split())
G[a].append(b)
G[b].append(a)
l = [i for i in range(2,N+1)]
import itertools
ans = 0
for v in itertools.permutations(l):
s = 1
t = 1
while v:
s = t
t = v.pop(0)
if t not in G[s]:
break
if len(v) == 0:
ans += 1
print(ans) |
s589965427 | p03805 | u183896397 | 1573795661 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 587 | N,M = (int(i) for i in input().split())
K = []
for i in range(M):
J = [int(i) for i in input().split()]
J.sort()
K.append(J)
#1以外の頂点リストを作成
vertex = []
for i in range(N-1):
vertex.append(i+2)
#順列作成
X = list(itertools.permutations(vertex))
#各値ごとに有無をチェック
ans = 0
for i in X:
start = 1
localcount = 0
for j in i:
tmp = [start,j]
tmp.sort()
if tmp in K:
localcount += 1
start = j
if localcount == N-1:
ans += 1
print(ans) |
s453090378 | p03805 | u183896397 | 1573794476 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 627 | N,M = (int(i) for i in input().split())
K = []
for i in range(M):
J = [int(i) for i in input().split()]
J.sort()
K.append(J)
#1以外の頂点リストを作成
vertex = []
for i in range(N-1):
vertex.append(i+2)
#順列作成
X = list(itertools.permutations(vertex))
#各値ごとに有無をチェック
ans = 0
for i in X:
start = 1
localcount = 0
for j in i:
tmp = [start,j]
tmp.sort()
if tmp in K == False:
break
else:
localcount += 1
start = j
if localcount == N-1:
ans += 1
print(ans) |
s382530249 | p03805 | u457423258 | 1573756886 | Python | Python (3.4.3) | py | Runtime Error | 35 | 8052 | 463 | import itertools
N,M=map(int,input().split())
if M==0:
print(0)
exit()
S=[list(map(int,input().split()))for _ in range(N) ]
a=[]
for i in range(N):
a.append(i+1)
L=list(itertools.permutations(a))
cnt=0
for l in L:
tmp=1
if l[0]!=1:
break
for j in range(N-1):
if ([l[j],l[j+1]] in S) or ([l[j+1],l[j]] in S):
tmp=1
else:
tmp=0
break
if tmp==1:
cnt+=1
print(cnt) |
s153051124 | p03805 | u457423258 | 1573756738 | Python | Python (3.4.3) | py | Runtime Error | 35 | 8052 | 430 | import itertools
N,M=map(int,input().split())
S=[list(map(int,input().split()))for _ in range(N) ]
a=[]
for i in range(N):
a.append(i+1)
L=list(itertools.permutations(a))
cnt=0
for l in L:
tmp=1
if l[0]!=1:
break
for j in range(N-1):
if ([l[j],l[j+1]] in S) or ([l[j+1],l[j]] in S):
tmp=1
else:
tmp=0
break
if tmp==1:
cnt+=1
print(cnt) |
s605551234 | p03805 | u910756197 | 1573592487 | Python | Python (3.4.3) | py | Runtime Error | 29 | 3064 | 370 | N, M = map(int, input().split())
g = [[] for _ in range(M + 1)]
for _ in range(M):
a, b = map(int, input().split())
g[a].append(b)
g[b].append(a)
ans = 0
def walk(cur, visited):
global ans
tmp = visited + [cur]
if len(tmp) == N:
ans += 1
for nex in g[cur]:
if nex not in tmp:
walk(nex, tmp)
walk(1, [])
print(ans) |
s994663022 | p03805 | u910756197 | 1573592423 | Python | Python (3.4.3) | py | Runtime Error | 29 | 3064 | 374 | N, M = map(int, input().split())
g = [list() for _ in range(M + 1)]
for _ in range(M):
a, b = map(int, input().split())
g[a].append(b)
g[b].append(a)
ans = 0
def walk(cur, visited):
global ans
tmp = visited + [cur]
if len(tmp) == N:
ans += 1
for nex in g[cur]:
if nex not in tmp:
walk(nex, tmp)
walk(1, [])
print(ans) |
s368847787 | p03805 | u910756197 | 1573592284 | Python | Python (3.4.3) | py | Runtime Error | 32 | 3064 | 395 | N, M = map(int, input().split())
g = [list() for _ in range(M + 1)]
for _ in range(M):
a, b = map(int, input().split())
g[a].append(b)
g[b].append(a)
ans = 0
def walk(cur, visited):
global ans, g, N, M
tmp = visited + [cur]
if len(tmp) == N:
ans += 1
for nex in g[cur]:
if nex not in tmp:
walk(nex, visited + [cur])
walk(1, [])
print(ans) |
s636889416 | p03805 | u910756197 | 1573592132 | Python | Python (3.4.3) | py | Runtime Error | 31 | 3064 | 386 | N, M = map(int, input().split())
g = [list() for _ in range(M + 1)]
for _ in range(M):
a, b = map(int, input().split())
g[a].append(b)
g[b].append(a)
ans = 0
def walk(cur, visited):
global ans
tmp = visited + [cur]
if len(tmp) == N:
ans += 1
for nex in g[cur]:
if nex not in tmp:
walk(nex, visited + [cur])
walk(1, [])
print(ans) |
s208279989 | p03805 | u910756197 | 1573591894 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 390 | N, M = map(int, input().split())
g = [list() for _ in range(M + 1)]
for _ in range(M):
a, b = map(int, input().split())
g[a].append(b)
g[b].append(a)
ans = 0
def walk(cur, visited):
global ans
if cur == N:
ans += 1
return
visited.append(cur)
for nex in g[cur]:
if nex not in visited:
walk(nex, visited)
walk(1, [])
print(ans) |
s106762227 | p03805 | u030749892 | 1572484932 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 465 | N,M = map(int,input().split())
x = [input().split() for i in range(M)]
y = [[] for _ in range(M)]
z = [0 for _ in range(M)]
for i in x:
y[int(i[0])-1].append(int(i[1])-1)
y[int(i[1])-1].append(int(i[0])-1)
tmp = y[0]
ans = 0
cnt = 0
while True:
if cnt != N - 1:
tmp2 = []
for l in tmp:
if z[l] == 0:
z[l] += 1
for n in y[l]:
tmp2.append(n)
tmp = tmp2
cnt += 1
else:
ans = len(tmp)
break
print(ans) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.