https://school.programmers.co.kr/learn/courses/30/lessons/43162
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
https://github.com/stellaluminary/Programmers
GitHub - stellaluminary/Programmers
Contribute to stellaluminary/Programmers development by creating an account on GitHub.
github.com
def parent(p, x):
if p[x] != x:
p[x] = parent(p, p[x])
return p[x]
def union(p, a, b):
a = parent(p, a)
b = parent(p, b)
p[max(a, b)] = min(a, b)
def solution(n, computers):
p = [i for i in range(n)]
for i in range(n):
for j in range(n):
if i != j and computers[i][j]:
union(p, i, j)
a = []
for i in range(n):
a.append(parent(p, i))
answer = len(set(a))
return answer
def dfs(computers, visit, start, n):
visit[start] = 1
for i in range(n):
if visit[i] == 0 and computers[start][i] == 1:
dfs(computers, visit, i, n)
def solution(n, computers):
answer = 0
visit = [0] * n
for i in range(n):
if visit[i] == 0:
dfs(computers, visit, i, n)
answer += 1
return answer