Skip to content

Latest commit

 

History

History
34 lines (33 loc) · 902 Bytes

File metadata and controls

34 lines (33 loc) · 902 Bytes

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

  • 네트워크 dfs
def solution(n, computers):
    answer = 0
    popi =[]
    for i in range(n):
        for j in range( n):
            if i!=j and computers[i][j] ==1:
                popi.append((i,j))
    
    visit = [0 for _ in range(n)]
    while popi:
        #answer+=1
        t,j = popi.pop()
        a=0
        if visit[t]==0:
            a+=1
            visit[t]=1
            for i in range(n):
                if computers[i][t]==1 and visit[i]==0:
                    popi.append((i,t))
        if visit[j]==0:
            a+=1
            visit[j]= 1
            for i in range(n):
                if computers[i][j]==1 and visit[i]==0:
                    popi.append((i,j))                
        if a==2:
            #print("real")
            answer+=1
    answer+= (n-sum(visit))
    return answer