Skip to content

Commit

Permalink
2024-09-04
Browse files Browse the repository at this point in the history
  • Loading branch information
wonjunYou committed Sep 5, 2024
1 parent b5f0561 commit 25ce1c8
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions wonjunYou/BFS/PRG_도넛과_막대_그래프.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from collections import deque

def solution(edges):
MAX_NUMBER = 1000001

start = [[] for _ in range(MAX_NUMBER)]
end = [[] for _ in range(MAX_NUMBER)]
visited = [False] * (MAX_NUMBER)

result = [0, 0, 0, 0]

for a, b in edges:
start[a].append(b)
end[b].append(a)

middle = -1
for node in range(MAX_NUMBER + 1):
if (len(end[node]) == 0 and len(start[node]) >= 2):
middle = node
break

result[0] = middle

def bfs(i):
q = deque()
q.append(i)

visited[i] = True

while q:
node = q.popleft()

if len(start[node]) == 2 and len(end[node]) == 2:
result[3] += 1
return

if not start[node]:
result[2] += 1
return

for next_node in start[node]:
if not visited[next_node]:
visited[next_node] = 1
q.append(next_node)

result[1] += 1

for node in start[middle]:
end[node].remove(middle)
bfs(node)

return result

0 comments on commit 25ce1c8

Please sign in to comment.