https://school.programmers.co.kr/learn/courses/30/lessons/43162
#include <string>
#include <vector>
using namespace std;
vector<bool> visited;
void dfs(int here, const vector<vector<int>>& computers)
{
visited[here] = true;
for (int i = 0; i < computers.size(); ++i)
{
if (!visited[i] && computers[here][i])
dfs(i, computers);
}
}
int solution(int n, vector<vector<int>> computers) {
//24.10.23
int answer = 0;
visited.resize(n);
for (int i = 0; i < n; ++i)
{
if (!visited[i])
{
dfs(i, computers);
++answer;
}
}
return answer;
}
'Coding Test > Programmers' 카테고리의 다른 글
[Programmers] (깊이/너비 우선 탐색(DFS/BFS)) Lv 2. 타겟 넘버 (0) | 2024.10.29 |
---|---|
[Programmers] (동적계획법(Dynamic Programming)) Lv 3. N으로 표현 (0) | 2024.10.26 |
[Programmers] (완전탐색) Lv 2. 피로도 (3) | 2024.10.23 |
[Programmers] (동적계획법(Dynamic Programming)) Lv 3. 정수 삼각형 (0) | 2024.10.21 |
[Programmers] (완전탐색) Lv 2. 소수 찾기 (0) | 2024.10.21 |
댓글