나는 정직한 방식으로 풀었는데 엄청난 코드 발견.......(아래 첨부)
내 코드
from collections import deque
def bfs(v, computers):
q = deque([v])
while q:
now = q.popleft()
for i, value in enumerate(computers[now]):
if value == 1:
q.append(i)
computers[now][i] = 0
def solution(n, computers):
count = 0
for i in range(n):
for j in range(n):
if computers[i][j]:
computers[i][j] = 0
bfs(j, computers)
count += 1
return count
다른 사람 코드 - 플로이드-워셜(Floyd-Warshall) 알고리즘
def solution(n, computers):
temp = []
for i in range(n):
temp.append(i)
for i in range(n):
for j in range(n):
if computers[i][j]:
for k in range(n):
if temp[k] == temp[i]:
temp[k] = temp[j]
return len(set(temp))