-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12-2.py
More file actions
35 lines (30 loc) · 798 Bytes
/
12-2.py
File metadata and controls
35 lines (30 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from collections import defaultdict
graph = defaultdict(lambda: [])
explored = {}
paths = 0
def DFS(start, double=False):
global paths
double_current = False
if start == "end":
paths += 1
return
if start.islower():
if explored.get(start, False):
if double or start == "start":
return
else:
double_current = True
double = True
explored[start] = True
for neighbor in graph[start]:
DFS(neighbor, double)
if not double_current:
explored[start] = False
try:
while True:
start, end = input().split("-")
graph[start].append(end)
graph[end].append(start) # undirected graph
except EOFError:
DFS("start", 0)
print(paths)