# 비슷한 개수 = 두 연결의 차의 절대값이 가장 작다
from collections import deque
def solution(n, wires):
answer = 101
for i in range(len(wires)):
a, b = [wires[i][0]], [wires[i][1]]
q = deque(wires)
while q:
wire = q.popleft()
if wire[0] == a[0] and wire[1] == b[0]:
continue
if wire[0] in a:
a.append(wire[1])
elif wire[1] in a:
a.append(wire[0])
elif wire[0] in b:
b.append(wire[1])
elif wire[1] in b:
b.append(wire[0])
else:
q.append(wire)
answer = min(answer, abs(len(a) - len(b)))
return answer
Python
복사