Skip to content

Commit be50600

Browse files
authored
๐Ÿœ Study: ๋„๋„›๊ณผ ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„ (#56)
1 parent 178002e commit be50600

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import defaultdict, deque
2+
3+
def solution(edges):
4+
answer = [0] * 4
5+
graph = defaultdict(lambda: [[], []])
6+
for e in edges:
7+
graph[e[0]][1].append(e[1])
8+
graph[e[1]][0].append(e[0])
9+
answer[0] = next(g for g in graph
10+
if not graph[g][0] and len(graph[g][1]) > 1)
11+
for g in graph[answer[0]][1]:
12+
sin, dou, s, q = False, False, {g}, deque([g])
13+
while q:
14+
cur = q.popleft()
15+
for i in graph[cur][1]:
16+
if i not in s: s.add(i); q.append(i)
17+
elif not sin: sin = True
18+
else: dou = True
19+
answer[sin + (not (sin ^ dou)) * 2] += 1
20+
return answer

0 commit comments

Comments
ย (0)