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