[Programmers] 네트워크

Time Lapse :7min 9sec

solution.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <string>
#include <vector>
#include <queue>
#include <memory.h>
using namespace std;

int solution(int n, vector<vector<int>> computers) {
int answer = 0;
bool visit[200];
memset(visit,false,sizeof(visit));
for(int i=0;i<n;i++){
if(!visit[i]){
answer++;
queue<int> q;
q.push(i);
visit[i] = true;
while(!q.empty()){
int cur = q.front();
q.pop();
for(int j=0;j<computers[cur].size();j++){
if(computers[cur][j]==1&&!visit[j]){
q.push(j);
visit[j] = true;
}
}
}
}
}
return answer;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/network/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.