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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
s173126175 | p03805 | u902468164 | 1556214531 | Python | Python (3.4.3) | py | Runtime Error | 36 | 3064 | 724 | def dfs(v, N, visited, graph):
all_visited = True
for i in range(N):
if visited[i] is False :
all_visited = False
if all_visited:
return 1
ret = 0
for i in range(N):
if graph[v][i] is False:
continue
if visited[i]:
continue
visited[i] = True
ret += dfs(i, N, visited, graph)
visited[i] = False
return ret
N, M = [int(i) for i in input().split(" ")]
graph = [[False for i in range(M)] for i in range(M)]
for i in range(M):
A, B = [int(i) for i in input().split()]
graph[A-1][B-1] = graph[B-1][A-1] = True
visited = [False for i in range(N)]
visited[0] = True
print(dfs(0, N, visited, graph)) |
s898659779 | p03805 | u573970531 | 1556207137 | Python | Python (3.4.3) | py | Runtime Error | 48 | 3064 | 403 | import itertools
n,m=list(map(int,input().split()))
path=[[False]*m for _ in range(m)]
for i in range(m):
a,b=list(map(int,input().split()))
path[a-1][b-1]=True
ans=0
for i in itertools.permutations(list(range(n))):
if i[0]!=0:
continue
flg=True
for j in range(len(i)-1):
if path[min(i[j],i[j+1])][max(i[j],i[j+1])]==False:
flg=False
break
if flg:
ans+=1
print(ans) |
s487868978 | p03805 | u186838327 | 1555232343 | Python | Python (3.4.3) | py | Runtime Error | 33 | 3064 | 409 | n, m = map(int, input().split())
l = [[] for _ in range(m)]
for i in range(m):
a, b = map(int, input().split())
l[a-1].append(b-1)
l[b-1].append(a-1)
import itertools
ans = 0
for p in itertools.permutations(range(n)):
if p[0] == 0:
flag = True
for i in range(n-1):
if not p[i+1] in l[p[i]]:
flag = False
if flag:
ans += 1
print(ans)
|
s917491591 | p03805 | u186838327 | 1555232255 | Python | Python (3.4.3) | py | Runtime Error | 94 | 3064 | 434 | n, m = map(int, input().split())
l = [[] for _ in range(m)]
for i in range(m):
a, b = map(int, input().split())
l[a-1].append(b-1)
l[b-1].append(a-1)
import itertools
ans = 0
for p in itertools.permutations(range(n)):
if p[0] == 0:
flag = 1
else:
flag = 0
for i in range(n-1):
if not p[i+1] in l[p[i]]:
flag = 0
if flag == 1:
ans += 1
print(ans)
|
s582448031 | p03805 | u186838327 | 1555232108 | Python | Python (3.4.3) | py | Runtime Error | 32 | 3064 | 406 | n, m = map(int, input().split())
l = [[] for _ in range(m)]
for i in range(m):
a, b = map(int, input().split())
l[a-1].append(b-1)
l[b-1].append(a-1)
import itertools
ans = 0
for p in itertools.permutations(range(n)):
if p[0] == 0:
flag = 1
for i in range(n-1):
if not p[i+1] in l[p[i]]:
flag = 0
if flag == 1:
ans += 1
print(ans) |
s046744504 | p03805 | u186838327 | 1555231988 | Python | Python (3.4.3) | py | Runtime Error | 37 | 3188 | 433 | n, m = map(int, input().split())
l = [[] for _ in range(m)]
for i in range(m):
a, b = map(int, input().split())
l[a-1].append(b-1)
l[b-1].append(a-1)
import itertools
ans = 0
for p in itertools.permutations(range(n)):
if p[0] == 0:
flag = 1
for i in range(n-1):
if p[i+1] in l[p[i]]:
pass
else:
flag = 0
if flag == 1:
ans += 1
print(ans) |
s479640175 | p03805 | u603745966 | 1555127199 | Python | Python (3.4.3) | py | Runtime Error | 37 | 4324 | 1710 | from functools import reduce
import math
import itertools
def main():
# N! を求める
# f = math.factorial(N)
# 切り捨て
# 4 // 3
# 切り上げ
#-(-4 // 3)
# 初期値用:十分大きい数(100億)
# 1e10
# 初期値用:十分小さい数(-100億)
# -1e10
# 1文字のみを読み込み
# 入力:2
# a = input().rstrip()
# 変数:a='2'
# スペース区切りで標準入力を配列として読み込み
# 入力:2 4 5 7
# a, b, c, d = (int(_) for _ in input().split())
# 変数:a=2 b=4 c=5 d =7
# 1文字ずつ標準入力を配列として読み込み
# 入力:2 4 5 7
# a = list(int(_) for _ in input().split())
# 変数:a = [2, 4, 5, 7]
# 1文字ずつ標準入力を配列として読み込み
# 入力:2457
# a = list(int(_) for _ in input())
# 変数:a = [2, 4, 5, 7]
N,M = (int(_) for _ in input().split())
a = []
b = []
for i in range(N):
c, d = (int(_) for _ in input().split())
a.append(c)
b.append(d)
l = list(_ for _ in range(2,N+1))
d = []
for elem in itertools.permutations(l,len(l)):
elem = list(elem)
elem.insert(0,1)
d.append(elem)
ans = 0
for l1 in d:
flag = True
for i in range(len(l1)-1):
for j in range(M):
if(a[j]==l1[i] and b[j] ==l1[i+1]) or (b[j]==l1[i] and a[j] ==l1[i+1]):
flag = True
break
flag = False
if flag == False:
break
if flag:
ans += 1
print(ans)
if __name__ == '__main__':
main()
|
s896649735 | p03805 | u328207927 | 1555002683 | Python | Python (3.4.3) | py | Runtime Error | 31 | 3064 | 631 | import sys
sys.setrecursionlimit(100000)
ans=0
def dfs(v,n,visi):
allv=True
for i in visi:
if not i:
allv=False
if allv:
return 1
ret=0
for i in range(n):
if G[v][i]==False:
continue
if visi[i]:
continue
visi[i]=True
ret+=dfs(i,n,visi)
visi[i]=False
return ret
n,m=map(int,input().split())
ab=[list(map(int,input().split()))for i in range(m)]
G=[[False for i in range(n)]for i in range(m)]
for a,b in ab:
G[a-1][b-1]=G[b-1][a-1]=True
visi=[False for i in range(n)]
visi[0]=True
print(dfs(0,n,visi)) |
s199091251 | p03805 | u594567187 | 1554877622 | Python | Python (3.4.3) | py | Runtime Error | 21 | 3316 | 600 | from collections import defaultdict
vertices, edges = map(int, input().split(" "))
connect = defaultdict(set)
for i in range(vertices):
a, b = map(int, input().split(" "))
connect[a] |= {b}
connect[b] |= {a}
answer = 0
queue = [[set(), 1]]
while queue:
visited, now = queue.pop()
visited |= {now}
# print(visited, now, need_visit)
if len(visited) == vertices:
answer += 1
else:
for conn in connect[now]:
if conn not in visited:
queue.append([visited | {conn}, conn])
# print(visited, now, need_visit)
print(answer) |
s152467545 | p03805 | u594567187 | 1554705597 | Python | Python (3.4.3) | py | Runtime Error | 28 | 3316 | 1238 | from collections import defaultdict
vertices, edges = map(int, input().split(" "))
connect = defaultdict(set)
for i in range(vertices):
a, b = map(int, input().split(" "))
connect[a] |= {b}
connect[b] |= {a}
comb_answers = defaultdict(int)
def bfs(start, connect, visited):
queue = [start]
while queue:
here = queue.pop()
visited |= {here}
for conn in connect[here]:
if conn not in visited:
visited |= {conn}
queue.append(conn)
return visited
for e in connect[1]:
need_visit = bfs(e, connect, {1})
temp_answer = 0
queue = [[{1}, e]]
while queue:
visited, now = queue.pop()
visited |= {now}
# print(visited, now, need_visit)
for conn in connect[now]:
if conn not in visited:
queue.append([visited | {conn}, conn])
if visited == need_visit:
for c in comb_answers.keys():
if c in visited:
comb_answers[c] += 1
break
else:
comb_answers[e] += 1
# print(visited, now, need_visit)
answer = 1
for v in comb_answers.values():
answer *= v
print(answer) |
s809728280 | p03805 | u151625340 | 1554644765 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3188 | 577 | N,M = map(int,input().split())
dict = {}
for m in range(M):
a,b = map(int,input().split())
if a in dict:
dict[a].append(b)
else:
dict[a] = [b]
if b in dict:
dict[b].append(a)
else:
dict[b] = [a]
ans = 0
l = [0 for i in range(N+1)]
l[1] = 1
def dfs(now):
global l
global ans
if sum(l) == N:
ans += 1
return(0)
for i in dict[now]:
if l[i] == 0:
l[i] += 1
now = i
dfs(l,now)
l[i] -= 1
else:
continue
dfs(l,1)
print(ans)
|
s039517482 | p03805 | u285443936 | 1554519402 | Python | Python (3.4.3) | py | Runtime Error | 86 | 3064 | 526 | N, M = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(M)]
visited = {i:False for i in range(M)}
visited[0] = True
ans = 0
def DFS(x):
global ans
nextx = []
if False not in visited.values():
ans += 1
return
for i in range(M):
if x in ab[i]:
xcol = ab[i].index(x)
if visited[ab[i][1-xcol]-1]:
continue
else:
nextx.append(ab[i][1-xcol])
for i in nextx:
visited[i-1] = True
DFS(i)
visited[i-1] = False
return
DFS(1)
print(ans) |
s286696330 | p03805 | u466331465 | 1554506499 | Python | Python (3.4.3) | py | Runtime Error | 31 | 3316 | 502 | from collections import deque
def dfs(v,N,memo):
ans =0
if not False in memo.values():
return 1
for u in E[v]:
if memo[u]!=True:
memo[u] = True
ans += dfs(u,N,memo)
memo[u] = False
return ans
N,M = list(map(int,input().split()))
memo = {i:False for i in range(1,N+1)}
E = {i: []for i in range(1,M+1)}
for i in range(M):
u,v = list(map(int,input().split()))
E[u].append(v)
E[v].append(u)
memo[1] = True
print(dfs(1,N,memo)) |
s266019728 | p03805 | u594567187 | 1554445937 | Python | Python (3.4.3) | py | Runtime Error | 24 | 3316 | 514 | from collections import defaultdict
vertices, edges = map(int, input().split(" "))
connect = defaultdict(set)
for i in range(vertices):
a, b = map(int, input().split(" "))
connect[a] |= {b}
connect[b] |= {a}
answer = 0
queue = [[set(), 1]]
while queue:
visited, now = queue.pop()
visited |= {now}
if len(visited) == vertices:
answer += 1
continue
for conn in connect[now]:
if conn not in visited:
queue.append([visited | {conn}, conn])
print(answer) |
s629194471 | p03805 | u513900925 | 1554410644 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 433 | N,M = map(int,input().split())
edge_list = [list() for i in range(N)]
for i in range(M):
a,b = map(int,input().split())
edge_list[a-1].append(b-1)
edge_list[b-1].append(a-1)
done = [0] * n
chk = 0
def dfs(s,done):
done = done[:]
global chk
done[s] = True
if all(done):
chk += 1
for i in edges[u]:
if not done[i]:
dfs(i.done)
return 0
dfs(0,done)
print(chk) |
s136257552 | p03805 | u013408661 | 1554400426 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 416 | import sys
n,m=map(int,input().split())
number=[[int(i) for i in l.split()] for l in sys.stdin]
visit=[0]*(n+1)
visit[1]=1
node=[[] for i in range(n+1)]
for i in number:
node[i[0]].append(i[1])
node[i[1]].appedn(i[0])
ans=0
def dfs(x):
global ans
if visit.count(1)==n:
ans+=1
return 0
for i in node[x]:
if visit[i]==0:
visit[i]=1
dfs(i)
visit[i]=0
return 0
dfs(1)
print(ans) |
s693316443 | p03805 | u013408661 | 1554400322 | Python | Python (3.4.3) | py | Runtime Error | 19 | 3064 | 405 | import sys
n,m=map(int,input().split())
number=[[int(i) for i in l.split()] for l in sys.stdin]
visit=[0]*(n+1)
node=[[] for i in range(n+1)]
for i in number:
node[i[0]].append(i[1])
node[i[1]].appedn(i[0])
ans=0
def dfs(x):
global ans
if visit.count(1)==n:
ans+=1
return 0
for i in node[x]:
if visit[i]==0:
visit[i]=1
dfs(i)
visit[i]=0
return 0
dfs(1)
print(ans) |
s593578534 | p03805 | u013408661 | 1554400281 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 406 | import sys
n,m=map(int,input().split())
number=[[int(i) for i in l.split()] for l in sys.stdin]
visit=[0]*(n+1)
node=[[] for i in range(n+1)]
for i in number:
node[i[0]].append(i[1])
node[i[1]].appedn(i[0])
ans=0
def dfs(x):
global ans
if visit.count(1)==n:
ans+=1
return 0
for i in visit[x]:
if visit[i]==0:
visit[i]=1
dfs(i)
visit[i]=0
return 0
dfs(1)
print(ans) |
s984920442 | p03805 | u013408661 | 1554392373 | Python | Python (3.4.3) | py | Runtime Error | 30 | 3192 | 1197 | import sys
sys.setrecursionlimit(20)
n,m=map(int,input().split())
number=[[int(i) for i in l.split()] for l in sys.stdin]
startwithone=[i for i in number if i[0]==1]
notstartwithone=[i for i in number if i[0]!=1]
if len(startwithone)==0:
print(0)
exit()
if len(startwithone)==0:
if n==2:
print(1)
exit()
print(0)
exit()
ans=0
flag2=0
def find(x):
if root[x]==x:
return x
return find(root[x])
def check(x,y):
return find(x)==find(y)
def union(x,y):
if find(1)==find(x):
root[y]=x
else:
root[x]=y
def dfs(i,flag):
global ans
global root
global flag2
flag2=flag
if bin(flag)[2:].count("1")!=n-2 or len(notstartwithone)<i:
if len(notstartwithone)<i:
return 0
dfs(i+1,flag|2**i)
dfs(i+1,flag)
root=[i for i in defroot]
rank=[i for i in defrank]
for j in range(len(notstartwithone)):
if flag&2**j>0:
union(notstartwithone[j][0],notstartwithone[j][1])
for j in range(1,n):
if check(j,j+1)==False:
return 0
if len(set(root))==n:
ans+=1
for i in startwithone:
root=[i for i in range(n+1)]
rank=[0]*(n+1)
union(i[0],i[1])
defroot=[i for i in root]
defrank=[i for i in rank]
dfs(0,0)
print(ans) |
s058079099 | p03805 | u013408661 | 1554392223 | Python | Python (3.4.3) | py | Runtime Error | 30 | 3188 | 1072 | import sys
sys.setrecursionlimit(20)
n,m=map(int,input().split())
number=[[int(i) for i in l.split()] for l in sys.stdin]
startwithone=[i for i in number if i[0]==1]
notstartwithone=[i for i in number if i[0]!=1]
ans=0
flag2=0
def find(x):
if root[x]==x:
return x
return find(root[x])
def check(x,y):
return find(x)==find(y)
def union(x,y):
if find(1)==find(x):
root[y]=x
else:
root[x]=y
def dfs(i,flag):
global ans
global root
global flag2
flag2=flag
if bin(flag)[2:].count("1")!=n-2 or len(notstartwithone)<i:
if len(notstartwithone)<i:
return 0
dfs(i+1,flag|2**i)
dfs(i+1,flag)
root=[i for i in defroot]
rank=[i for i in defrank]
for j in range(len(notstartwithone)):
if flag&2**j>0:
union(notstartwithone[j][0],notstartwithone[j][1])
for j in range(1,n):
if check(j,j+1)==False:
return 0
if len(set(root))==n:
ans+=1
for i in startwithone:
root=[i for i in range(n+1)]
rank=[0]*(n+1)
union(i[0],i[1])
defroot=[i for i in root]
defrank=[i for i in rank]
dfs(0,0)
print(ans) |
s199904692 | p03805 | u013408661 | 1554352440 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 743 | import sys
n,m=map(int,input().split())
number=[[int(i) for i in l.split()] for l in sys.stdin]
ans=0
root=[i for i in range(n)]
rank=[0]*(n+1)
def find(x):
if root[x]==x:
return x
root[x]=find(root[x])
return root[x]
def check(x,y):
return find(x)==find(y)
def union(x,y):
x=find(x)
y=find(y)
if rank[x]>=rank[y]:
root[y]=x
else:
root[x]=y
if rank[x]==rank[y]:
rank[x]+=1
def dfs(i,flag):
if str(flag).count(1)!=n-1:
if i==m:
return 0
dfs(i+1,flag|2**i)
dfs(i+1|flag)
root=[i for i in range(n)]
rank=[0]*(n+1)
for j in range(m):
if flag&2**j>0:
union(number[j][0],number[j][1])
for j in range(1,n):
if check(i,i+1)==False:
return 0
ans+=1
dfs(0,0)
print(ans) |
s580015649 | p03805 | u588081069 | 1554230754 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 835 | 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)
|
s498058004 | p03805 | u588081069 | 1554226465 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 1680 | 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))
"""深さ優先探索: depthFirst
# 初期化
# nodeを展開し,nodeをclosed_listに入れる
# 2つの条件を満たしたら,childrenをopen_listに入れる
# # 1. open_listに同じchildrenがない
# # 2. closedに同じchildrenがない
# 条件を満たせばcntをインクリメントする
"""
# 初期化
node = "1"
# 探索対象
open_list = [node]
# 探索済み
closed_list = []
cnt = 0
while True:
# for i in range(10):
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:
# nodeの展開
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)
|
s052880314 | p03805 | u620945921 | 1554137203 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 654 | n,m=(int(i) for i in input().split())
mtrx=[[int(i) for i in input().split()] for j in range(m)]
mtrx=[[mtrx[j][0]-1,mtrx[j][1]-1] for j in range(m)]
adjacent=[[] for i in range(n)]
#print(adjacent)
for i in range(n):
adjacent[mtrx[i][0]]+=[mtrx[i][1]]
adjacent[mtrx[i][1]]+=[mtrx[i][0]]
#print(adjacent)
cnt=[]
def dfs(path):
p=path[-1]
if len(set(path))==n:
# print(path)
cnt.append(1)
return
else:
for x in adjacent[p]:
if x in path:
continue
else:
path.append(x)
dfs(path)
path.pop()
dfs([0])
print(len(cnt)) |
s892547326 | p03805 | u203377003 | 1554086609 | Python | Python (3.4.3) | py | Runtime Error | 26 | 3064 | 783 | n, m = map(int, input().split())
D = [list() for i in range(n)]
for i in range(m):
a, b = map(int, input().split())
D[a-1].append(b-1)
D[b-1].append(a-1)
d = [0]*n
d[0] = 1
cnt = 0
def dfs(x):
global cnt
if all(d):
cnt += 1
return
for i in D[x]:
if d[i] == 0:
d[i] = 1
dfs(i)
d[i] = 0
dfs(0)
print(cnt)
n, m = map(int, input().split())
D = [list() for i in range(n)]
for i in range(m):
a, b = map(int, input().split())
D[a-1].append(b-1)
D[b-1].append(a-1)
d = [0]*n
d[0] = 1
cnt = 0
def dfs(x):
global cnt
if all(d):
cnt += 1
return
for i in D[x]:
if d[i] == 0:
d[i] = 1
dfs(i)
d[i] = 0
dfs(0)
print(cnt) |
s968971737 | p03805 | u620945921 | 1554086383 | Python | Python (3.4.3) | py | Runtime Error | 19 | 3064 | 696 | n,m=(int(i) for i in input().split())
#print(n,m)
mtrx=[[int(i)-1 for i in input().split()] for j in range(m)]
#print(mtrx)
adjacent=[[i] for i in range(n)]
#print(adjacent)
for i in range(n):
adjacent[mtrx[i][0]]+=[mtrx[i][1]]
adjacent[mtrx[i][1]]+=[mtrx[i][0]]
#print(adjacent)
cnt=[]
def dfs(goal,path):
p=path[-1]
if p==goal:
cnt.append(len(set(path)))
return
else:
for x in adjacent[p]:
if x in path:
continue
else:
path.append(x)
dfs(goal,path)
path.pop()
for i in range(1,n):
dfs(i,[0])
if m==0:
print(0)
else:
print(cnt.count(n)) |
s505063471 | p03805 | u203377003 | 1554086342 | Python | Python (3.4.3) | py | Runtime Error | 26 | 3064 | 793 | n, m = map(int, input().split())
D = [list() for i in range(n)]
for i in range(m):
a, b = map(int, input().split())
D[a-1].append(b-1)
D[b-1].append(a-1)
d = [0]*n
d[0] = 1
cnt = 0
def dfs(x):
global cnt
if all(d):
cnt += 1
return
for i in D[x]:
if d[i] == 0:
d[i] = 1
dfs(i)
d[i] = 0
dfs(0)
print(cnt)
n, m = map(int, input().split())
D = [list() for i in range(n)]
for i in range(m):
a, b = map(int, input().split())
D[a-1].append(b-1)
D[b-1].append(a-1)
d = [0]*n
d[0] = 1
cnt = 0
def dfs(x):
global cnt
if all(d):
cnt += 1
return
for i in D[x]:
if d[i] == 0:
d[i] = 1
dfs(i)
d[i] = 0
dfs(0)
print(cnt) |
s479087196 | p03805 | u203377003 | 1554086174 | Python | Python (3.4.3) | py | Runtime Error | 26 | 3064 | 784 | n, m = map(int, input().split())
D = [list() for i in range(n)]
for i in range(m):
a, b = map(int, input().split())
D[a-1].append(b-1)
D[b-1].append(a-1)
d = [0]*n
d[0] = 1
cnt = 0
def dfs(x):
global cnt
if all(d):
cnt += 1
return
for i in D[x]:
if d[i] == 0:
d[i] = 1
dfs(i)
d[i] = 0
dfs(0)
print(cnt)
n, m = map(int, input().split())
D = [list() for i in range(n)]
for i in range(m):
a, b = map(int, input().split())
D[a-1].append(b-1)
D[b-1].append(a-1)
d = [0]*n
d[0] = 1
cnt = 0
def dfs(x):
global cnt
if all(d):
cnt += 1
return
for i in D[x]:
if d[i] == 0:
d[i] = 1
dfs(i)
d[i] = 0
dfs(0)
print(cnt)
|
s475638130 | p03805 | u620945921 | 1554085856 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 670 | n,m=(int(i) for i in input().split())
#print(n,m)
mtrx=[[int(i)-1 for i in input().split()] for j in range(m)]
#print(mtrx)
adjacent=[[i] for i in range(n)]
#print(adjacent)
for i in range(n):
adjacent[mtrx[i][0]]+=[mtrx[i][1]]
adjacent[mtrx[i][1]]+=[mtrx[i][0]]
#print(adjacent)
cnt=[]
def dfs(goal,path):
p=path[-1]
if p==goal:
cnt.append(len(set(path)))
return
else:
for x in adjacent[p]:
if x in path:
continue
else:
path.append(x)
dfs(goal,path)
path.pop()
for i in range(1,n):
dfs(i,[0])
print(cnt.count(n))
|
s063348131 | p03805 | u620945921 | 1554085476 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 664 | n,m=(int(i) for i in input().split())
#print(n,m)
mtrx=[[int(i)-1 for i in input().split()] for j in range(m)]
#print(mtrx)
adjacent=[[] for i in range(n)]
#print(adjacent)
for i in range(n):
adjacent[mtrx[i][0]]+=[mtrx[i][1]]
adjacent[mtrx[i][1]]+=[mtrx[i][0]]
#print(adjacent)
cnt=[]
def dfs(goal,path):
p=path[-1]
if p==goal:
cnt.append(len(path))
return
else:
for x in adjacent[p]:
if x in path:
continue
else:
path.append(x)
dfs(goal,path)
path.pop()
for i in range(1,n):
dfs(i,[0])
print(cnt.count(n))
|
s038476640 | p03805 | u620945921 | 1554084157 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 664 | n,m=(int(i) for i in input().split())
#print(n,m)
mtrx=[[int(i)-1 for i in input().split()] for j in range(m)]
#print(mtrx)
adjacent=[[i] for i in range(n)]
#print(adjacent)
for i in range(n):
adjacent[mtrx[i][0]]+=[mtrx[i][1]]
adjacent[mtrx[i][1]]+=[mtrx[i][0]]
#print(adjacent)
cnt=[]
def dfs(goal,path):
p=path[-1]
if p==goal:
cnt.append(len(path))
return
else:
for x in adjacent[p]:
if x in path:
continue
else:
path.append(x)
dfs(goal,path)
path.pop()
for i in range(1,n):
dfs(i,[0])
print(cnt.count(n)) |
s254223110 | p03805 | u538381753 | 1554067419 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 497 | n, m = [int(x) for x in input().split(' ')]
edges = set([tuple(input().split(' ')) for _ in range(n)])
edges.update([(x[1], x[0]) for x in list(edges)])
def dfs(node, history):
ans = 0
if len(history) == n - 1:
return 1
nextnodes = [
x[1] for x in edges if x[1] not in history and x[0] == node and x[1] != node
]
history.add(node)
for next in nextnodes:
ans += dfs(next, history)
history.remove(node)
return ans
print(dfs('1', set()))
|
s222734065 | p03805 | u538381753 | 1554067368 | Python | Python (3.4.3) | py | Runtime Error | 19 | 3064 | 520 | n, m = [int(x) for x in input().split(' ')]
edges = set([tuple(input().split(' ')) for _ in range(n)])
edges.update([(x[1], x[0]) for x in list(edges)])
def dfs(node, history):
ans = 0
if len(history) == n - 1:
print(history)
return 1
nextnodes = [
x[1] for x in edges if x[1] not in history and x[0] == node and x[1] != node
]
history.add(node)
for next in nextnodes:
ans += dfs(next, history)
history.remove(node)
return ans
print(dfs('1', set()))
|
s224982912 | p03805 | u538381753 | 1554063730 | Python | Python (3.4.3) | py | Runtime Error | 19 | 3064 | 462 | n, m = [int(x) for x in input().split(' ')]
edges = set([tuple(input().split(' ')) for _ in range(n)])
edges.update([(x[1], x[0]) for x in list(edges)])
def dfs(node, history):
ans = 0
if len(history) == n:
return 1
nextnodes = [
x[1] for x in edges if x[1] not in history and x[0] == node and x[1] != node
]
for next in nextnodes:
ans += dfs(next, history | set(next))
return ans
print(dfs('1', set(['1']))) |
s485698971 | p03805 | u228759454 | 1553973133 | Python | Python (3.4.3) | py | Runtime Error | 302 | 12484 | 703 | import numpy as np
N, M = map(int, input().split())
G = np.zeros((N, M))
total_path_num = 0
not_reach_node = list(range(1, N))
node = 0
for i in range(M):
a, b = map(int, input().split())
G[a-1, b-1] += 1
G[b-1, a-1] += 1
def search_path(G, not_reach_node, node):
global total_path_num
if len(not_reach_node) == 0:
total_path_num += 1
return total_path_num
for x in np.where((G[node] == 1))[0]:
try:
if (x in not_reach_node):
not_reach_node_copy = not_reach_node.copy()
not_reach_node_copy.remove(x)
search_path(G, not_reach_node_copy, x)
except:
pass
search_path(G, not_reach_node, node)
print(total_path_num) |
s809803999 | p03805 | u228759454 | 1553972860 | Python | Python (3.4.3) | py | Runtime Error | 296 | 12500 | 661 | import numpy as np
N, M = map(int, input().split())
G = np.zeros((N, M))
total_path_num = 0
not_reach_node = list(range(1, N))
node = 0
for i in range(M):
a, b = map(int, input().split())
G[a-1, b-1] += 1
G[b-1, a-1] += 1
def search_path(G, not_reach_node, node):
global total_path_num
if len(not_reach_node) == 0:
total_path_num += 1
return total_path_num
for x in np.where((G[node] == 1))[0]:
if (x in not_reach_node):
not_reach_node_copy = not_reach_node.copy()
not_reach_node_copy.remove(x)
search_path(G, not_reach_node_copy, x)
search_path(G, not_reach_node, node)
print(total_path_num)
|
s824965761 | p03805 | u707498674 | 1553909400 | Python | Python (3.4.3) | py | Runtime Error | 23 | 3064 | 362 | import itertools
N, M = map(int, input().split())
E = [[] for i in range(N)]
for i in range(N):
a, b = map(int, input().split())
a -= 1
b -= 1
E[a].append(b)
E[b].append(a)
count = 0
for V in itertools.permutations(range(1, N)):
if V[0] in E[0] and all(V[i] in E[V[i-1]] for i in range(1, N-1)):
count += 1
print(count)
|
s225882538 | p03805 | u707498674 | 1553909227 | Python | Python (3.4.3) | py | Runtime Error | 25 | 3064 | 377 | import itertools
N, M = map(int, input().split())
E = [[N] * (N + 1) for i in range(N + 1)]
for i in range(N):
a, b = map(int, input().split())
a -= 1
b -= 1
E[a].append(b)
E[b].append(a)
count = 0
for V in itertools.permutations(range(1, N)):
if V[0] in E[0] and all(V[i] in E[V[i-1]] for i in range(1, N-1)):
count += 1
print(count)
|
s565916562 | p03805 | u707498674 | 1553909127 | Python | Python (3.4.3) | py | Runtime Error | 23 | 3064 | 407 | import itertools
N, M = map(int, input().split())
E = [[-1] * N for i in range(N)]
for i in range(N):
a, b = map(int, input().split())
a -= 1
b -= 1
E[a].append(b)
E[b].append(a)
count = 0
#V = (1,2,3,4,5,6), (1,2,3,4,6,5) ...
for V in itertools.permutations(range(1, N)):
if V[0] in E[0] and all(V[i] in E[V[i-1]] for i in range(1, N-1)):
count += 1
print(count)
|
s337822199 | p03805 | u707498674 | 1553908606 | Python | Python (3.4.3) | py | Runtime Error | 24 | 3064 | 367 | import itertools
N, M = map(int, input().split())
E = [[0] * N for i in range(N)]
for i in range(N):
a, b = map(int, input().split())
a -= 1
b -= 1
E[a].append(b)
E[b].append(a)
count = 0
for V in itertools.permutations(range(1, N)):
if V[0] in E[0] and all(V[i] in E[V[i-1]] for i in range(1, N-1)):
count += 1
print(count)
|
s552649841 | p03805 | u334712262 | 1553455424 | Python | Python (3.4.3) | py | Runtime Error | 42 | 5692 | 1496 | # -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub
sys.setrecursionlimit(100000)
def read_int():
return int(input())
def read_int_n():
return list(map(int, input().split()))
def read_float():
return float(input())
def read_float_n():
return list(map(float, input().split()))
def read_str():
return input().strip()
def read_str_n():
return list(map(str, input().split()))
def error_print(*args):
print(*args, file=sys.stderr)
def mt(f):
import time
def wrap(*args, **kwargs):
s = time.time()
ret = f(*args, **kwargs)
e = time.time()
error_print(e - s, 'sec')
return ret
return wrap
@mt
def slv(N, M, AB):
g = defaultdict(set)
for a, b in AB:
g[a].add(b)
g[b].add(a)
ans = 0
for ns in permutations(range(2, N+1), N-1):
ns = list(ns)
ns.insert(0, 1)
for i in range(N-1):
if ns[i+1] not in g[ns[i]]:
break
else:
ans += 1
return ans
def main():
N, M = read_int_n()
AB = [read_int_n() for _ in range(N)]
print(slv(N, M, AB))
if __name__ == '__main__':
main()
|
s998302904 | p03805 | u620945921 | 1552685509 | Python | Python (3.4.3) | py | Runtime Error | 2211 | 1708588 | 559 | import itertools
n,m=(int(i) for i in input().split())
#print(n,m)
alist=[]
for i in range(m):
a,b=(int(i) for i in input().split())
alist.append(10*a+b)
alist.append(10*b+a)
#print(alist)
ans=0
mylist=list(itertools.permutations(range(2,m+1)))
#print(mylist)
for j in range(len(mylist)):
mylist1=[10+mylist[j][0]]
for i in range(m-2):
mylist1.append(10*mylist[j][i]+mylist[j][i+1])
# print(mylist1)
test=0
for i in range(len(mylist1)):
if alist.count(mylist1[i]) > 0:
test+=1
if len(mylist1)==test:
ans+=1
print(ans) |
s969721152 | p03805 | u533885955 | 1552661840 | Python | Python (3.4.3) | py | Runtime Error | 102 | 3936 | 545 | #C問題
N,M = map(int,input().split())
ab=[list(map(int,input().split())) for i in range(M)]
reach=[-1]*(N)
reach[0]=0
ans=0
def cdfs(n):
global ans
global reach
if sum(reach)==0:
ans+=1
reach=[-1]*(N)
reach[0]=0
return
else:
for j in range(N):
if reach[j] == -1 and ([n+1,j+1] in ab or [j+1,n+1] in ab):
reach[j] = 0
cdfs(j)
reach[j] = -1
else:
pass
return
reach[0]=0
cdfs(0)
print(ans)
|
s028911225 | p03805 | u411858517 | 1552174076 | Python | Python (3.4.3) | py | Runtime Error | 121 | 8052 | 820 | N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
V = [i+1 for i in range(N)]
import itertools
ptr = list(itertools.permutations(V, N))
res = 0
for p in ptr:
if p[0] == 1:
now = 1
flag = [0 for _ in range(N)]
flag[0] = 1
for i in range(N-1):
for j in range(M):
if A[j][0] == now and A[j][1] == p[i+1]:
flag[A[j][1] - 1] += 1
now = p[i+1]
if A[j][1] == now and A[j][0] == p[i+1]:
flag[A[j][0] - 1] += 1
now = p[i+1]
tmp = 1
for i in range(N):
if flag[i] != 1:
tmp = 0
break
res += tmp
print(res) |
s806969687 | p03805 | u497596438 | 1552088674 | Python | PyPy3 (2.4.0) | py | Runtime Error | 203 | 41968 | 470 | import itertools
N,M=map(int,input().split())
ab=[]
for i in range(N):
a,b=map(int,input().split())
ab.append([a,b])
per=[i for i in range(2,N+1)]
permu=[]
for i in itertools.permutations(per):
lst=[1]
lst.extend(i)
permu.append(lst)
num=0
for lst in permu:
can=True
for i in range(N-1):
if not([lst[i],lst[i+1]] in ab) and not([lst[i+1],lst[i]] in ab):
can=False
break
if can:
num+=1
print(num)
|
s872444771 | p03805 | u367130284 | 1551260761 | Python | Python (3.4.3) | py | Runtime Error | 28 | 3064 | 674 | n,m=map(int,input().split())
AdjacencyMatrix=[[0]*n for i in [0]*m]
for i in [0]*m:
a,b=map(int,input().split())
AdjacencyMatrix[a-1][b-1]=1
AdjacencyMatrix[b-1][a-1]=1
#print(AdjacencyMatrix)
def DFS(point,TFlist):
# print(TFlist)
if not False in TFlist:
return 1
ans=0
for i in range(n):
if not AdjacencyMatrix[point][i]:
continue #AMの0なら処理せず
if TFlist[i]:
continue #探索済みなら処理せず
TFlist[i]=True #探索したのでTFlistをTrue
ans+=DFS(i,TFlist) #再帰的にansを加算
TFlist[i]=False #枝を戻すためにTFlistをFalse
return ans
TFlist=[True]+[False]*(n-1)
print(DFS(0,TFlist)) |
s891179193 | p03805 | u136090046 | 1550453286 | Python | Python (3.4.3) | py | Runtime Error | 128 | 3064 | 699 | n, m = map(int, input().split())
array = [[int(x) for x in input().split()] for y in range(m)]
modify = [[] for x in range(m + 1)]
for value1, value2 in array:
modify[value1].append(value2)
modify[value2].append(value1)
def solver(start, array, visited, cnt):
if visited[start]:
return cnt
else:
visited[start] = True
for i in array[start]:
if len(set(visited)) == 1:
return cnt+1
else:
cnt = solver(i, array, list(visited), cnt)
return cnt
start = modify[1]
res = 0
for i in start:
visited = [False if x != 0 and x != 1 else True for x in range(m + 1)]
res += solver(i, modify, visited, 0)
print(res)
|
s894542083 | p03805 | u945418216 | 1550364045 | Python | Python (3.4.3) | py | Runtime Error | 36 | 3064 | 688 | N,M = map(int,input().split())
a = [0]*M
b = [0]*M
for i in range(M):
a_, b_ = map(int,input().split())
# 配列の添字としたいので-1
a[i] = a_ -1
b[i] = b_ -1
route = [0]*N # 通過済
connect=[[0]*M for _ in range(M)]
def dfs(now, depth):
# print("#"*depth,now, depth)
# 通過済ならNG
if route[now]: return 0
# depth = Nなら達成
if depth == N: return 1
route[now] = 1
ans = 0
for i in range(N):
if connect[now][i]:
ans += dfs(i, depth+1)
route[now] = 0
return ans
for i in range(M):
connect[a[i]][b[i]] = connect[b[i]][a[i]] = 1
# for xx in connect:
# print(xx)
print(dfs(0,1)) |
s374028682 | p03805 | u983918956 | 1548549569 | Python | Python (3.4.3) | py | Runtime Error | 32 | 3064 | 540 | import sys
sys.setrecursionlimit(10**9)
N,M = map(int,input().split())
note = [[0]*M for i in range(N)]
visited = [False]*N
visited[0] = True
ans = 0
for i in range(M):
a,b = map(int,input().split())
a -= 1
b -= 1
note[a][b] = 1
note[b][a] = 1
def dfs(p=0, depth=0):
if depth == N-1:
global ans
ans += 1
return
for i in range(N):
if note[p][i] == 1 and visited[i] is False:
visited[i] = True
dfs(i,depth+1)
visited[i] = False
dfs()
print(ans) |
s966791527 | p03805 | u983918956 | 1548548823 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 483 | import sys
sys.setrecursionlimit(10**9)
N,M = map(int,input().split())
note = [[0]*M for i in range(N)]
visited = [False]*N
ans = 0
for i in range(M):
a,b = map(int,input().split())
a -= 1
b -= 1
note[a][b] = 1
note[b][a] = 1
def dfs(p=0, depth=0):
if depth == N-2:
global ans
ans += 1
return
visited[p] = True
for i in range(N):
if note[p][i] == 1 and visited[i] is False:
dfs(i,depth+1)
dfs()
print(ans) |
s715455324 | p03805 | u983918956 | 1548548162 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 620 | N,M = map(int,input().split())
note = [[0]*M for i in range(N)]
visited = [False]*N
ans = 0
for i in range(M):
a,b = map(int,input().split())
a -= 1
b -= 1
note[a][b] = 1
note[b][a] = 1
def dfs(p = 0):
True_count = 0
False_count = 0
for f in visited:
if f is True:
True_count += 1
elif f is False:
False_count += 1
if True_count == N-1 and False_count == 1:
global ans
ans += 1
return
visited[p] = True
for i in range(N):
if note[p][i] == 1 and visited[i] is False:
dfs(i)
dfs()
print(ans) |
s445268066 | p03805 | u575431498 | 1547175762 | Python | Python (3.4.3) | py | Runtime Error | 30 | 3064 | 464 | def dfs(i):
if all(visited):
global ans
ans += 1
return
for j in range(N):
if g[i][j] and not visited[j]:
visited[j] = True
dfs(j)
visited[j] = False
N, M = map(int, input().split())
g = [[False] * M for _ in range(M)]
for _ in range(M):
a, b = map(int, input().split())
g[a-1][b-1] = True
g[b-1][a-1] = True
ans = 0
visited = [False] * N
visited[0] = True
dfs(0)
print(ans) |
s934617149 | p03805 | u575431498 | 1547175656 | Python | Python (3.4.3) | py | Runtime Error | 30 | 3064 | 464 | def dfs(i):
if all(visited):
global ans
ans += 1
return
for j in range(N):
if g[i][j] and not visited[j]:
visited[j] = True
dfs(j)
visited[j] = False
return
N, M = map(int, input().split())
g = [[False] * M for _ in range(M)]
for _ in range(M):
a, b = map(int, input().split())
g[a-1][b-1] = True
g[b-1][a-1] = True
ans = 0
visited = [False] * N
visited[0] = True
dfs(0) |
s224936079 | p03805 | u879870653 | 1546786104 | Python | Python (3.4.3) | py | Runtime Error | 24 | 3064 | 529 | from itertools import permutations
N,M = map(int,input().split())
Matrix = [[False for i in range(N)] for j in range(N)]
for i in range(N) :
a,b = map(int,input().split())
a -= 1
b -= 1
Matrix[a][b] = True
Matrix[b][a] = True
Matrix[i][i] = True
arange = list(range(2,N+1))
ans = 0
for pm in permutations(arange) :
pm = [1] + list(pm)
for row,col in zip(pm,pm[1:]) :
row -= 1
col -= 1
if not Matrix[row][col] :
break
else :
ans += 1
print(ans)
|
s519786213 | p03805 | u879870653 | 1546748794 | Python | Python (3.4.3) | py | Runtime Error | 25 | 3064 | 541 | from itertools import permutations
N,M = map(int,input().split())
Matrix = [[False for i in range(N)] for j in range(N)] #隣接行列
for i in range(N) :
Matrix[i][i] = True
for i in range(N) :
row,column = map(int,input().split())
Matrix[row-1][column-1] = True
Matrix[column-1][row-1] = True
arange = range(2,N+1)
ans = 0
for pm in permutations(arange) :
P = [1] + list(pm)
for row,column in zip(P,P[1:]) :
if not Matrix[row-1][column-1] :
break
else :
ans += 1
print(ans)
|
s476909995 | p03805 | u879870653 | 1546748599 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 553 | from itertools import permutations as pm
N,M = map(int,input().split())
Matrix = [[False for i in range(N)] for j in range(N)] #隣接行列
for i in range(N) :
Matrix[i][i] = True
for i in range(N) :
row,column = map(int,input().split())
Matrix[row-1][column-1] = True
Matrix[column-1][row-1] = True
arange = list(range(2,N+1))
ans = 0
for pm in permutations(arange) :
P = [1] + list(pm)
for row,column in zip(P,P[1:]) :
if not Matrix[row-1][column-1] :
break
else :
ans += 1
print(ans)
|
s550194450 | p03805 | u879870653 | 1546748537 | Python | Python (3.4.3) | py | Runtime Error | 25 | 3064 | 547 | from itertools import permutations
N,M = map(int,input().split())
Matrix = [[False for i in range(N)] for j in range(N)] #隣接行列
for i in range(N) :
Matrix[i][i] = True
for i in range(N) :
row,column = map(int,input().split())
Matrix[row-1][column-1] = True
Matrix[column-1][row-1] = True
arange = list(range(2,N+1))
ans = 0
for pm in permutations(arange) :
P = [1] + list(pm)
for row,column in zip(P,P[1:]) :
if not Matrix[row-1][column-1] :
break
else :
ans += 1
print(ans)
|
s657882668 | p03805 | u879870653 | 1546748487 | Python | Python (3.4.3) | py | Runtime Error | 27 | 3064 | 549 | from itertools import permutations
N,M = map(int,input().split())
Matrix = [[False for i in range(N)] for j in range(N)] #隣接行列
#for i in range(N) :
# Matrix[i][i] = True
for i in range(N) :
row,column = map(int,input().split())
Matrix[row-1][column-1] = True
Matrix[column-1][row-1] = True
arange = list(range(2,N+1))
ans = 0
for pm in permutations(arange) :
P = [1] + list(pm)
for row,column in zip(P,P[1:]) :
if not Matrix[row-1][column-1] :
break
else :
ans += 1
print(ans)
|
s262100035 | p03805 | u903596281 | 1546463558 | Python | Python (3.4.3) | py | Runtime Error | 24 | 3572 | 395 | import itertools
N,M=map(int,input().split())
edge=[]
for i in range(N+1):
edge.append([0]*(N+1))
for j in range(N):
a,b=map(int,input().split())
edge[a][b]=1
edge[b][a]=1
roots=list(itertools.permutations(range(2,N+1)))
ans=0
for root in roots:
pos=1
for i in range(N-1):
nxt=root[i]
if edge[pos][nxt]==1:
pos=nxt
else:
break
else:
ans+=1
print(ans)
|
s196121704 | p03805 | u669696235 | 1545880306 | Python | Python (2.7.6) | py | Runtime Error | 11 | 2696 | 825 | import queue
import copy
s=list()
N,M=map(int,raw_input().split())
n=[[0 for i in range(N)] for j in range(N)]
for i in range(M):
a,b=map(int,raw_input().split())
n[a-1][b-1]=1
n[b-1][a-1]=1
arrived=[0 for i in range(N)]
arrived[0]=1
def solve(s,arrived):
narrived=copy.deepcopy(arrived)
global n
global N
op=False
ans=0
for i in range(N):
arrived=copy.deepcopy(narrived)
if(s[i]==1 and arrived[i]!=1):
op=True
arrived[i]=1
ans+=solve(n[i],arrived)
if(sum(arrived)==N and op==False):
return 1
elif(op==False):
return 0
return ans
print(solve(n[0],arrived))
|
s277342779 | p03805 | u643840641 | 1545545423 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 449 | m, n = map(int, input().split())
edge = [[False for j in range(n)] for i in range(n)]
for _ in range(m):
a, b = map(int, input().split())
edge[a-1][b-1] = edge[b-1][a-1] = True
visited = [False for _ in range(n)]
ans = 0
def dfs(v):
visited[v] = True
if all(x==True for x in visited):
global ans
ans += 1
return
for i in range(0, n):
if edge[v][i]==True and visited[i]==False:
dfs(i)
visited[i] = False
return
dfs(0)
print(ans) |
s746079798 | p03805 | u643840641 | 1545545193 | Python | Python (3.4.3) | py | Runtime Error | 19 | 3064 | 439 | m, n = map(int, input().split())
edge = [[False for j in range(n)] for i in range(n)]
for _ in range(m):
a, b = map(int, input().split())
edge[a-1][b-1] = edge[b-1][a-1] = True
visited = [False for _ in range(n)]
ans = 0
def dfs(v):
visited[v] = True
if all(x==True for x in visited):
global ans
ans += 1
return
for i in range(0, n):
if edge[v][i]==True and visited[i]==False:
dfs(i)
visited[i] = False
dfs(0)
print(ans) |
s821919369 | p03805 | u029329478 | 1542406346 | Python | Python (3.4.3) | py | Runtime Error | 53 | 4332 | 1203 | import copy
class Line:
def __init__(self, a, b):
self.a = a
self.b = b
def generate_patterns(remains, current_pattern, patterns):
if len(remains) == 0:
patterns.append(current_pattern)
return
for remain in remains:
_current_pattern = copy.copy(current_pattern)
_current_pattern.append(remain)
_remains = copy.copy(remains)
_remains.remove(remain)
generate_patterns(_remains, _current_pattern, patterns)
return patterns
N, M = map(int, input().split())
lines = []
for _ in range(N):
a, b = map(int, input().split())
lines.append(Line(a, b))
m = {}
for i in range(1, N+1):
m[i] = {}
for j in range(1, N+1):
m[i][j] = False
for line in lines:
m[line.a][line.b] = True
m[line.b][line.a] = True
remains = []
for i in range(2, N+1):
remains.append(i)
patterns = generate_patterns(remains, [1], [])
cnt = 0
for pattern in patterns:
countable = True
for i in range(len(pattern)-1):
current = pattern[i]
next = pattern[i+1]
if not(m[current][next]):
countable = False
break
if countable:
cnt += 1
print(cnt)
|
s722697617 | p03805 | u029329478 | 1542405863 | Python | Python (3.4.3) | py | Runtime Error | 52 | 4332 | 1171 | import copy
class Line:
def __init__(self, a, b):
self.a = a
self.b = b
def generate_patterns(remains, current_pattern, patterns):
if len(remains) == 0:
patterns.append(current_pattern)
return
for remain in remains:
_current_pattern = copy.copy(current_pattern)
_current_pattern.append(remain)
_remains = copy.copy(remains)
_remains.remove(remain)
generate_patterns(_remains, _current_pattern, patterns)
return patterns
N, M = map(int, input().split())
lines = []
for _ in range(N):
a, b = map(int, input().split())
lines.append(Line(a, b))
m = {}
for line in lines:
m.setdefault(line.a, {})
m.setdefault(line.b, {})
m[line.a][line.b] = True
m[line.b][line.a] = True
remains = []
for i in range(2, N+1):
remains.append(i)
patterns = generate_patterns(remains, [1], [])
cnt = 0
for pattern in patterns:
countable = True
for i in range(len(pattern)-1):
current = pattern[i]
next = pattern[i+1]
if next not in m[current]:
countable = False
break
if countable:
cnt += 1
print(cnt)
|
s981123985 | p03805 | u426108351 | 1540330338 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 677 | N, M = map(int, input().split())
A = []
B = []
for i in xrange(M):
a, b = map(int, input().split())
A.append(a)
B.append(b)
for i in xrange(M):
A.append(B[i])
B.append(A[i])
result = []
stack = []
def path(start, visited):
if len(visited) >= N:
if visited not in result:
result.append(visited[:])
exit
stack.append(start)
if len(stack) > 0:
nextpoint = stack.pop()
if nextpoint not in visited:
visited.append(nextpoint)
for i in xrange(2*M):
if A[i] == start:
path(B[i], visited)
visited.pop()
path(1, [])
print(len(result))
|
s683140295 | p03805 | u426108351 | 1540326766 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 823 | def lensetlist(A):
B = []
for i in A:
if i not in B:
B.append(i)
return len(B)
N, M = map(int, input().split())
A = []
B = []
for i in range(M):
a, b = map(int, input().split())
A.append(a)
B.append(b)
for i in range(M):
A.append(B[i])
B.append(A[i])
result = []
stack = []
ans = 0
count = 0
def path(start, visited):
count += 1
if count > 10000:
break
if len(visited) == N:
result.append(visited[:])
exit
stack.append(start)
while len(stack) > 0:
nextpoint = stack.pop()
if nextpoint in visited:
continue
visited.append(nextpoint)
for i in range(2*M):
if A[i] == start:
path(B[i], visited)
visited.pop()
path(1, [])
print(lensetlist(result))
|
s228483788 | p03805 | u094565093 | 1538687400 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3060 | 319 | N,M=map(int,input().split())
A=[ list(input()) for i in range(N)]
B=[ list(input()) for i in range(M)]
ans='Yes'
for i in range(N-M+1):
for j in range(N-M+1):
for k in range(i,i+M):
for l in range(j,j+M):
if A[k][l]!=B[k-i][l-j]:
ans='No'
print(ans)
|
s259620998 | p03805 | u608088992 | 1538581906 | Python | Python (3.4.3) | py | Runtime Error | 33 | 3064 | 637 | N, M = map(int, input().split())
Edge = [[] for i in range(M)]
for i in range(M):
a, b = map(int, input().split())
a, b = a-1, b-1
Edge[a].append(b)
Edge[b].append(a)
Visited = [False for i in range(N)]
Visited[0] = True
ans = 0
def DFS(i):
allvisited = True
for j in range(N):
if Visited[j] == False:
allvisited = False
if allvisited:
return 1
else:
ret = 0
for node in Edge[i]:
if Visited[node] == False:
Visited[node] = True
ret += DFS(node)
Visited[node] = False
return ret
print(DFS(0)) |
s288863898 | p03805 | u769852547 | 1537153144 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 632 | n, m = map(int, input().split())
ab = [ list(map(int, input().split())) for _ in range(m) ]
graph = [ [False] * 8 for _ in range(8) ]
visited = [False] * n
def dfs(a, N, visited):
finish = True
for i in range(N):
if not visited[i]:
finish = False
if finish:
return 1
ans = 0
for i in range(N):
if not graph[a][i] or visited[i]:
continue
visited[i] = True
ans += dfs(i,N,visited)
visited[i] = False
for i in range(m):
graph[ab[i][0]-1][ab[i][1]-1], graph[ab[i][1]-1][ab[i][0]-1] = True, True
visited[0] = True
print(dfs(0,n,visited)) |
s419532066 | p03805 | u411858517 | 1536161583 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 613 | N, M = map(int, input().split())
E = [[0 for i in range(N)] for j in range(N)] #頂点のつながり
for i in range(M):
a, b = map(int, input().split())
E[a-1][b-1] = 1
E[b-1][a-1] = 1
V = [ 0 for i in range(N)] #訪問済みの頂点が1
V[0] = 1 #頂点1は訪問済み
def DPS(now, V):
res = 0 #全て訪問できた回数
if 0 not in V:
return 1
for i in range(M):
if (E[now][i] == 1) and (V[i] == 0): #現在の頂点と隣接してかつ未訪問の頂点
V[i] = 1
res = res + DPS(i, V)
V[i] = 0
return res
print(DPS(0, V)) |
s875770532 | p03805 | u366644013 | 1535727499 | Python | Python (3.4.3) | py | Runtime Error | 2104 | 3188 | 418 | import itertools
na = lambda: list(map(int, input().split()))
n, m = na()
t = [[0] * 10 for _ in range(10)]
for _ in range(m):
a, b = na()
a -= 1
b -= 1
t[a][b] = t[b][a] = 1
ans = 0
for l in itertools.permutations(range(m)):
if l[0]: continue
flag = 1
for i in range(1, n):
if t[l[i]][l[i-1]] == 0:
flag = 0
break
if flag:
ans += 1
print(ans) |
s549176978 | p03805 | u366644013 | 1535727316 | Python | Python (3.4.3) | py | Runtime Error | 2104 | 3064 | 400 | import itertools
na = lambda: list(map(int, input().split()))
n, m = na()
t = [[0] * 10 for _ in range(10)]
for _ in range(m):
a, b = na()
a -= 1
b -= 1
t[a][b] = t[b][a] = 1
ans = 0
for l in itertools.permutations(range(m)):
if l[0]: continue
flag = 1
for i in range(1, n):
if t[l[i]][l[i-1]] == 0:
flag = 0
if flag:
ans += 1
print(ans) |
s089060629 | p03805 | u108617242 | 1535593167 | Python | Python (3.4.3) | py | Runtime Error | 105 | 3444 | 674 | import copy
def explore(key, flags):
flags = copy.copy(flags)
if flags[key-1]:
return 0
flags[key-1] = True
if False not in flags:
return 1
elif key in graph:
value = graph[key]
ans = 0
for i in value:
ans += explore(i, flags)
return ans
else:
return 0
n, m = map(int, input().split())
graph = {}
for i in range(m):
a, b = map(int, input().split())
if a in graph:
graph[a].append(b)
else:
graph[a] = [b]
if b in graph:
graph[b].append(a)
else:
graph[b] = [a]
flags = [False for i in range(m)]
ans = explore(1, flags)
print(ans)
|
s267868736 | p03805 | u118605645 | 1534338789 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 576 | def search(node):
global ans
if sum(is_visit) == n:
ans += 1
for i in range(n):
if is_neighbor[node][i]:
if not is_visit[i]:
is_visit[i] = True
search(i)
is_visit[i] = False
n, m = map(int, input().split())
is_neighbor = [[False for j in range(n)] for i in range(n)]
for i in range(n):
a, b = map(int, input().split())
is_neighbor[a - 1][b - 1] = True
is_neighbor[b - 1][a - 1] = True
is_visit = [False for _ in range(n)]
ans = 0
is_visit[0] = True
search(0)
print(ans) |
s657224927 | p03805 | u140251125 | 1533261780 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3188 | 488 | # input
N, M = map(int, input().split())
G = [list(map(int, input().split())) for _ in range(M)]
edges = [set() for _ in range(N)]
for a, b in G:
edges[a - 1].add((b - 1, 1))
edges[b - 1].add((a - 1, 1))
def dfs(start, edges, path):
path.append(start)
if len(path) == n:
path.pop()
return 1
ans = 0
for u in edges[start]:
if u in path:
continue
ans += dfs(u, path)
path.pop()
return ans
print(dfs(0, []))
|
s754752454 | p03805 | u858523893 | 1531063652 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3060 | 296 | from itertools import permutations
n, m = map(int, input().split())
paths = set()
for i in range(n) :
_a, _b = map(int, input().split())
# paths.add((_a, _b))
# paths.add((_b, _a))
# print(sum(all(s in paths for s in zip((1,) + p, p)) for p in permutations(range(2, n + 1))))
|
s672372895 | p03805 | u858523893 | 1531063469 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3060 | 292 | from itertools import permutations
n, m = map(int, input().split())
paths = set()
for i in range(n) :
_a, _b = map(int, input().split())
paths.add((_a, _b))
paths.add((_b, _a))
# print(sum(all(s in paths for s in zip((1,) + p, p)) for p in permutations(range(2, n + 1))))
|
s786748614 | p03805 | u858523893 | 1531017926 | Python | Python (3.4.3) | py | Runtime Error | 22 | 3060 | 290 | from itertools import permutations
n, m = map(int, input().split())
paths = set()
for i in range(n) :
_a, _b = map(int, input().split())
paths.add((_a, _b))
paths.add((_b, _a))
print(sum(all(s in paths for s in zip((1,) + p, p)) for p in permutations(range(2, n + 1))))
|
s359495497 | p03805 | u858523893 | 1531017625 | Python | Python (3.4.3) | py | Runtime Error | 22 | 3064 | 314 | from itertools import permutations
n, m = map(int, input().split())
# a, b = [], []
paths = set()
for i in range(n) :
_a, _b = map(int, input().split())
paths.add((_a, _b))
paths.add((_b, _a))
print(sum(all((s, t) in paths for s, t in zip((1,) + p, p)) for p in permutations(range(2, n + 1))))
|
s818146637 | p03805 | u858523893 | 1531017463 | Python | Python (3.4.3) | py | Runtime Error | 21 | 3064 | 388 | from itertools import permutations
n, m = map(int, input().split())
a, b = [], []
for i in range(n) :
_a, _b = map(int, input().split())
a.append(_a)
b.append(_b)
# create set
paths = set()
for i in range(m) :
paths.add((a[i], b[i]))
paths.add((b[i], a[i]))
print(sum(all((s, t) in paths for s, t in zip((1,) + p, p)) for p in permutations(range(2, n + 1))))
|
s126672763 | p03805 | u162660367 | 1530545975 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 551 | import sys
sys.setrecursionlimit(10000)
N,M=list(map(int,input().split()))
graph=[[False]*N for i in range(N)]
for i in range(N):
a,b=list(map(int, input().split()))
graph[a-1][b-1]=True
graph[b-1][a-1]=True
visited=[False]*N
def dfs(v,visited):
if all(visited):
return 1
ret=0
for i in range(N):
if not graph[v][i]:
continue
if visited[i]:
continue
visited[i]=True
ret+=dfs(i,visited)
visited[i]=False
return ret
visited[0]=True
print(dfs(0,visited)) |
s579641767 | p03805 | u162660367 | 1530545500 | Python | Python (3.4.3) | py | Runtime Error | 21 | 3316 | 535 | from collections import defaultdict
import sys
sys.setrecursionlimit(10000)
N,M=list(map(int,input().split()))
links=defaultdict(list)
for i in range(N):
a,b=list(map(int, input().split()))
links[a].append(b)
links[b].append(a)
ct=0
visited=[False]*N
def DFS(idx):
global visited
if all(visited):
global ct
ct+=1
return
for i in links[idx]:
if visited[i-1]:
continue
visited[i-1]=True
DFS(i)
visited[i-1]=False
visited[0]=True
DFS(1)
print(ct) |
s862382901 | p03805 | u162660367 | 1530545125 | Python | Python (3.4.3) | py | Runtime Error | 21 | 3444 | 492 | from collections import defaultdict
import sys
sys.setrecursionlimit(10000)
N,M=list(map(int,input().split()))
links=defaultdict(list)
for i in range(N):
a,b=list(map(int, input().split()))
links[a].append(b)
links[b].append(a)
ct=0
def DFS(idx,visited):
visited.add(idx)
for i in links[idx]:
if i in visited:
continue
visi=set(visited)
DFS(i,visi)
if len(visi)==N:
global ct
ct+=1
DFS(1,set())
print(ct) |
s050802859 | p03805 | u856232850 | 1529533847 | Python | Python (3.4.3) | py | Runtime Error | 2104 | 3064 | 584 | import itertools
n,m = list(map(int,input().split()))
a = []
for i in range(m):
b = (list(map(int,input().split())))
b = [b[0]-1,b[1]-1]
a.append(b)
b = [[0 for i in range(n)] for j in range(n)]
for i in a:
b[i[0]][i[1]] = 1
b[i[1]][i[0]] = 1
c = [i+1 for i in range(m-1)]
ans = 0
for i in itertools.permutations(c,m-1):
j = [0]
j += i
count = 0
for i in range(m-1):
if b[j[i]][j[i+1]] == 1:
count += 1
if count == m-1:
ans += 1
count = 0
else:
break
print(ans) |
s882177668 | p03805 | u136090046 | 1528929370 | Python | Python (3.4.3) | py | Runtime Error | 26 | 3064 | 647 | import sys
n, m = map(int, input().split())
array = [[int(x) for x in input().split()] for x in range(m)]
if m == 0:
print(0)
sys.exit()
adj_array = [[] for x in range(m + 1)]
res_array = [True] + [False for x in range(n)]
for item in array:
adj_array[item[0]].append(item[1])
adj_array[item[1]].append(item[0])
def dfs(n, res_array, res):
res_array[n] = True
if False not in res_array:
return res+1
else:
for next in adj_array[n]:
if not res_array[next]:
res = dfs(next, list(res_array), res)
return res
res = 0
res_array[1] = True
print(dfs(1, res_array, res))
|
s058531577 | p03805 | u136090046 | 1528853476 | Python | Python (3.4.3) | py | Runtime Error | 26 | 3064 | 595 | n, m = map(int, input().split())
array = [[int(x) for x in input().split()] for x in range(m)]
adj_array = [[] for x in range(m + 1)]
res_array = [True] + [False for x in range(n)]
for item in array:
adj_array[item[0]].append(item[1])
adj_array[item[1]].append(item[0])
def dfs(n, res_array, res):
res_array[n] = True
if False not in res_array:
return res+1
else:
for next in adj_array[n]:
if not res_array[next]:
res = dfs(next, list(res_array), res)
return res
res = 0
res_array[1] = True
print(dfs(1, res_array, res))
|
s665584193 | p03805 | u136090046 | 1528852657 | Python | Python (3.4.3) | py | Runtime Error | 28 | 3064 | 595 | n, m = map(int, input().split())
array = [[int(x) for x in input().split()] for x in range(m)]
adj_array = [[] for x in range(m + 1)]
res_array = [True] + [False for x in range(m)]
for item in array:
adj_array[item[0]].append(item[1])
adj_array[item[1]].append(item[0])
def dfs(n, res_array, res):
res_array[n] = True
if False not in res_array:
return res+1
else:
for next in adj_array[n]:
if not res_array[next]:
res = dfs(next, list(res_array), res)
return res
res = 0
res_array[1] = True
print(dfs(1, res_array, res))
|
s149642657 | p03805 | u226108478 | 1528075859 | Python | Python (3.4.3) | py | Runtime Error | 31 | 3316 | 806 | # -*- 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))
|
s830057508 | p03805 | u226108478 | 1528073057 | Python | Python (3.4.3) | py | Runtime Error | 2104 | 3188 | 997 | # -*- 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)
|
s850261693 | p03805 | u226108478 | 1527907108 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 688 | # -*- 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)
|
s102264496 | p03805 | u813450984 | 1527184252 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 1050 | n, m = map(int, input().split())
l = []
combo = []
for i in range(max(n, m) + 1):
l.append([])
def dfs(path, now):
if len(set(path)) == n and not path in combo:
combo.append(path)
return
for i in l[now]:
if not i in path:
temp = path[:]
temp.append(i)
dfs(temp, i)
return
if m == 0:
print(0)
else:
for i in range(m):
f, t = map(int, input().split())
if not t in l[f]:
l[f].append(t)
if not f in l[t]:
l[t].append(f)
dfs([1], 1)
print(len(combo))n, m = map(int, input().split())
l = []
combo = []
for i in range(max(n, m) + 1):
l.append([])
def dfs(path, now):
if len(set(path)) == n and not path in combo:
combo.append(path)
return
for i in l[now]:
if not i in path:
temp = path[:]
temp.append(i)
dfs(temp, i)
return
if m == 0:
print(0)
else:
for i in range(m):
f, t = map(int, input().split())
if not t in l[f]:
l[f].append(t)
if not f in l[t]:
l[t].append(f)
dfs([1], 1)
print(len(combo)) |
s557168054 | p03805 | u813450984 | 1527183876 | Python | Python (3.4.3) | py | Runtime Error | 437 | 3828 | 514 | n, m = map(int, input().split())
l = []
for i in range(m+1):
l.append([])
types = set()
combo = []
for i in range(m):
f, t = map(int, input().split())
if not t in l[f]:
l[f].append(t)
if not f in l[t]:
l[t].append(f)
def dfs(path, now):
if len(set(path)) == n and not path in combo:
combo.append(path)
return
for i in l[now]:
if not i in path:
temp = path[:]
temp.append(i)
dfs(temp, i)
return
if m < n-1:
print(0)
else:
dfs([1], 1)
print(len(combo)) |
s404304809 | p03805 | u813450984 | 1527183725 | Python | Python (3.4.3) | py | Runtime Error | 430 | 3828 | 482 | n, m = map(int, input().split())
l = []
for i in range(m+1):
l.append([])
types = set()
combo = []
for i in range(m):
f, t = map(int, input().split())
if not t in l[f]:
l[f].append(t)
if not f in l[t]:
l[t].append(f)
def dfs(path, now):
if len(set(path)) == n and not path in combo:
combo.append(path)
return
for i in l[now]:
if not i in path:
temp = path[:]
temp.append(i)
dfs(temp, i)
return
dfs([1], 1)
print(len(combo)) |
s028982451 | p03805 | u503901534 | 1526966038 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 689 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
n,m = map(int,input().split())
H = [[0 for _ in range(n)] for _ in range(n) ]
for _ in range(m):
a, b = map(int,input().split())
edge_list.append([a-1,b-1])
H[a-1][b-1] = 1
H[b-1][a-1] = 1
l = [0 for _ in range(n)]
ans = 0
def dfs(node,visited):
global ans
if visited.count(0) == 0:
ans += 1
return 0
else:
visited[node] = 1
for node_,edge_ in enumerate(H[node]):
if edge_ == 1 and visited[node_] != 1:
visited[node_] = 1
visited[node] = 1
dfs(node_,visited)
visited[node_] = 0
dfs(0,l)
print(ans) |
s368221068 | p03805 | u503901534 | 1526965930 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 642 | n,m = map(int,input().split())
H = [[0 for _ in range(n)] for _ in range(n) ]
for _ in range(m):
a, b = map(int,input().split())
edge_list.append([a-1,b-1])
H[a-1][b-1] = 1
H[b-1][a-1] = 1
l = [0 for _ in range(n)]
ans = 0
def dfs(node,visited):
global ans
if visited.count(0) == 0:
ans += 1
return 0
else:
visited[node] = 1
for node_,edge_ in enumerate(H[node]):
if edge_ == 1 and visited[node_] != 1:
visited[node_] = 1
visited[node] = 1
dfs(node_,visited)
visited[node_] = 0
dfs(0,l)
print(ans) |
s391444038 | p03805 | u503901534 | 1526965899 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 657 | n,m = map(int,input().split())
H = [[0 for _ in range(n)] for _ in range(n) ]
for _ in range(m):
a, b = map(int,input().split())
edge_list.append([a-1,b-1])
H[a-1][b-1] = 1
H[b-1][a-1] = 1
l = [0 for _ in range(n)]
ans = 0
def dfs(node,visited):
global ans
if visited.count(0) == 0:
ans += 1
return 0
else:
visited[node] = 1
for node_,edge_ in enumerate(H[node]):
if edge_ == 1 and visited[node_] != 1:
visited[node_] = 1
visited[node] = 1
dfs(node_,visited)
visited[node_] = 0
dfs(0,l)
print(ans) |
s463865893 | p03805 | u503901534 | 1526965869 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 658 | n,m = map(int,input().split())
H = [[0 for _ in range(n)] for _ in range(n) ]
for _ in range(m):
a, b = map(int,input().split())
edge_list.append([a-1,b-1])
H[a-1][b-1] = 1
H[b-1][a-1] = 1
l = [0 for _ in range(n)]
ans = 0
def dfs(node,visited):
global ans
if visited.count(0) == 0:
ans += 1
return 0
else:
visited[node] = 1
for node_,edge_ in enumerate(H[node]):
if edge_ == 1 and visited[node_] != 1:
visited[node_] = 1
visited[node] = 1
dfs(node_,visited)
visited[node_] = 0
dfs(0,l)
print(ans)
|
s380031187 | p03805 | u879302598 | 1526770570 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 622 | from itertools import permutations
def f(n, m, uu, vv):
a = [[0] * n for _ in range(n)]
for u, v in zip(uu, vv):
a[u][v] = 1
a[v][u] = 1
res = 0
for p in permutations(range(n)):
if p[0] != 0:
break
ok = True
for i in range(n-1):。
if a[p[i][p[i+1]] == 0:
ok = False
break
if ok:
res += 1
return res
n,m = map(int,input().split())
a = []
b = []
for i in range(m):
_a, _b = map(int, input().split())
_a -= 1
_b -= 1
a.append(_a)
b.append(_b)
print(f(n, m, a, b))
|
s730652573 | p03805 | u136090046 | 1525557530 | Python | Python (3.4.3) | py | Runtime Error | 47 | 3064 | 685 | import sys
sys.setrecursionlimit(50000)
n, m = map(int, input().split())
array = [[int(x) for x in input().split()] for x in range(m)]
if m == 0 or m == 1:
print(1)
sys.exit()
adjacency_list = [[] for x in range(n + 1)]
for i in array:
adjacency_list[i[0]].append(i[1])
adjacency_list[i[1]].append(i[0])
searched = [False for x in range(m+1)]
def calc(start, maze, searched):
res = 0
if searched[start]:
return 0
searched[start] = True
if searched.count(True) == n:
return 1
else:
for next in maze[start]:
res += calc(next, maze, list(searched))
return res
print(calc(1, adjacency_list, searched))
|
s096402867 | p03805 | u583010173 | 1524643032 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 480 | # -*- coding: utf-8 -*-
n, m = [int(x) for x in input().split()]
adj = [[0]*n for i in range(n)]
visited = [0]*n
for i in range(n):
a, b = [int(x) for x in input().split()]
adj[a-1][b-1] = 1
adj[b-1][a-1] = 1
ans = 0
def dfs(v):
global ans
if sum(visited) == n:
ans += 1
for i in range(n):
if adj[v-1][i] == 1 and visited[i] == 0:
visited[i] = 1
dfs(i+1)
visited[i] = 0
visited[0] = 1
dfs(1)
print(ans) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.