From 25ce1c84f9255b5023ad545cd59f5e538f646679 Mon Sep 17 00:00:00 2001 From: wonjunYou Date: Fri, 6 Sep 2024 01:30:05 +0900 Subject: [PATCH] 2024-09-04 --- ...0_\352\267\270\353\236\230\355\224\204.py" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "wonjunYou/BFS/PRG_\353\217\204\353\204\233\352\263\274_\353\247\211\353\214\200_\352\267\270\353\236\230\355\224\204.py" diff --git "a/wonjunYou/BFS/PRG_\353\217\204\353\204\233\352\263\274_\353\247\211\353\214\200_\352\267\270\353\236\230\355\224\204.py" "b/wonjunYou/BFS/PRG_\353\217\204\353\204\233\352\263\274_\353\247\211\353\214\200_\352\267\270\353\236\230\355\224\204.py" new file mode 100644 index 0000000..7b6ac4f --- /dev/null +++ "b/wonjunYou/BFS/PRG_\353\217\204\353\204\233\352\263\274_\353\247\211\353\214\200_\352\267\270\353\236\230\355\224\204.py" @@ -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