Created
September 21, 2019 09:31
-
-
Save surinoel/83382ef9b9e6084e92c74558c502b7ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
using namespace std; | |
vector<int> mat[200]; | |
bool visit[200]; | |
void go(int idx) { | |
if (visit[idx]) { | |
return; | |
} | |
visit[idx] = true; | |
for (int i = 0; i < mat[idx].size(); i++) { | |
int y = mat[idx][i]; | |
go(y); | |
} | |
} | |
int solution(int n, vector<vector<int>> computers) { | |
int answer = 0; | |
for (int i = 0; i < computers.size(); i++) { | |
for (int j = 0; j < computers[i].size(); j++) { | |
if (computers[i][j] == 1) { | |
mat[i].push_back(j); | |
} | |
} | |
} | |
int row = computers.size(); | |
for (int i = 0; i < row; i++) { | |
if (!visit[i]) { | |
answer += 1; | |
go(i); | |
} | |
} | |
return answer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment