99클럽 코테 스터디 10일차 TIL + 전력망을 둘로 나누기

히치키치·2024년 5월 29일
0

항해99코테스터디

목록 보기
4/13

https://school.programmers.co.kr/learn/courses/30/lessons/86971

제출 코드

from collections import deque

def solution(n, wires):
    answer = float("inf")
    graph =[[] for _ in range(n+1)]
    for a,b in wires:
        graph[a].append(b)
        graph[b].append(a)
    for x,y in wires:
        que =  deque()
        que.append(x)
        count = 0
        visited = [False]*(n+1)
        visited[x] = True

        while que:
            x = que.popleft()
            count+=1
            for next_ in graph[x]:
                if next_ != y and not(visited[next_]):
                    visited[next_]=True
                    ##print(x,next_,count)
                    que.append(next_)

        
        #print(count, n-count, abs(n-count-count))       
        answer = min(answer, abs(n-count-count) )
            

    return answer

0개의 댓글