https://school.programmers.co.kr/learn/courses/30/lessons/86971
from collections import deque
def solution(n, wires):
graph = [[] for _ in range(n + 1)]
answer = n # wire의 마지막 값 저장
for a, b in wires:
graph[a].append(b)
graph[b].append(a)
def bfs(start):
cnt = 1
visited = [0] * (n + 1)
visited[start] = True
q = deque([(start)])
while q:
now = q.popleft()
for i in graph[now]:
if not visited[i]:
q.append(i)
visited[i] = True
cnt += 1
return cnt
for a, b in wires:
graph[a].remove(b) # a, b를 끊기
graph[b].remove(a)
answer = min(abs(bfs(a) - bfs(b)), answer) # bfs를 돌린 후 절대값 확인
graph[a].append(b) # a, b 다시 연결
graph[b].append(a)
return answer
n = 9
wires = [[1,3],[2,3],[3,4],[4,5],[4,6],[4,7],[7,8],[7,9]]
print(solution(n, wires))
remove는 인자로 값을 받아서 삭제
pop은 인자로 인덱스를 받아서 삭제