https://programmers.co.kr/learn/courses/30/lessons/86971
전선을 하나씩 끊어 보며 bfs로 풀었다.
from collections import deque
def solution(n,wires):
answer = 101
for i in range(len(wires)):
wire = wires[:i] + wires[i+1:]
graph = [[] for _ in range(n+1)]
visited = [False for _ in range(n+1)]
for w in wire:
graph[w[0]].append(w[1])
graph[w[1]].append(w[0])
queue = deque()
queue.append((1))
grid_count = 1
visited[1] = True
while queue:
now = queue.popleft()
for v in graph[now]:
if visited[v] == False:
visited[v]=True
grid_count +=1
queue.append(v)
answer = min(answer,abs(n-grid_count-grid_count))
return answer
'Programming > Programmers' 카테고리의 다른 글
[프로그래머스] 캐시 (Python) (0) | 2021.11.03 |
---|---|
[프로그래머스] 모음사전 (Python) (0) | 2021.11.03 |
[프로그래머스] 교점에 별 만들기 (Python) (0) | 2021.11.03 |
[프로그래머스] 영어 끝말잇기 (Python) (0) | 2021.11.02 |
[프로그래머스] 삼각 달팽이 (Python) (0) | 2021.11.02 |
댓글