Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created September 21, 2019 09:31
Show Gist options
  • Save surinoel/83382ef9b9e6084e92c74558c502b7ea to your computer and use it in GitHub Desktop.
Save surinoel/83382ef9b9e6084e92c74558c502b7ea to your computer and use it in GitHub Desktop.
#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