728x90
반응형
programmers.co.kr/learn/courses/30/lessons/43162
def dfs(graph, v, visited) :
tmp = 0
if not visited[v] :
visited[v] = True
for i in range( len(graph[v]) ):
if graph[v][i] :
dfs(graph, i, visited)
tmp += 1
if tmp >= 1 : return True
return False
def solution(n, computers):
visited = [False] * n
answer = 0
for i in range(n):
if dfs(computers, i , visited):
answer += 1
return answer
=> 조금 더 간결하게
def dfs(graph,v, visited):
if visited[v] == 1 :
return False
visited[v]=1
for i in range(len(graph[v])):
if graph[v][i]==1 :
dfs(graph,i,visited)
return True
def solution(n, computers):
answer = 0
visited= [0]*n
for i in range(n):
if dfs(computers,i,visited) == True :
answer += 1
return answer
728x90
반응형